ProgrammingThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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]
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.
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.
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.
(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
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.