Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

awk - treat multiple delimiters as one

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 08-28-2002
peter.herlihy peter.herlihy is offline
Registered User
 
Join Date: Nov 2001
Last Activity: 1 August 2006, 11:51 AM EDT
Location: New Zealand
Posts: 333
Thanks: 0
Thanked 0 Times in 0 Posts
awk - treat multiple delimiters as one

Is there anyway to get awk to treat multiple delimiters as one? Particularly spaces...
Sponsored Links
    #2  
Old Unix and Linux 08-28-2002
peter.herlihy peter.herlihy is offline
Registered User
 
Join Date: Nov 2001
Last Activity: 1 August 2006, 11:51 AM EDT
Location: New Zealand
Posts: 333
Thanks: 0
Thanked 0 Times in 0 Posts
okay.... figured this out....Linux

awk -F" +"

trailing + will mean one or more - if you're interested!
Sponsored Links
    #3  
Old Unix and Linux 08-29-2002
Optimus_P Optimus_P is offline Forum Advisor  
flim flam flamma jamma
 
Join Date: May 2001
Last Activity: 24 March 2008, 9:43 PM EDT
Location: Chicago IL, USA
Posts: 1,006
Thanks: 0
Thanked 2 Times in 1 Post
not sure why you would have to specify multiple spaces as a delimiter. awk's default delimiter is a space.


Code:
[hostname]/export/home/optimus$echo 'this   is a test'|awk '{ print $1 $2 $3 }'
thisisa
[hostname]/export/home/optimus$

    #4  
Old Unix and Linux 08-29-2002
peter.herlihy peter.herlihy is offline
Registered User
 
Join Date: Nov 2001
Last Activity: 1 August 2006, 11:51 AM EDT
Location: New Zealand
Posts: 333
Thanks: 0
Thanked 0 Times in 0 Posts
I see..... yes that's quite good.... but I'm a little confused now. I see that the default is to use either tab or space....and that it by default treats these consecutively as one. But with other delimiters that I specify it didn't seem to work....

i..e

1|2|3|4
1||2||3
1|||||2|||||3

###FIRST
$ awk -F"|+" ' { print $1,$2,$3} ' my_file
1 2 3
1 2
1
##SECOND
$ awk -F"|" ' { print $1,$2,$3} ' my_file
1 2 3
1 2
1
Sponsored Links
    #5  
Old Unix and Linux 08-30-2002
oombera's Unix or Linux Image
oombera oombera is offline Forum Advisor  
Registered User
 
Join Date: Aug 2002
Last Activity: 3 October 2004, 6:07 PM EDT
Location: Cleveland, OH
Posts: 804
Thanks: 0
Thanked 6 Times in 5 Posts
It should work if you add brackets around the delimiter:

In a script:
awk -F"[|]+" ' { print $1,$2,$3} ' my_file

From the command line:
echo '1||2|3|4'|awk -F"[|]+" '{print $1,$2,$3}'
Sponsored Links
    #6  
Old Unix and Linux 08-30-2002
Kelam_Magnus's Unix or Linux Image
Kelam_Magnus Kelam_Magnus is offline Forum Advisor  
Registered User
 
Join Date: Aug 2001
Last Activity: 18 April 2013, 3:03 PM EDT
Location: San Antonio, TX,
Posts: 1,070
Thanks: 0
Thanked 5 Times in 5 Posts
great tip!

Cool! And it even works with different multiples of a delimiter.

# cat myfile
1|||2||||3|||||4

# awk -F"[|]+" ' { print $1,$2,$3,$4} ' myfile
1 2 3 4


Awesome!!
Linux Linux
Sponsored Links
    #7  
Old Unix and Linux 08-30-2002
oombera's Unix or Linux Image
oombera oombera is offline Forum Advisor  
Registered User
 
Join Date: Aug 2002
Last Activity: 3 October 2004, 6:07 PM EDT
Location: Cleveland, OH
Posts: 804
Thanks: 0
Thanked 6 Times in 5 Posts
*beaming at the praise* Linux Linux
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
treating multiple delimiters[solved] sam_bd Shell Programming and Scripting 1 05-23-2012 11:29 AM
Sorting problem: Multiple delimiters, multiple keys Ryan. Shell Programming and Scripting 20 07-13-2011 08:41 AM
Sorting based on multiple delimiters gimley Shell Programming and Scripting 8 05-04-2011 12:20 AM
AWK with multiple delimiters gdub Shell Programming and Scripting 7 09-06-2010 05:03 PM
Cutting a file with multiple delimiters into columns luckycharm Shell Programming and Scripting 7 04-02-2010 06:29 AM



All times are GMT -4. The time now is 01:34 AM.