I was reading this question
Awk code to select multiple patterns
The user has this as input
------------------------------------------------------------------------
r4544 | n479826 | 2012-08-28 07:12:33 -0400 (Tue, 28 Aug 2012) | 1 line
Changed paths:
M /branches/8.6.0/conf/src/main/config/RTSConfig.xml
CET-402: some text comment
------------------------------------------------------------------------
r4550 | n479826 | 2012-09-04 05:51:29 -0400 (Tue, 04 Sep 2012) | 1 line
Changed paths:
M /branches/8.6.0/conf/src/main/config/RTSConfig.xml
M /branches/8.6.0/conf/src/main/config/base.cfg
M /branches/8.6.0/conf/src/main/config/prod.cfg
M /branches/8.6.0/conf/src/main/config/qa.cfg
M /branches/8.6.0/conf/src/main/config/uat.cfg
CET-438: some text comment
and he wants this as output
r4544 | n479826 | 2012-08-28 07:12:33 | /branches/8.6.0/conf/src/main/config/RTSConfig.xml
r4550 | n479826 | 2012-09-04 05:51:29 | /branches/8.6.0/conf/src/main/config/RTSConfig.xml
r4550 | n479826 | 2012-09-04 05:51:29 | /branches/8.6.0/conf/src/main/config/base.cfg
r4550 | n479826 | 2012-09-04 05:51:29 | /branches/8.6.0/conf/src/main/config/prod.cfg
r4550 | n479826 | 2012-09-04 05:51:29 | /branches/8.6.0/conf/src/main/config/qa.cfg
r4550 | n479826 | 2012-09-04 05:51:29 | /branches/8.6.0/conf/src/main/config/uat.cfg
The correct answer is this
awk -F"|" '/^r/{a=$1;b=$2;c=substr($3,0,20)}/^ M/{gsub(/ M /," ");print a"|"b"|"c"|"$0}' your_file
I didn’t understood that fully.
Now i got this part
/^r/{a=$1;b=$2;c=substr($3,0,20)}/^
but i didn’t get the second part
M/{gsub(/ M /," ");print a"|"b"|"c"|"$0}
My problems are
- What is
Mmeans in the beginning
2.Now awk will read the file line by line so it means at the second line
i.e Chngaed paths the value ofa =0because there is no field separator|on that line - Now when the awk comes to third line
then againa,b,c =0and$0 = /bracnhesbut how is the result still showing the old value of a,b,c
I am getting confused when awk is used on multi lines
I was lazy enough to explain the answer 🙂
But lemme put my lazyness aside for some time now:
The above block of code will execute only when the line starts with a letter r.
inside the block says store the first field in a ,second field in b and third field from input is :
but i need only the date with timestamp and the rest is obsolete for me.
it is always 20 characters.
so i took a substring from the third field and stored it in c.
my main interest was the line which starts with /^ M/ which i have to display with the information present in the previous line which start with r
and for sure there is a line which starts with r before our desired line which has all the information i have to prepend the lines which start with M.
so every time a line starts with M will be prepended with the values stored in a b and c.
gsub part will remove the part of ” M ” with a space from the current line.
print part just prepends the value of a b and c to the current line with | as teh separator.
This what the logic is!
I will be back into my lazy mode now 🙂