crashmaxed
crashmaxed

Reputation: 540

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'

Upvotes: 1

Views: 612

Answers (4)

BMW
BMW

Reputation: 45293

This script will be more straight forward, if yum output keeps same format.

yum -C check-update 2>/dev/null | awk 'NR>3{$1=$1;print}' OFS=, 

Upvotes: 0

jaypal singh
jaypal singh

Reputation: 77155

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 ,

Upvotes: 1

Barmar
Barmar

Reputation: 781868

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 }'

Upvotes: 0

La-comadreja
La-comadreja

Reputation: 5755

sed 's/\t+/,/g' output > output2

"sed" is a command that finds and replaces Regex expressions, e.g. 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'

Upvotes: 0

Related Questions