Home > bash > awk – Using multiple Input Field Separators

awk – Using multiple Input Field Separators

Problem:
Each record is separated by |~|

Solution:

$echo "9882590|~|20111207|~|K03501000063005574033961|~|K|~|" | awk -v FS="[|~]+" -v OFS = "," '{print $1,$2,$3,$4}'

Source:
http://www.unix.com/shell-programming-scripting/172903-read-multiple-character-delimited-flat-file.html

Update: 2012-11-20 15:58:50
Another example:
I wanted to get rid of the time portion in the first filed. This part in the awk command FS=” |\|” says the Field separator is either a space or a pipe character.

$ less file
# Date time | Number
2012-10-01 06:08:54|377772327281
2012-10-01 06:08:55|371202225977
2012-10-01 06:08:55|061712877272
2012-10-01 06:08:55|671472162075
2012-10-01 06:08:55|179001295722
$ awk -v FS=" |\|" '{print $1, $3}' file | sort -k2,1 
2012-10-01 061712877272
2012-10-01 179001295722
2012-10-01 371202225977
2012-10-01 377772327281
2012-10-01 671472162075

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: