Unix Shell Script只从文件内容中获取所需的行

时间:2013-09-17 00:41:14

标签: bash shell unix sh

修改

我有一个包含字符串行的文件

mediafire.com/?a6o37kfc7m68dri

注意:媒体上的文件实际上是“logs.out”我刚刚将其重命名为logs.txt。

示例内容:(logs.out)

BIF in bif_init> login successful
BIF in dbInit> INFO: DB status:20,successful
BIF in dbInit> INFO: get the fileName need be query DB. Size(0) 
BIF in dbInit> INFO: get the fileName need be query DB. Size(30) 
BIF in modInit> INFO: modInit finished
BIF in setInputTag> set input tag value, name/value:ACCOUNT_NO/4302253 
BIF in setInputTag> set input tag value, name/value:bill_ref_no/199863752 
BIF in setInputTag> set input tag value, name/value:bill_ref_resets/1 
BIF in setInputTag> set input tag value, name/value:DISPUTE_AMOUNT/0 
BIF in setInputTag> set input tag value, name/value:NOD_FLAG/0 
BIF in setInputTag> set input tag value, name/value:OUTSTANDING_BALANCE/12904
BIF in setInputTag> set input tag value, name/value:TOTAL_DUE/12904 
BIF in run> INFO: updateParameterValues finished
BIF in getFile> INFO: Feed file(/arbordata/feedfile/PS-M07-1205-20130807-20130911-201711-1-bif01.DAT) for current invoice
BIF in insertNewFile> m_pcurFileNameAttr->file_seq_no:(1)  
BIF in insertNewFile> INFO:Output fileHeader(000020130911201711
    ) to feedfile,successful 
