且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用于检查文件中是否存在段落/行流的Shell脚本

更新时间:2023-11-25 17:44:10

通常,它们并不相同. 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