Skip to content
Advertisement

How to append a column for the result set in shell script

I need a script for the below scenario. I am very new to shell script.

wc file1 file2  

the above query results with following result

40     149     947   file1
2294   16638   97724 file2

Now I need to get result as follows: 1st column, 3rd column ,4th column of above result set and new column with default values

40    947   file1  DF.tx1
2294  97724 file2  DF.rb2

Here the last column values is always known values i.e for file1 DF.tx1 and file2 DF.rb2.

If the give filenames in any order the default values should not change.

Please help me to write this script. Thanks in advance!!

Advertisement

Answer

You can use awk:

wc file1 file2 | 
  awk '$4 != "total"{if ($4 ~ /file1/) f="DF.tx1"; else if ($4 ~ /file2/) f="DF.rb2";
         else if ($4 ~ /file3/) f="foo.bar"; print $1, $3, $4, f}'

1 12 file1 DF.tx1
9 105 file2 DF.rb2
5 15 file3 foo.bar
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement