Grep在某些字符之后选择备用输出



我有一个文本文件test.txt

It has list as follows
Profile Roy
 City: New York
 Age: 78_months
 Sex: Male
Profile Smith
 City: Texas
 Age: 56_months
 Sex: Male

我想要的是grep输出的Roy和Smith等等......这样它应该输出为

78_months New York
56_months Texas

目前为止我所尝试的都没有成功

grep -l 'Age:|City:' test.txt >> output txt

我不会在这里使用grep,查看sed或awk ..

awk/sed的例子,我们有一段时间没有使用它们了…

<>以前人的名字高街123号(222) 466 - 1234另一个人高街487号(523) 643 - 8754$ awk 'BEGIN{FS="n";RS = " "}{打印$ 1、$ 3}的地址

将输出

人名(222)466-1234另一个人(523)643-8754

so awk '/Age|City/,1'将输出

<>之前城市:纽约年龄:78 _months城市:德州年龄:56个月Then awk awk '{x = $0;getline;打印;打印x} '

将换行到

年龄:78个月城市:纽约年龄:56 _months城市:Texas

所以一个快捷的方法是

awk '/Age|City/,1' test | awk '{x = $0;getline;打印;打印x} ' | sed - r ' s/年龄://g ' | sed - r ' s/城市://g"| xargs n2 | sed的s/*//g">

: p

78_months New York56 _months德州

建议使用awk脚本:

 awk '{print $5, $3}' RS="Profile" FS="[:n]" input.1.txt

提供输入的输出:

$ awk '{print $5, $3}' RS="Profile" FS="[:n]" input.1.txt
 
 78_months  New York
 56_months  Texas

解释:

{print $5, $3} 打印第5个字段和第3个字段,后跟换行符。

RS="Profile" 设置awk记录分隔符为RegExp "Profile">

FS="[:n]" 设置awk字段分隔符为char :和换行n

最新更新