How to use grep to get anything just after `name=`?

前端 未结 5 1493
执念已碎
执念已碎 2020-12-07 18:45

I’m stuck in trying to grep anything just after name=, include only spaces and alphanumeric.

e.g.:

name=some value here
<
5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-07 19:09

    Try this:

    sed -n 's/^name=//p' filename
    

    It tells sed to print nothing (-n) by default, substitute your prefix with nothing, and print if the substitution occurs.

    Bonus: if you really need it to only match entries with only spaces and alphanumerics, you can do that too:

    sed -n 's/^name=\([ 0-9a-zA-Z]*$\)/\1/p' filename
    

    Here we've added a pattern to match spaces and alphanumerics only until the end of the line ($), and if we match we substitute the group in parentheses and print.

提交回复
热议问题