Home > OS >  How to find specific lines in a file and then replace a word from those lines keeping everything els
How to find specific lines in a file and then replace a word from those lines keeping everything els

Time:02-25

I have a random file with a bunch of lines. Snippet:

lorium0{([y])} Contrary to popular belief, Lorem Ipsum is not simply random text cold
lorium0{([y])} Contrary to popular belief, Lorem Ipsum is not simply random text hot
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout
There are many variations of passages of Lorem Ipsum available
lorium0{([y])} Contrary to popular belief, Lorem Ipsum is not simply random text tea
There are many variations of passages of Lorem Ipsum available
lorium0{([y])} Contrary to popular belief, Lorem Ipsum is not simply random text bee

etc

I want to match every line starting with lorium0{([y])} and then replace simply with very using the vi or vim editor (Preferably with vi). Would like to accomplish this with a 1 line command.

  1. Find Regex match to filter out unwanted lines
  2. Find word simply and replace with very

ie.:

lorium0{([y])} Contrary to popular belief, Lorem Ipsum is not simply random text bee
Becomes:
lorium0{([y])} Contrary to popular belief, Lorem Ipsum is not very random text bee

Trying:

:1,$ s\(lorium0[^ ].*\)\1\(simply\)\2\1: very\2g 

But having difficulty formatting the command and understanding capture groups in substitution.

Thanks.

CodePudding user response:

You can use

:%s/^lorium0{(\[y])}.*\zs\<simply\>/very/g

Details:

  • ^ - start of string/line
  • lorium0{(\[y])} - a literal lorium0{([y])} text
  • .* - any zero or more chars other than line break chars as many as possible
  • \zs - the text before is just context, the match starts at this point
  • \<simply\> - a whole word simply (\< and \> are word boundaries).

Or, you may use a group (in the regex) with a backreference (in the RHS):

%s/^\(lorium0{(\[y])}.*\)\<simply\>/\1very/g
    ^^                 ^^           ^^

CodePudding user response:

Use :g with a pattern to restrict the lines on which the replacement is made. For example:

:g/^lorium0{(\[y\])}/s/simply/very
  • Related