BIF in setInputTag> set input tag value, name/value:ACCOUNT_NO/4302253 
BIF in setInputTag> set input tag value, name/value:bill_ref_no/199863752 
BIF in setInputTag> set input tag value, name/value:bill_ref_resets/1 
BIF in setInputTag> set input tag value, name/value:DISPUTE_AMOUNT/0 
BIF in setInputTag> set input tag value, name/value:NOD_FLAG/0 
BIF in setInputTag> set input tag value, name/value:OUTSTANDING_BALANCE/12904
BIF in setInputTag> set input tag value, name/value:TOTAL_DUE/12904 
BIF in addInheritedByField> INFO: Succeed to put the inherited value of field(BILLZIPCODE) into ACCT map 
BIF in addInheritedByField> INFO: Succeed to put the inherited value of field(BILLEDACCOUNTNUMBER) into ACCT map 
BIF in addInheritedByField> INFO: Succeed to put the inherited value of field(BILLNUMBER) into ACCT map 
BIF in addInheritedByField> INFO: Succeed to put the inherited value of field(BILLNUMBERRESETS) into ACCT map 
BIF in addInheritedByField> INFO: Succeed to put the inherited value of field(OWNINGACCOUNTNUMBER) into SI inherited_map 
BIF in addInheritedByField> INFO: Succeed to put the inherited value of field(SUBSCRBNUMBER) into SI inherited_map 
BIF in addInheritedByField> INFO: Succeed to put the inherited value of field(SUBSCRBNUMBERRESET) into SI inherited_map 
BIF in addInheritedByField> INFO: Succeed to put the inherited value of field(DEFAULTEXTERNALID) into SI inherited_map 
BIF in InvoiceRecord> INFO: Output data to SI map,successful. SI=2813096,0
BIF in addInheritedByField> INFO: Succeed to put the inherited value of field(OWNINGACCOUNTNUMBER) into SI inherited_map 
BIF in addInheritedByField> INFO: Succeed to put the inherited value of field(SUBSCRBNUMBER) into SI inherited_map 
BIF in addInheritedByField> INFO: Succeed to put the inherited value of field(SUBSCRBNUMBERRESET) into SI inherited_map 
BIF in addInheritedByField> INFO: Succeed to put the inherited value of field(DEFAULTEXTERNALID) into SI inherited_map 
BIF in InvoiceRecord> INFO: Output data to SI map,successful. SI=13415970,0
BIF in addInheritedByField> INFO: Succeed to put the inherited value of field(OWNINGACCOUNTNUMBER) into SI inherited_map 
BIF in addInheritedByField> INFO: Succeed to put the inherited value of field(SUBSCRBNUMBER) into SI inherited_map 
BIF in addInheritedByField> INFO: Succeed to put the inherited value of field(SUBSCRBNUMBERRESET) into SI inherited_map
BIF in setInputTag> set input tag value, name/value:ACCOUNT_NO/2726650 
BIF in setInputTag> set input tag value, name/value:bill_ref_no/190155402 
BIF in setInputTag> set input tag value, name/value:bill_ref_resets/1 
BIF in setInputTag> set input tag value, name/value:DISPUTE_AMOUNT/0 
BIF in setInputTag> set input tag value, name/value:NOD_FLAG/0 
BIF in setInputTag> set input tag value, name/value:OUTSTANDING_BALANCE/164888
BIF in setInputTag> set input tag value, name/value:TOTAL_DUE/164888 
BIF in run> INFO: updateParameterValues finished
BIF in getFile> INFO: Feed file(/arbordata/feedfile/PS-M02-1204-20130602-20130911-201711-1-bif01.DAT) for current invoice
BIF in insertNewFile> m_pcurFileNameAttr->file_seq_no:(1)  
BIF in insertNewFile> INFO:Output fileHeader(000020130911201711
) to feedfile,successful 
BIF in setInputTag> set input tag value, name/value:ACCOUNT_NO/2726650 
BIF in setInputTag> set input tag value, name/value:bill_ref_no/190155402 
BIF in setInputTag> set input tag value, name/value:bill_ref_resets/1 
BIF in setInputTag> set input tag value, name/value:DISPUTE_AMOUNT/0 
BIF in setInputTag> set input tag value, name/value:NOD_FLAG/0 
BIF in setInputTag> set input tag value, name/value:OUTSTANDING_BALANCE/164888
BIF in setInputTag> set input tag value, name/value:TOTAL_DUE/164888 
BIF in addInheritedByField> INFO: Succeed to put the inherited value of field(BILLZIPCODE) into ACCT map 
BIF in addInheritedByField> INFO: Succeed to put the inherited value of field(BILLEDACCOUNTNUMBER) into ACCT map 
BIF in addInheritedByField> INFO: Succeed to put the inherited value of field(BILLNUMBER) into ACCT map 
BIF in addInheritedByField> INFO: Succeed to put the inherited value of field(BILLNUMBERRESETS) into ACCT map 
BIF in addInheritedByField> INFO: Succeed to put the inherited value of field(OWNINGACCOUNTNUMBER) into SI inherited_map 

只有想要拥有的行:

BIF in setInputTag> set input tag value, name/value:ACCOUNT_NO/4302253
BIF in setInputTag> set input tag value, name/value:bill_ref_no/199863752 
BIF in setInputTag> set input tag value, name/value:bill_ref_resets/1

BIF in setInputTag> set input tag value, name/value:ACCOUNT_NO/4302253 
BIF in setInputTag> set input tag value, name/value:bill_ref_no/199863752 
BIF in setInputTag> set input tag value, name/value:bill_ref_resets/1 

BIF in setInputTag> set input tag value, name/value:ACCOUNT_NO/2726650 
BIF in setInputTag> set input tag value, name/value:bill_ref_no/190155402 
BIF in setInputTag> set input tag value, name/value:bill_ref_resets/1 

BIF in setInputTag> set input tag value, name/value:ACCOUNT_NO/2726650 
BIF in setInputTag> set input tag value, name/value:bill_ref_no/190155402 
BIF in setInputTag> set input tag value, name/value:bill_ref_resets/1 

我希望将它输出到一个文件格式为:(horizo​​ntaly和pipe delimiter)

