【问题标题】:how to match one word or another, within a single line, with sed如何使用 sed 在一行中匹配一个单词或另一个单词
【发布时间】:2013-11-23 13:21:19
【问题描述】:

对于这个命令xwininfo -id 0x8a00004 |grep "Absolute\|Width\|Height"我有这个输出

Absolute upper-left X:  44
Absolute upper-left Y:  53
Width: 999
Height: 698

通过使用单个 sed 命令,我希望它变成这样:

nX=44
nY=53
nWidth=999
nHeight=698

我当前的代码是这样的:

xwininfo -id 0x8a00004 |grep "Absolute\|Width\|Height" |sed -r 's".*([XY]):[[:blank:]]*(.*)"n\1=\2"'

我知道要使用 sed 匹配单词,我需要在表达式上使用 \bWordToMatch\b,但我找不到将它放在我的命令中 ([XY]) 的位置... 我也知道要匹配“一个或另一个”字,我需要使用\|,我也猜不到..

【问题讨论】:

    标签: regex sed word match


    【解决方案1】:

    尝试关注sed

    sed -r 's/.*(X|Y|Width|Height)\s*:\s*([0-9]+)/n\1=\2/'
    

    【讨论】:

    • 成功了!我必须打破[] 的思维障碍; | 也受到 () 的限制(我认为这不起作用..)thx!
    • GNU sed 中,括号和竖线need to be escaped 像这样:\( \| \)
    • 此答案使用-r 标志,相当于GNU --regexp-extended。不用-r逃跑,但答案应该是这样的。
    猜你喜欢
    • 2022-01-23
    • 2014-04-20
    • 1970-01-01
    • 1970-01-01
    • 2015-04-03
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多