2017-09-25 57 views
1

如何對齊此腳本輸出。外殼輸出對齊

for instance in `find /bxp/*/*/*/prod/*/apache_*/httpd/htdocs/ -type f -name status.txt` ; do 
echo "`hostname`: `ls -ltr | ${instance}` : `cat ${instance}`" 
done 

輸出的樣子:

r008abc, /bxp/xip/xip.pentaho-server_pentaho-server-assembly/pentaho.prod.jobengine/prod/xip.pentaho-server_web.partition_0.0.1/apache_5.3.3-2.2. 
26/httpd/htdocs/status.txt, Web server is disabled 

但是我想要的輸出如:

r008abc| xip - xip.pentaho-server_web.partition_0.0.1 | Web server is disabled 

XIP - 不過是$實例的第二列 - xip.pentaho- server_web.partition_0.0.1是$實例的第6列。我怎樣才能做到這一點。我嘗試過awk命令,但沒有幫助。你的建議表示讚賞。

命令我試過

for instance in `find /bxp/*/*/*/prod/*/apache_*/httpd/htdocs/ -type f -name status.txt` ; do 
echo "`hostname`: `"ls -ltr | awk -F '/' '{print $3}"' ${instance}` : `cat ${instance}`" 
done 

回答

0

像這樣oneliner:

find /bxp/*/*/*/prod/*/apache_*/httpd/htdocs/ -type f -name status.txt | awk -F/ -v host=$(hostname) '{cmd="cat \047" $0 "\047"; if ((cmd | getline out) > 0){printf "%s| %s - %s | %s\n", host,$3, $7, out} close(cmd);}' 

AWK-腳本的說明:

awk -F/           # use/as field separator 
    -v host=$(hostname)       # set var host  
'{ 
    cmd="cat \047" $0 "\047"      # define cmd 
    if ((cmd | getline out) > 0)     # read output of cmd 
     printf "%s| %s - %s | %s\n",host,$3,$7,out; # print formatted result 
    close(cmd); 
}' 
+0

這只是Awesome..Thanks。我幾個小時都在爲此而頭痛。 – Satte

+0

將輸出更改爲適當的列。 –

+0

是的,我已經完成了。謝謝 – Satte