Awk code to select multiple patterns

偶尔善良 提交于 2020-01-05 03:27:08

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/14336710/awk-code-to-select-multiple-patterns

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!