外壳输出对齐

时间:2017-09-25 09:26:04

标签: shell awk sed

如何对齐此脚本输出。

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 - 只是$ instance的第二列 - xip.pentaho-server_web.partition_0.0.1是$ instance的第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

1 个答案:

答案 0 :(得分: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-script的解释:

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);
 }'