Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine output from two commands into single table with shell script

I want to display the output of the following commands which are as below-:

1)

mount | grep -i "/dev/sd*" | awk '{ print NR "\t" $1 "\t" $3 }'

2)

/usr/sbin/smartctl -a /dev/sdb | grep Device: | awk '{print $2 }'

The 1st comand displays 3 columns with multiple rows and the next command displays one one column of information.

I want to concat the outputs of both the commands and concat and display as 4 columns with multiple rows. Please suggest.

like image 681
arpita Avatar asked Nov 02 '11 12:11

arpita


People also ask

How do I combine two Bash commands?

In Bash, we can use both “{ }” and “( )” operators to group commands.

What do we use to keep output from multiple print commands on the same line?

Putting a comma on the end of the print() line prevents print() from issuing a new line (you should note that there will be an extra space at the end of the output). It works perfectly.


1 Answers

This is what paste is for. Use process substitution to make the shell treat your commands like files:

paste <(mount | awk 'tolower($0) ~ /\/dev\/sd*/ {print NR "\t" $1 "\t" $3}') \
      <(/usr/sbin/smartctl -a /dev/sdb | awk '/Device:/ {print $2}')

I removed the grep commands, which awk can easily do.

like image 57
glenn jackman Avatar answered Oct 24 '22 14:10

glenn jackman