LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-09-2012, 07:48 PM   #1
fantasy1215
Member
 
Registered: Oct 2011
Posts: 70

Rep: Reputation: Disabled
awk sub odd behavior!


My awk version:GNU Awk 3.1.5
I have a data file, well formatted, some fields are missing in some lines, but still well formatted. So the output to format well is my command.
I need to add some string to the last field, And I've learned I can use awk's sub function, But sub's behavior is very odd.
This is very data file, I paste two lines of it.
Code:
09:31:47 09:31:47 2012050700501027 110 0880158402  900758400030060111  222584060124001 10104001 050700501027 501027 000000000001 0507     [0000]
09:31:44 09:31:44 2012050700501026 900 0880158402                      222584060124001 10104001              501026 0            0507     [9999]
I use the following code:
Code:
cat data|awk '{sub($NF,"here"$NF"here");print}'

but the output is as follow, which is not the result I expected

here[0000]here9:31:47 09:31:47 2012050700501027 110 0880158402  900758400030060111  222584060124001 10104001 050700501027 501027 000000000001 0507     [0000]
0here[9999]here:31:44 09:31:44 2012050700501026 900 0880158402                      222584060124001 10104001              501026 0            0507     [9999]
Code:
cat data|awk '{sub($2,"here"$2"here");print}'

the output is as follow, which is odd too:

here09:31:47here 09:31:47 2012050700501027 110 0880158402  900758400030060111  222584060124001 10104001 050700501027 501027 000000000001 0507     [0000]
here09:31:44here 09:31:44 2012050700501026 900 0880158402                      222584060124001 10104001              501026 0            0507     [9999]
but when I use
Code:
cat data|awk '{sub($1,"here"$1"here");print}'
cat data|awk '{sub($3,"here"$3"here");print}'
would be fine.

Last edited by fantasy1215; 07-09-2012 at 07:51 PM.
 
Old 07-09-2012, 11:48 PM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,489

Rep: Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891
Firstly, please don't cat through a pipe when awk can readily read files.
Code:
awk '...' file
Secondly, your issue is not considering your data.

Imagine the following scenario:
Code:
echo "9 at the start and the end 9" | awk 'sub(/[9]/,"10")'
Which '9' do you expect to be changed?

Now look at your data in the last field.
 
Old 07-10-2012, 02:04 AM   #3
fantasy1215
Member
 
Registered: Oct 2011
Posts: 70

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
Firstly, please don't cat through a pipe when awk can readily read files.
Code:
awk '...' file
Secondly, your issue is not considering your data.

Imagine the following scenario:
Code:
echo "9 at the start and the end 9" | awk 'sub(/[9]/,"10")'
Which '9' do you expect to be changed?

Now look at your data in the last field.
Sorry, I don't get what you mean. I can't see what wrong with my data.
I give awk the $NF to indicate the last field, then what am I wrong with that?
Still thanks for your quick reply.

Last edited by fantasy1215; 07-10-2012 at 02:15 AM.
 
Old 07-10-2012, 03:35 AM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,489

Rep: Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891
What does $NF equal? As per your example, on the first line the last field is '[0000]'. Looking at my example, your data will be interpreted as a character class.
ie. each item inside the square brackets (all just happen to be zero) are searched for and the first occurrence is replaced with your new string. As the first '0'
in your data is at the start of the string, it is now replaced with your last field between your changes.
 
Old 07-10-2012, 03:45 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Read the description of the sub function here carefully:

http://www.gnu.org/software/gawk/man...Functions.html

Note that the first argument is treated as a regular expression. The last fields in your input are of the pattern of "[0123]", which are valid regex syntax (i.e. match zero, one, two, or three). So it simply finds the first character on the line that matches one of those numbers and does the substitution on it.

You can handle the problem here by using the third, "target" argument, and use in the first argument a regex that simply matches all the text in that field.

Code:
awk '{sub(/.*/,"here"$NF"here",$NF) ; print}' infile.txt
(Edit: Whoops, sorry grail. I didn't intend to bypass your teaching moment. But I do think this would've been a bit difficult for him to figure out on his own anyway. It even took me a few minutes to figure it out.)

Last edited by David the H.; 07-10-2012 at 03:47 AM. Reason: as stated
 
Old 07-10-2012, 03:47 AM   #6
fantasy1215
Member
 
Registered: Oct 2011
Posts: 70

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
What does $NF equal? As per your example, on the first line the last field is '[0000]'. Looking at my example, your data will be interpreted as a character class.
ie. each item inside the square brackets (all just happen to be zero) are searched for and the first occurrence is replaced with your new string. As the first '0'
in your data is at the start of the string, it is now replaced with your last field between your changes.
Oh, I see, Your explaination is very helpful, and I understand that now.
Now, I just want to insert here and here before and after arbitrary field, How could I achieve that goal?
And thanks for your detail explaination again.
 
Old 07-10-2012, 03:55 AM   #7
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,489

Rep: Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891Reputation: 1891
No problems David I am always happy to have others provide feedback

OP - the point will be that it will depend on the data in the field, hence you will need to have an understanding of your data so we may best assist you.
 
Old 07-10-2012, 07:55 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Quote:
Originally Posted by fantasy1215 View Post
Now, I just want to insert here and here before and after arbitrary field, How could I achieve that goal?
I showed you how in my last post. Although actually, you don't even need to use sub for this. You can simply re-set the contents of the field before you print it.

Code:
awk 'BEGIN{ FS=OFS="|" } { $NF="here"$NF"here" } { print }' file.txt
For other fields, simply replace $NF with the number of the field.

Last edited by David the H.; 07-10-2012 at 08:00 AM. Reason: added whimsy
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] BASH: Odd behavior gearge Programming 11 09-08-2011 10:19 AM
ODD behavior in KDE mkhan919 Linux - Newbie 6 01-24-2007 05:09 AM
Odd TCP Behavior Rawjoe Linux - Networking 1 12-14-2006 10:53 AM
postgresql odd behavior doublefailure Linux - Software 1 08-28-2002 12:40 AM
RH 6.2 ... odd behavior jubal Linux - Networking 3 02-27-2001 09:04 AM


All times are GMT -5. The time now is 09:41 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration