Replace whole line when match found with sed

前端 未结 4 1765
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-07 20:25

I need to replace the whole line with sed if it matches a pattern. For example if the line is \'one two six three four\' and if \'six\' is there, then the whole

4条回答
  •  無奈伤痛
    2020-12-07 21:01

    To replace whole line containing a specified string with the content of that line

    Text file:

    Row: 0 last_time_contacted=0, display_name=Mozart, _id=100, phonebook_bucket_alt=2
    Row: 1 last_time_contacted=0, display_name=Bach, _id=101, phonebook_bucket_alt=2
    

    Single string:

    $ sed 's/.* display_name=\([[:alpha:]]\+\).*/\1/'
    output:
    100
    101
    

    Multiple strings delimited by white-space:

    $ sed 's/.* display_name=\([[:alpha:]]\+\).* _id=\([[:digit:]]\+\).*/\1 \2/'
    output:
    Mozart 100
    Bach 101
    

    Adjust regex to meet your needs

    [:alpha] and [:digit:] are Character Classes and Bracket Expressions

提交回复
热议问题