【发布时间】:2012-07-27 09:54:08
【问题描述】:
我有一个 verilog 文件,其中定义了多个包含各种输入和输出变量的模块。 我需要使用 sed 脚本找出此类变量(输入/输出)的最后一次出现。 我运行以下命令
地址=sed -n '100,200{/output/=};100,200{/input/=}' file.txt
它给我的输出为 102 103 104 105 106
而我只想要106。
请给我一些建议。
【问题讨论】:
我有一个 verilog 文件,其中定义了多个包含各种输入和输出变量的模块。 我需要使用 sed 脚本找出此类变量(输入/输出)的最后一次出现。 我运行以下命令
地址=sed -n '100,200{/output/=};100,200{/input/=}' file.txt
它给我的输出为 102 103 104 105 106
而我只想要106。
请给我一些建议。
【问题讨论】:
这可能对你有用:
sed '100,200{/input\|output/=};d' file.txt | sed '$!d'
或者如你所愿:
address=$(sed '100,200{/input\|output/=};d' file.txt | sed '$!d')
【讨论】:
sed -n '100,200p' foo.txt | awk '/input/{s=NR} /output/{s=NR} END{print s}'
【讨论】:
您不必使用 sed,当然 sed/awk 可以做到。试试这个:
grep -nE "input|output" test.txt|tail -1|cut -f1 -d:
编辑
你想要这个吗?
kent$ echo "102 103 104 105 106"|awk '{print $NF}'
106
再次编辑
kent$ another=$(echo "102 103 104 105 106"|awk '{print $NF}')
kent$ echo $another
106
【讨论】:
你可以这样做:
nl -ba < file.txt | sed -n '100,200{/output\|input/h};$x;$p'
【讨论】:
cat -n file.txt:)
cat -n 未在 IEEE Std 1003.1-2008 下标准化,但 nl -ba 是。 (但cat -n 可能可用!)