BIF in setInputTag> set input tag value, name/value:ACCOUNT_NO/4302253|BIF in setInputTag> set input tag value, name/value:bill_ref_no/199863752|BIF in setInputTag> set input tag value, name/value:bill_ref_resets/1 
BIF in setInputTag> set input tag value, name/value:ACCOUNT_NO/4302253|BIF in setInputTag> set input tag value, name/value:bill_ref_no/199863752|BIF in setInputTag> set input tag value, name/value:bill_ref_resets/1
BIF in setInputTag> set input tag value, name/value:ACCOUNT_NO/2726650|BIF in setInputTag> set input tag value, name/value:bill_ref_no/190155402|BIF in setInputTag> set input tag value, name/value:bill_ref_resets/1
BIF in setInputTag> set input tag value, name/value:ACCOUNT_NO/2726650|BIF in setInputTag> set input tag value, name/value:bill_ref_no/190155402|BIF in setInputTag> set input tag value, name/value:bill_ref_resets/1 

请帮助我:(     我想创建.sh文件,它将在PuTTy上运行

执行此代码时,它正在运行

sed -n -e '/value:ACCOUNT_NO/{N;N;s/..\n\(.\)/|\1/g;p;}' logs.out> OUPUT_logs.out

但是

sed -n -e '/value:ACCOUNT_NO/{N;N;s/..\n\(.\)/|\1/g;p;}' logs.out> OUPUT_logs.txt

每条记录之间都是“BLANK LINE”。

3 个答案:

答案 0 :(得分:0)

创建一个名为summarise_accounts.sh

的shell脚本
#!/bin/bash
exec paste -d'|' <(grep value:ACCOUNT_NO "$1") <(grep value:bill_ref_no "$1") <(grep value:bill_ref_resets "$1")

使其可执行(chmod +x summarise_accounts.sh),然后像这样运行:

./summarise_accounts.sh logs.txt >account_summary.txt

当然,您可以在命令行上更改文件名。

答案 1 :(得分:0)

sed -n -e '/value:ACCOUNT_NO/{N;N;s/\n/|/g;p;}' logs.txt > outfile.txt

修改

我已修改上面的命令将输出重定向到文件,但更正确的是我们更正了输出。我已经使用您给出的示例logs.txt测试了该命令;我不太清楚你的系统说它为什么不能在那里工作,所以让我们试验一下。试试这个:

sed -n -e '/value:ACCOUNT_NO/p' logs.txt

输出是什么?

修改

到目前为止,这么好。现在试试这个:

sed -n -e '/value:ACCOUNT_NO/{p;}' logs.txt

相同的输出?

修改

好!现在:

sed -n -e '/value:ACCOUNT_NO/{N;N;p;}' logs.txt

输出是什么?

修改

最后一步,我们重定向到outputfile.txt并删除换行符。正如@ ChrisJester-Young所说,我们可以在同一步骤中删除尾随空格:

sed -n -e '/value:ACCOUNT_NO/{N;N;s/ \n/|/;p;}' logs.txt > outputfile.txt

修改

试试这个,我们称之为代码6

sed -n -e '/value:ACCOUNT_NO/{N;N;s/..\n/|/g;p;}' logs.txt > outputfile.txt

修改

问题是你的logs.txt有DOS风格的行结尾。我认为Code 6将它们改为UNIX风格,而ultra edit警告你它们很奇怪。试试这个(代码7 ):

sed -n -e '/value:ACCOUNT_NO/{N;N;s/..\n\(.\)/|\1/g;p;}'

答案 2 :(得分:0)

您正在PuTTY上运行它。我想这是你拥有的logs.txt的Windows版本。首先在日志文件上运行dos2unix命令,然后执行我写的sh文件:

dos2unix logs.txt

这是a.sha.sh的内容如下:

#!/bin/sh  
awk '/ACCOUNT_NO/,/resets\/1/ {printf $0" "}' logs.txt | awk '{gsub(/resets\/1/,"resets/1\n");print}' | sed -e 's/ BIF/|BIF/g' -e 's/^ |BIF/BIF/g'

在执行前更改文件的权限。

chmod a+x ./a.sh

然后执行文件

./a.sh > new_file.txt