[英]Using awk print all lines after a regular expression and change output field separator
I've been using the "b" example from another stackoverflow post to return all the rows after a pattern match (empty line - $^) and not including the match until EOF. 我一直在使用另一个stackoverflow帖子中的“ b”示例来返回模式匹配(空行-$ ^)之后的所有行,直到EOF才包括该匹配。 The first example is the unfiltered output for reference (including the empty line after "Loading support for...") and the second is what currently gets output using awk. 第一个示例是未经过滤的输出以供参考(包括“正在加载支持...”之后的空行),第二个示例是当前使用awk获取的输出。
[root@servername ~]# yum -C check-update 2>/dev/null
Loaded plugins: kabi, product-id, refresh-packagekit, security, subscription-manager
Loading support for Red Hat kernel ABI
kernel.x86_64 2.6.32-431.5.1.el6 rhel-6-server-rpms
kernel-abi-whitelists.noarch 2.6.32-431.5.1.el6 rhel-6-server-rpms
kernel-devel.x86_64 2.6.32-431.5.1.el6 rhel-6-server-rpms
kernel-doc.noarch 2.6.32-431.5.1.el6 rhel-6-server-rpms
kernel-firmware.noarch 2.6.32-431.5.1.el6 rhel-6-server-rpms
kernel-headers.x86_64 2.6.32-431.5.1.el6 rhel-6-server-rpms
[root@servername ~]# yum -C check-update 2>/dev/null | awk 'f;/$^/{f=1}'
kernel.x86_64 2.6.32-431.5.1.el6 rhel-6-server-rpms
kernel-abi-whitelists.noarch 2.6.32-431.5.1.el6 rhel-6-server-rpms
kernel-devel.x86_64 2.6.32-431.5.1.el6 rhel-6-server-rpms
kernel-doc.noarch 2.6.32-431.5.1.el6 rhel-6-server-rpms
kernel-firmware.noarch 2.6.32-431.5.1.el6 rhel-6-server-rpms
kernel-headers.x86_64 2.6.32-431.5.1.el6 rhel-6-server-rpms
I can't figure out how to go about changing the awk command to use a different output field separator "," to output in CSV format. 我不知道如何更改awk命令以使用其他输出字段分隔符“,”以CSV格式输出。 I want the output to look like this: 我希望输出看起来像这样:
kernel.x86_64,2.6.32-431.5.1.el6,rhel-6-server-rpms
kernel-abi-whitelists.noarch,2.6.32-431.5.1.el6,rhel-6-server-rpms
kernel-devel.x86_64,2.6.32-431.5.1.el6,rhel-6-server-rpms
kernel-doc.noarch,2.6.32-431.5.1.el6,rhel-6-server-rpms
kernel-firmware.noarch,2.6.32-431.5.1.el6,rhel-6-server-rpms
kernel-headers.x86_64,2.6.32-431.5.1.el6,rhel-6-server-rpms
Edit: Trying to keep it in awk and as a one-liner. 编辑:尝试将其保持awk和单线。 I could've just piped into awk '{OFS=","} $2 = $2'
我本来可以用awk '{OFS=","} $2 = $2'
Modify your existing command to - 将现有命令修改为-
yum -C check-update 2>/dev/null | awk '{$1=$1}f;/$^/{f=1}' OFS=','
{$1=$1}
That's just an awk idiom that forces awk to recompute the value of $1
{$1=$1}
这只是一个迫使awk重新计算$1
值的awk习语 OFS
will set the Output Field Separator to ,
OFS
会将输出字段分隔符设置为,
sed 's/\t+/,/g' output > output2
"sed" is a command that finds and replaces Regex expressions, eg tabs with commas. “ sed”是用于查找和替换正则表达式的命令,例如带逗号的制表符。 If you want to read about more things sed can do, type "man sed" into the command line. 如果您想了解sed可以做的更多事情,请在命令行中输入“ man sed”。
The full command is as follows: 完整命令如下:
yum -C check-update 2>/dev/null | awk 'f;/$^/{f=1}' | sed 's/\t+/,/g'
You change the output field separator by setting OFS
. 您可以通过设置OFS
更改输出字段分隔符。 To trigger the output to be reformatted, you have to assign to one of the fields or $0
; 要重新格式化输出,您必须将其分配给字段之一或$0
; if you're not modifying anything, the idiom is to assign the line to itself: 如果您不进行任何修改,则习惯用法是将行分配给自身:
awk -v OFS=, 'f { $0 = $0; print; }
/^$/ { f = 1 }'
如果yum输出保持相同格式,则此脚本将更加简单。
yum -C check-update 2>/dev/null | awk 'NR>3{$1=$1;print}' OFS=,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.