LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   awk sub odd behavior! (https://www.linuxquestions.org/questions/programming-9/awk-sub-odd-behavior-4175415796/)

fantasy1215 07-09-2012 07:48 PM

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.

grail 07-09-2012 11:48 PM

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.

fantasy1215 07-10-2012 02:04 AM

Quote:

Originally Posted by grail (Post 4723551)
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.

grail 07-10-2012 03:35 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.

David the H. 07-10-2012 03:45 AM

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.)

fantasy1215 07-10-2012 03:47 AM

Quote:

Originally Posted by grail (Post 4723731)
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.

grail 07-10-2012 03:55 AM

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.

David the H. 07-10-2012 07:55 AM

Quote:

Originally Posted by fantasy1215 (Post 4723747)
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.


All times are GMT -5. The time now is 06:06 AM.