![搜索单个或多个出现的相同字符串并编辑下一行](https://linux22.com/image/151889/%E6%90%9C%E7%B4%A2%E5%8D%95%E4%B8%AA%E6%88%96%E5%A4%9A%E4%B8%AA%E5%87%BA%E7%8E%B0%E7%9A%84%E7%9B%B8%E5%90%8C%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%B9%B6%E7%BC%96%E8%BE%91%E4%B8%8B%E4%B8%80%E8%A1%8C.png)
我的文件有时会出现一次或多次出现同一字符串。我需要在文件中搜索一个或多个匹配项,然后转到下一行并保留该行中的值,但在末尾添加一个值,同时保持文件的其余部分完好无损。这是我所拥有的:
Line: 3, 0, "General Thunderstorm Risk"
37.74, -87.17
36.22, -85.50
34.80, -85.41
34.01, -85.72
33.14, -86.64
32.45, -88.05
31.88, -89.68
31.38, -91.39
Line: 3, 0, "General Thunderstorm Risk"
29.04, -94.27
29.74, -95.99
30.91, -98.31
31.68, -99.14
32.45, -98.94
33.58, -97.73
35.11, -96.96
36.63, -96.57
if grep "General Thunderstorm Risk" d1.txt; then
echo "General Thunderstorm Risk Detected"
POLYGON=`cat d1.txt | awk '/Line: 4, 0, \"General Thunderstorm Risk\"/{getline; print}'`
cat d1.txt | sed -e 's/Line: 4, 0, \"General Thunderstorm Risk\"/Polygon:/g' | sed -e "s/$POLYGON/$POLYGON, 0, 176, 80, 40/g" > day1
_colored.txt
fi
答案1
要将某些内容应用于 中的特殊行sed
,您地址它们,就像/pattern/command
适用command
于包括pattern
.
如果要将命令应用于模式行之后的下一行,请首先使用n
命令前进该行。
然后,要附加到行尾,请使用s/$/whatever you want to append/
($
代表行尾)。
现在您需要将这些命令与 结合起来{}
,因此寻址对它们都有效,并且您和
sed '/General Thunderstorm Risk/{n;s/$/, 42/;}' yourfile.txt > out.txt
添加, 42
到匹配后的行。如果这不是您要查找的内容,请编辑您的问题。