AWK代码选择多种模式 [英] Awk code to select multiple patterns

查看:83
本文介绍了AWK代码选择多种模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的输入文件,例如Modifyed.txt

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

我的输出应为:

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

输入文件是一个示例svn-log文件.我想用它们各自的凭据过滤所有修改过的(M)文件. 有人可以帮忙提供一些示例代码吗?预先感谢.

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.

推荐答案

awk -F"|" '/^r/{a=$1;b=$2;c=substr($3,0,20)}/^   M/{gsub(/   M /," ");print a"|"b"|"c"|"$0}' your_file

经过测试:

> 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

说明:

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

仅当行以字母r开头时,以上代码块才会执行. 块内表示将第一个字段存储在a中,将第二个字段存储在b中,输入的第三个字段是:

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)

但是我只需要带有时间戳的日期,其余的对于我来说已经过时了. 它始终是20个字符. 所以我从第三个字段中提取了一个子字符串并将其存储在c中.

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.

我的主要兴趣是以/^ M/开头的行,我必须将其与上一行以r开头的信息一起显示 并且可以肯定,在我们想要的行之前有一行以r开头的行,其中包含所有信息,我必须在以M开头的行之前加行.

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.

因此,每次以M开头的行都将以存储在b和c中的值开头.

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将从当前行中删除带有空格的"M"部分. 打印部分只是将a和b的值添加到当前行|中.作为分隔符.

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.

这篇关于AWK代码选择多种模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