Error in shell script and how to write to a file [

2019-09-22 08:43发布

问题:

I am writing a shell script that is extracting the data from a command:

I have tried running the script in both the vi and vim editor. But everything in vain.

Please help me out. And how write the output of this in a file.

It may be noted that this is just a starting point so the script will produce multiple files so I cannot write:

Script_name > filename

回答1:

I think this question is fine now, the input file is good enough after edit, I can fully understand what you ask for now.

With awk, you need learn to use 2-d array, it will simplify the code.

awk 'BEGIN{print "Instance id           Name                      Owner         Cost.centre"}
/TAG/{split($0,a,FS);a[4]=tolower(a[4]);$1=$2=$3=$4="";b[a[3],a[4]]=$0;c[a[3]]}
END{for (i in c) printf "%-18s%-26s%-14s%-20s\n",i,b[i,"name"],b[i,"owner"],b[i,"cost.center"]}' file

Instance id           Name                      Owner         Cost.centre
i-e1cfc499            Memcached                                               
i-7f4b9300            Test_LB01_Sachin                                        
i-c4260db8            Rishi_Win_SAML            Rishi Pandey                    
i-fb5ca283            CLIQR-DO NOT TOUCH        mataa         1234