【问题标题】:How do I find the string between word A and word B? [closed]如何找到单词 A 和单词 B 之间的字符串? [关闭]
【发布时间】:2021-08-29 16:15:01
【问题描述】:

下午好 BASH。 如何在命令输出中找到单词Address 和单词Parameters 之间的线:

Address 2001:0:284a:364:3869:30e6:4fc4:c8f8 Parameters
---------------------------------------------------------
Interface Luid : Teredo Tunneling Pseudo-Interface
Scope Id : 0.0
Actual lifetime : infinite
Preferred lifetime : infinite
DAD state : Preferred
Address Type : Public
Skip as source : false

Address fe80::3869:30e6:4fc4:c8f8%11 Parameters
---------------------------------------------------------
Interface Luid : Teredo Tunneling Pseudo-Interface
Scope Id : 0.11
Actual lifetime : infinite
Preferred lifetime : infinite
DAD state : Preferred
Address Type : Other
Skip as source : false

【问题讨论】:

标签: windows bash cmd


【解决方案1】:
$ sed -n 's/Address \(.*\) Parameters/\1/p' file
2001:0:284a:364:3869:30e6:4fc4:c8f8
fe80::3869:30e6:4fc4:c8f8%11

【讨论】:

    【解决方案2】:

    您可以分两步完成:

    1- 找到我们有单词“地址”和单词“参数”的行:How to find lines containing a string in linux

    grep 'Address.*Parameters' 
    
    >>> Address 2001:0:284a:364:3869:30e6:4fc4:c8f8 Parameters
    >>> Address fe80::3869:30e6:4fc4:c8f8%11 Parameters
    

    2- 提取两个单词之间的字符串:How to use sed/grep to extract text between two words?

    sed -e 's/Address\(.*\)Parameters/\1/'
    
    >>> 2001:0:284a:364:3869:30e6:4fc4:c8f8 
    >>> fe80::3869:30e6:4fc4:c8f8%11 
    

    3 - 瞧!!!

    【讨论】:

    • 你永远不需要 grep 和 sed。如果它很简单,你可以单独在 sed 中做任何事情,如果不是,那么你最好单独在 awk 中做。
    • 感谢@EdMorton 先生的提醒。我试图通过使用不同的基本案例来采取教学态度。但我本可以改善反射。
    【解决方案3】:

    与当前提交的答案不同,此答案假定 (基于您的其他两个标签 ,您的 标签应该是 @987654324 @。

    它还对用于写入您提交的输出的初始命令进行了最佳猜测,以便它可以一次性完成所有操作。

    @Echo Off
    SetLocal EnableExtensions
    (Set LF=^
    % 0x0A %
    )
    For /F %%G In ('Copy /Z "%~f0" NUL') Do Set "CR=%%G"
    For /F "Delims=" %%G In ('%SystemRoot%\System32\cmd.exe /V/D/C
     "%SystemRoot%\System32\netsh.exe interface ipv6 show addresses interface="Teredo Tunneling Pseudo-Interface" level=verbose | %SystemRoot%\System32\findstr.exe /RC:".*!CR!!LF!----" 2>NUL"
    ') Do For %%H In (%%G) Do Set /P "=%%H" 0<NUL | %SystemRoot%\System32\find.exe ":"
    Pause
    

    预期输出:

    2001:0:284a:364:3869:30e6:4fc4:c8f8
    fe80::3869:30e6:4fc4:c8f8%11
    Press any key to continue . . . 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多