Gawk output difference


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Gawk output difference
# 1  
Old 03-26-2013
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

# 2  
Old 03-26-2013
In the second command the FS is set after reading the first record.
# 3  
Old 03-26-2013
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
Quote:
Originally Posted by Don Cragun
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

# 5  
Old 03-26-2013
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

# 6  
Old 03-26-2013
Quote:
Originally Posted by Shivdatta
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Gawk --- produce the output in pattern space instead of END space

hi, I'm trying to calculate IP addresses and their respective calls to our apache Server. The standard format of the input is HOST IP DATE/TIME - - "GET/POST reuest" "User Agent" HOST IP DATE/TIME - - "GET/POST reuest" "User Agent" HOST IP DATE/TIME - - "GET/POST reuest" "User Agent" HOST... (2 Replies)
Discussion started by: busyboy
2 Replies

2. Shell Programming and Scripting

Difference in awk output and while

so, im going over one of my scripts and trying to optimize it. i have a code like this: cksum sjreas.py | awk '{prinnt $1$2}' This does what I need. However, i dont want to call the external command awk. so im doing this: cksum sjreas.py | while OFS=' ' read v1 v2 ; do printf... (4 Replies)
Discussion started by: SkySmart
4 Replies

3. Shell Programming and Scripting

Gawk output separated by tab

In the gawk below, I am trying to output the file tab-deliminated but don't think that is the correct syntax. Thank you :). gawk OFS='/t' '{sub(/-+/,"",$2); ar=$0} END{n = asort(ar) for (i = 1; i <= n; i++) print ar}' file (2 Replies)
Discussion started by: cmccabe
2 Replies

4. Shell Programming and Scripting

Difference output of files

Need help on below req Compare two files and send difference of file to other file File2 is static which never changes ex: File1 A.txt B.ttx C.txt E.txt File2 A.txt (6 Replies)
Discussion started by: satish1222
6 Replies

5. Shell Programming and Scripting

Trying to learn to use functions in gawk and not getting expected output.

I've been working on improving my awk, and the next thing I want to learn is to properly use functions (I understand functions in shell and python). I have the following code which includes how I did this without functions before, and two attempts I've made to do it with functions: function... (3 Replies)
Discussion started by: DeCoTwc
3 Replies

6. Solaris

Difference in date output

HiCan anyone tell me why I am getting a difference in the date format on 2 different Solaris servers?On one I get: -Monday, 9 November 2009 09:02:45 GMTand the other: -Monday November 9 09:03:05 GMT 2009Both servers are running OS Version M-11/16/88iCan anyone tell me why one uses a "," and the... (5 Replies)
Discussion started by: steadyonabix
5 Replies

7. UNIX for Advanced & Expert Users

GAWK removes FS | on output

I have the simple gawk script below. When the script runs in the output of all the ITM lines the FS is replaced with a space, the Non ITM lines retain the | field separator. The ITM lines have many fields and I can't insert "|" between each field because some of the fields are blank. Is... (1 Reply)
Discussion started by: paulr211
1 Replies

8. HP-UX

Difference in netstat -a and -an output.

Hi, Does anyone know why I get a different output when using "netstat -a" or "netstat -an" ?? # netstat -a | grep ts15r135 tcp 0 0 nbsol152.62736 ts15r135.23211 ESTABLISHED # netstat -an | grep 172.23.160.78 tcp 0 0 135.246.39.152.51954 ... (4 Replies)
Discussion started by: ejdv
4 Replies

9. Shell Programming and Scripting

How to find date Difference in AWK/GAWK with millisecond precision

Hi, I have a log file that has the date in this format "2006-05-30_13:14:04,256". I need to find the time difference between two log entries in milliseconds. How to achieve this in AWK/GAWK script? :confused: (2 Replies)
Discussion started by: omprasad
2 Replies

10. UNIX for Dummies Questions & Answers

How to output the difference of two files?

Hi, I had two data file (File1, File2), each one just have one column, but two file were very big. File2 is smaller, all its data included in File1. I want to ouput the result which don't have any data in File2. Could any one give me a help on how to do that? Thanks in advance! Yun ... (4 Replies)
Discussion started by: yxiao
4 Replies
Login or Register to Ask a Question