Register
It is currently Fri Apr 18, 2014 7:09 am

awk on a "|" delimited file (that's a pipe)


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Mon May 16, 2005 8:30 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 657
Location: Des Moines, Iowa
Originally posted by mekanik

here's a quick awk snippet which acts on a Field Separator -FS set as a pipe - "|"...this can be changed to a :, ,, ;...the output will separate each field by a newline...

Code:
awk 'BEGIN {FS = "|"} \
     {f1 = $1; f2 = $2; f3 = $3; f4 = $4; f5 = $5; f6 = $6; f7 = $7; f8 = $8} \
     {print "------\n"f1"\n"f2"\n"f3"\n"f4"\n"f5"\n"f6"\n"f7"\n"f8"------\n"}' somefile.txt


Quote:
stdin:
field1|field2|field3|field4|field6|field7|field8

stdout:
------
field1
field2
field3
field4
field5
field6
field7
field8
------


endless possibilities...i would also like to note, that you don't have to set the positional parameters to names, you can just call them by their position if you want...

:)


Top
 Profile WWW  
 PostPosted: Fri Aug 26, 2005 3:48 am   

Joined: Sat Jun 04, 2005 3:16 am
Posts: 16
Location: That Lunar Colony That Everybody Denies the Existance of.
Is there a way to convert a single line delimited with : (colons) into a colum of lines with an arbitary number of fields? Like:

"1:2:3:4:5:6" into:
"1
2
3
4
5
6"

The " marks are just to be explicit when the line or column of lines begin and end.


Top
 Profile  
 PostPosted: Fri Aug 26, 2005 4:52 pm   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
crouse c/o mekanik wrote:
Code:
awk 'BEGIN {FS = ":"} \
     {f1 = $1; f2 = $2; f3 = $3; f4 = $4; f5 = $5; f6 = $6; f7 = $7; f8 = $8} \
     {print "------\n"f1"\n"f2"\n"f3"\n"f4"\n"f5"\n"f6"\n"f7"\n"f8"------\n"}' somefile.txt


Quote:
stdin:
1:2:3:4:5:6

stdout:
------
field1
field2
field3
field4
field5
field6
field7
field8
------

jbsnake wrote:
notice i just changed the value of FS in the line of code
from | to :


:)


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP