简体   繁体   中英

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

[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. 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. I could've just piped into 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
  • OFS will set the Output Field Separator to ,
sed 's/\t+/,/g' output > output2

"sed" is a command that finds and replaces Regex expressions, eg tabs with commas. If you want to read about more things sed can do, type "man sed" into the command line.

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 . To trigger the output to be reformatted, you have to assign to one of the fields or $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=, 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM