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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
07-09-2012, 07:48 PM
|
#1
|
|
Member
Registered: Oct 2011
Posts: 53
Rep: 
|
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.
|
|
|
|
07-09-2012, 11:48 PM
|
#2
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,322
|
Firstly, please don't cat through a pipe when awk can readily read files.
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.
|
|
|
|
07-10-2012, 02:04 AM
|
#3
|
|
Member
Registered: Oct 2011
Posts: 53
Original Poster
Rep: 
|
Quote:
Originally Posted by grail
Firstly, please don't cat through a pipe when awk can readily read files.
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.
|
|
|
|
07-10-2012, 03:35 AM
|
#4
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,322
|
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.
|
|
|
|
07-10-2012, 03:45 AM
|
#5
|
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,577
|
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
|
|
|
|
07-10-2012, 03:47 AM
|
#6
|
|
Member
Registered: Oct 2011
Posts: 53
Original Poster
Rep: 
|
Quote:
Originally Posted by grail
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.
|
|
|
|
07-10-2012, 03:55 AM
|
#7
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,322
|
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.
|
|
|
|
07-10-2012, 07:55 AM
|
#8
|
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,577
|
Quote:
Originally Posted by fantasy1215
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
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:25 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|