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.

Gawk output difference

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 03-26-2013
Shivdatta Shivdatta is offline
Registered User
 
Join Date: Mar 2006
Last Activity: 7 January 2015, 3:22 AM EST
Posts: 104
Thanks: 0
Thanked 1 Time in 1 Post
Gawk output difference

Why the below option2 doesn't fetch similar output as option1 ? I am on linux.


Code:
$cat test
2013-01-01-00.25.43.643845

Option1:
cat test | gawk -F"-" ' {print $2 " " $3 " " $1}'
01 01 2013

Option2:
cat test | gawk '{FS="-"}  {print $2 " " $3 " " $1}'
2013-01-01-00.25.43.643845

Sponsored Links
    #2  
Old Unix and Linux 03-26-2013
Franklin52's Unix or Linux Image
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 27 July 2015, 1:09 PM EDT
Location: The Netherlands
Posts: 7,731
Thanks: 129
Thanked 547 Times in 515 Posts
In the second command the FS is set after reading the first record.
Sponsored Links
    #3  
Old Unix and Linux 03-26-2013
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 31 July 2015, 5:09 AM EDT
Location: San Jose, CA, USA
Posts: 6,819
Thanks: 289
Thanked 2,274 Times in 1,945 Posts
You have to change the field separator before you read the record or you have to force awk to re-evaluate the record to make the change in FS take effect. So, either of the following should work:

Code:
gawk 'BEGIN{FS="-"}  {print $2 " " $3 " " $1}' test

or
gawk '{FS="-";$1=$1} {print $2 " " $3 " " $1}' < test
Note that there is no need to use cat and a pipeline instead of the simple command letting awk open the file itself or using redirection in the shell to set the input file before kicking off awk.
    #4  
Old Unix and Linux 03-26-2013
Franklin52's Unix or Linux Image
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 27 July 2015, 1:09 PM EDT
Location: The Netherlands
Posts: 7,731
Thanks: 129
Thanked 547 Times in 515 Posts
Quote:
Originally Posted by Don Cragun View Post
You have to change the field separator before you read the record or you have to force awk to re-evaluate the record to make the change in FS take effect. So, either of the following should work:

Code:
gawk 'BEGIN{FS="-"}  {print $2 " " $3 " " $1}' test

or
gawk '{FS="-";$1=$1} {print $2 " " $3 " " $1}' < test
Note that there is no need to use cat and a pipeline instead of the simple command letting awk open the file itself or using redirection in the shell to set the input file before kicking off awk.
Should be:

Code:
$0=$0

Sponsored Links
    #5  
Old Unix and Linux 03-26-2013
Shivdatta Shivdatta is offline
Registered User
 
Join Date: Mar 2006
Last Activity: 7 January 2015, 3:22 AM EST
Posts: 104
Thanks: 0
Thanked 1 Time in 1 Post
Thanks, that helps.

Was just wondering , if this thing is specific to GAWK 'coz nawk works fine on Solaris


Code:
$ cat test  | nawk '{FS="-"}  {print $2 " " $3 " " $1}' 
03 01 2013

Sponsored Links
    #6  
Old Unix and Linux 03-26-2013
radoulov's Unix or Linux Image
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 23 April 2015, 3:05 PM EDT
Location: Варна, България / Milano, Italia
Posts: 5,690
Thanks: 184
Thanked 626 Times in 584 Posts
Quote:
Originally Posted by Shivdatta View Post
Thanks, that helps.

Was just wondering , if this thing is specific to GAWK 'coz nawk works fine on Solaris


Code:
$ cat test  | nawk '{FS="-"}  {print $2 " " $3 " " $1}' 
03 01 2013

Yes, different awk versions implement field splitting differently:


Code:
$ echo a:b | /usr/xpg4/bin/awk '{ FS = ":"; print NF }'
1
$ echo a:b | nawk '{ FS = ":"; print NF }'
2

That's why usually the correct way to assign a non-default field separator is in the BEGIN block or directly on the command line.


Code:
$ awk -F- '{ print $2, $3, $1 }' infile
01 01 2013
$ nawk -F- '{ print $2, $3, $1 }' infile
01 01 2013
$ /usr/xpg4/bin/awk -F- '{ print $2, $3, $1 }' infile
01 01 2013

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
Difference in date output steadyonabix Solaris 5 11-10-2009 05:51 AM
GAWK removes FS | on output paulr211 UNIX for Advanced & Expert Users 1 10-09-2009 06:39 PM
Difference in netstat -a and -an output. ejdv HP-UX 4 08-05-2009 07:59 AM
How to find date Difference in AWK/GAWK with millisecond precision omprasad Shell Programming and Scripting 2 06-06-2006 04:59 AM
How to output the difference of two files? yxiao UNIX for Dummies Questions & Answers 4 01-26-2004 12:55 PM



All times are GMT -4. The time now is 05:21 AM.