用于检查文件中是否存在段落/行流的Shell脚本 [英] Shell script to check if a paragraph/stream of lines exist in a file
问题描述
我最近必须编写一个bash脚本来检查文件中是否存在垂直段落. 该文件的内容是.
I recently got to write a bash script to check if a perticular paragraph exist in a file. Content of the file is.
出版了1EO's
保存完成
节省贸易节省贸易成功 用于MCR的56945458 | 220841 | b:来自来源:ICE Tradecapture API的CMDTY 重试计数:0(从这一行中,我们检查公司名称-CMDTY)
Published 1EO's
Save completed
Trade saving save successful for trade 56945458|220841|b for MCR: CMDTY from source:ICE Tradecapture API retry count: 0 (From this line we check Company Name – CMDTY)
发布了4EO's
保存完成
节省贸易节省贸易成功
5666688 | 000 | b对于MCR:CMDTY,来源:ICE
Published 4EO's
Save completed
Trade saving save successful for trade
5666688|000|b for MCR: CMDTY from source:ICE
出版了1EO's
保存完成
节省贸易节省贸易成功
用于MCR的56945458 | 220841 | b:来自来源:ICE Tradecapture API的CMDTY
重试计数:0(从这一行中,我们检查公司名称-CMDTY)
Published 1EO's
Save completed
Trade saving save successful for trade
56945458|220841|b for MCR: CMDTY from source:ICE Tradecapture API
retry count: 0 (From this line we check Company Name – CMDTY)
需要匹配的段落是
出版了1EO's
保存完成
节省贸易,为贸易成功保存 用于MCR的56945458 | 220841 | b:来自来源:ICE Tradecapture API的CMDTY 重试计数:0(从这一行中,我们检查公司名称-CMDTY)
Published 1EO's
Save completed
Trade saving save successful for trade 56945458|220841|b for MCR: CMDTY from source:ICE Tradecapture API retry count: 0 (From this line we check Company Name – CMDTY)
将以上段落的内容保存在名为temp的文件中.
saved the content of above paragraph in a file named temp.
我编写了一个简单的脚本来完成此任务,但是似乎无法正常工作.
I wrote a simple script to do this task, But It seems to be not working somehow.
#!/bin/bash
result=$(cat temp | grep -A 2 "Published 1EO's")
echo $result
line="Published 1EO's Save completed Trade saving save successful for trade 56945458|220841|b for MCR: CMDTY from source:ICE Tradecapture API retry count: 0 (From this line we check Company Name – CMDTY)"
echo $line | grep "\b$result\b"
if [ "$line" == "$result" ]; then
echo "match"
else
echo "does not match"
fi
任何帮助将不胜感激.
谢谢.
推荐答案
通常,它们并不相同. grep var $ result,其中包含换行(\ n)字符,而$ line包含空格.
Typically , these are not the same. The grep var $result, contains new line (\n) characters inside, while the $line contains spaces.
如果在echo $ result之前设置IFS = $"\ n",您将能够看到它们之间的区别.
If you set IFS=$"\n" before echo $result, you will be able to see the difference between them.
我不得不在$ line上插入\ n(在正确的位置),现在工作正常:
I had to insert some \n to $line (in the correct position) and now works fine:
#!/bin/bash
result=$(cat test.log | grep -A 2 "Published 1EO's")
IFS=$"\n"
echo $result
line=$(echo -e "Published 1EO's\nSave completed\nTrade saving save successful for trade 56945458|220841|b for MCR: CMDTY from source:ICE Tradecapture API retry count: 0 (From this line we check Company Name – CMDTY)")
echo "----------------------------------"
#echo $line | grep "\b$result\b"
echo $line
unset IFS
if [[ $line = $result ]]; then
echo "match"
else
echo "does not match"
fi
结果:
$./bashtest.sh
Published 1EO's
Save completed
Trade savi g save successful for trade 56945458|220841|b for MCR: CMDTY from source:ICE Tradecapture API retry cou t: 0 (From this li e we check Compa y Name – CMDTY)
----------------------------------
Published 1EO's
Save completed
Trade savi g save successful for trade 56945458|220841|b for MCR: CMDTY from source:ICE Tradecapture API retry cou t: 0 (From this li e we check Compa y Name – CMDTY)
match
这篇关于用于检查文件中是否存在段落/行流的Shell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!