Gawk output difference | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Gawk output difference

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-26-2013
Shivdatta Shivdatta is offline
Registered User
 
Join Date: Mar 2006
Last Activity: 20 May 2014, 1:42 AM EDT
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 03-26-2013
Franklin52's Avatar
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 31 August 2014, 10:29 AM EDT
Location: The Netherlands
Posts: 7,706
Thanks: 121
Thanked 535 Times in 505 Posts
In the second command the FS is set after reading the first record.
Sponsored Links
    #3  
Old 03-26-2013
Don Cragun's Avatar
Don Cragun Don Cragun is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 1 September 2014, 7:11 AM EDT
Location: San Jose, CA, USA
Posts: 4,484
Thanks: 177
Thanked 1,504 Times in 1,276 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 03-26-2013
Franklin52's Avatar
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 31 August 2014, 10:29 AM EDT
Location: The Netherlands
Posts: 7,706
Thanks: 121
Thanked 535 Times in 505 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 03-26-2013
Shivdatta Shivdatta is offline
Registered User
 
Join Date: Mar 2006
Last Activity: 20 May 2014, 1:42 AM EDT
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 03-26-2013
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 31 August 2014, 3:16 PM EDT
Location: Варна, България / Milano, Italia
Posts: 5,667
Thanks: 182
Thanked 617 Times in 575 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 Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 07:18 AM.