Awk code to select multiple patterns

2019-07-17 07:42发布

This is my input file, say modified.txt

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

My output should be like:

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 input file is a sample svn-log file. I want to filter all modified (M) files with their respective credentials. Can someone help with some sample code. Thanks in advance.

标签: linux shell awk
1条回答
乱世女痞
2楼-- · 2019-07-17 08:27
awk -F"|" '/^r/{a=$1;b=$2;c=substr($3,0,20)}/^   M/{gsub(/   M /," ");print a"|"b"|"c"|"$0}' your_file

tested:

> awk -F"|" '/^r/{a=$1;b=$2;c=substr($3,0,20)}/^   M/{gsub(/   M /," ");print a"|"b"|"c"|"$0}' temp
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

explanation:

/^r/{a=$1;b=$2;c=substr($3,0,20)}

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 :

2012-08-28 07:12:33 -0400 (Tue, 28 Aug 2012)

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.

M/{gsub(/ M /," ");print a"|"b"|"c"|"$0}

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.

查看更多
登录 后发表回答