所以我有一个文件,其中包含如下所示的块:
menuentry ... {
....
....
}
....
menuentry ... {
....
....
}
我需要查看 bash 脚本中每个菜单项的内容。我对 sed 的经验非常有限,但通过非常详尽的搜索,我能够构建以下内容:
cat $file | sed '/^menuentry.*{/!d;x;s/^/x/;/x\{1\}/!{x;d};x;:a;n;/^}/!ba;q'
我可以用\{1\}
我想获得第 n 个块的任何数字替换。这样可以正常工作,但问题是我需要迭代任意次数:
numEntries=$( egrep -c "^menuentry.*{" $file )
for i in $( seq 1 $numEntries); do
i=$( echo $i | tr -d '\n' ) # A google search indicated sed encounters problems
# when a substituted variable has a trailing return char
# Get the nth entry block of text with the sed statement from before,
# but replace with variable $i
entry=$( cat $file | sed '/^menuentry.*{/!d;x;s/^/x/;/x\{$i\}/!{x;d};x;:a;n;/^}/!ba;q')
# Do some stuff with $entry #
done
我已经尝试过在变量周围和 sed 语句周围使用引号/双引号和大括号的每一种组合,无论我这样做,我都会遇到某种错误。正如我所说,我对 sed 知之甚少,而科学怪人的声明正是我从各种谷歌搜索中设法混杂在一起的,所以任何帮助或解释都将不胜感激!
TIA