Shell-Script – How to Delete Everything Until a Pattern and After Another Pattern from a Line

shell-scripttext processing

In the following file:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut eu metus id lectus vestibulum ultrices. Maecenas rhoncus.

I want to delete everything before consectetuer and everything after elit.

My desired output:

consectetuer adipiscing elit.

How can I do this?

Best Answer

I'd use sed

sed 's/^.*\(consectetuer.*elit\).*$/\1/' file

Decoded the sed s/find/replace/ syntax:

  • s/^.* -- substitute starting at the beginning of the line (^) followed by anything (.*) up to...
  • \( - start a named block
  • consectetuer.*elit\. - match the first word, everything (.*) up to the last word (in this case, including the trailing (escaped)dot) you want to match
  • \) - end the named block
  • match everything else (.*) to the end of the line ($)
  • / - end the substitute find section
  • \1 - replace with the name block between the \( and the \) above
  • / - end the replace
Related Question