最近,我需要编写一个bash脚本,以检查文件中是否存在一个perticular段落。文件的内容是。
出版1 1EO's 保存完成 贸易储蓄成功为贸易56945458\x\220841_ 出版4 4EO's 保存完成 成功的贸易储蓄,5666688,000,000,MCR: CMDTY,来源:ICE 出版1 1EO's 保存完成 贸易储蓄成功为贸易56945458\x\220841_
需要匹配的段落是。
出版1 1EO's 保存完成 贸易储蓄成功为贸易56945458\x\220841_
将上述段落的内容保存在名为temp的文件中。
我编写了一个简单的脚本来完成这个任务,但它似乎不起作用。
#!/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
任何帮助都将不胜感激。
谢谢。
发布于 2016-11-14 13:56:23
通常情况下,这些是不一样的。grep $result中包含新的行(\n)字符,而$line包含空格。
如果在回送IFS=$之前设置$result“\n”,则可以看到它们之间的差异。
我不得不在$line中插入一些\n (在正确的位置),现在工作得很好:
#!/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
https://stackoverflow.com/questions/40586948
复制相似问题