gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 10:29:26 24 4
gpt4 key购买 nike

我最近要编写一个 bash 脚本来检查文件中是否存在特定段落。文件内容为。

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)

Published 4EO's
Save completed
Trade saving save successful for trade 5666688|000|b for MCR: CMDTY from source:ICE

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)

需要匹配的段落是。

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的文件中。

我写了一个简单的脚本来完成这个任务,但它似乎无法正常工作。

#!/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 包含空格。

如果在 echo $result 之前设置 IFS=$"\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

关于linux - 用于检查文件中是否存在段落/行流的 Shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40586948/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com