【问题标题】:Finding Last occurrence out of multiple occurrences of a regexp using SED Scripting使用 SED 脚本从多次出现的正则表达式中查找最后一次出现
【发布时间】:2012-07-27 09:54:08
【问题描述】:

我有一个 verilog 文件,其中定义了多个包含各种输入和输出变量的模块。 我需要使用 sed 脚本找出此类变量(输入/输出)的最后一次出现。 我运行以下命令

地址=sed -n '100,200{/output/=};100,200{/input/=}' file.txt
它给我的输出为 102 103 104 105 106 而我只想要106。 请给我一些建议。

【问题讨论】:

    标签: sed awk


    【解决方案1】:

    这可能对你有用:

    sed '100,200{/input\|output/=};d' file.txt | sed '$!d'
    

    或者如你所愿:

    address=$(sed '100,200{/input\|output/=};d' file.txt | sed '$!d')
    

    【讨论】:

      【解决方案2】:
      sed -n '100,200p' foo.txt | awk '/input/{s=NR} /output/{s=NR} END{print s}'
      

      【讨论】:

      • 它没有给我行号,但在最后一次 input/output.plz 帮助之后出现了模式!!
      【解决方案3】:

      您不必使用 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
      

      【讨论】:

      • 有一个变量 $adress 具有所有这些值 101 102 103 .. 请告诉我找出这个变量中最后一个值的方法。
      • @user1468315 检查更新的答案,不确定是否是您想要的。
      • 还有一个问题,我想把这个值保存到另一个变量中。
      • @user1468315 再次检查答案
      【解决方案4】:

      你可以这样做:

      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 可能可用!)
      猜你喜欢
      • 1970-01-01
      • 2012-01-12
      • 2016-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多