在shell脚本,以及如何错误写入到文件 [英] Error in shell script and how to write to a file

查看:458
本文介绍了在shell脚本,以及如何错误写入到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个从指令提取数据的shell脚本:

我试图运行在vi和vim编辑器这两个脚本。但是,一切都白费了。

请帮助我。怎么写,它的输出在文件中。

可以注意到,这仅仅是一个起点,使该脚本将产生多个文件,以便
我不能写:

  SCRIPT_NAME>文件名


解决方案

我认为这个问题是好的,现在,输入文件是编辑好后,就够了,我完全可以理解你问什么了。

使用awk的,你需要学会使用2-D阵列,它将简化code。

 的awk'BEGIN {打印实例ID名称所有者Cost.centre}
/ TAG / {分裂($ 0时,一个,FS);一个[4] = tolower的(一个[4]); $ 1 = $ 2 = $ 3 = $ 4 =;苯并[a [3],A [4] = $ 0; C [A [3]}
END {了(我在C)的printf%-18s%-26s%-14s%-20s \\ n,I,B [我,名],B [我,所有者],B [我, cost.center]}'文件实例ID名称所有者Cost.centre
I-e1cfc499的Memcached
I-7f4b9300 Test_LB01_Sachin
I-c4260db8 Rishi_Win_SAML仙人潘迪
I-fb5ca283 CLIQR,请勿触摸mataa 1234

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

解决方案

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    

这篇关于在shell脚本,以及如何错误写入到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