Capture column from command output and convert it to a row to pass it further.
The simplest usage scenario is to convert single column output to TAB separated row.
$ pgrep dnsmasq | paste -s
2430 2431
This solution does not play well with ps
output as you get redundant space characters because data is aligned using those.
$ ps -opid= -u netdata | paste -s
1943 1985 24046 24137
You can remove all space characters and keep values separated using TAB.
$ ps -opid= -u netdata | paste -s | tr -d ' '
1943 1985 24046 24137
Alternatively, you can squeeze space characters, ignore these at the beginning of the row, and use remaining space characters to keep values separated.
$ ps -opid= -u netdata | paste -s -d " " | tr -s ' ' | sed 's.^ *..'
1943 1985 24046 24137
It gets slightly more complicated when you want to extract a specific column.
$ ps ax --no-header | grep -v grep | grep sshguard | tr -s " " | sed "s.^ *.." | cut -d " " -f1 | paste -s -d ' ' 1394 1449
There is an alternative, of course. Use awk
to print specific column using custom output record separator (ORS
built-in variable).
$ ps ax --no-header | awk -v ORS=" " '/firefox/ {print $1} END{printf "\n"}'
3867 3947 4058 4352 4604 4688 22346