How can I delete everything until a pattern and everything after another pattern from a line?
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 blockconsectetuer.*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
If every line contains both start and end pattern then the easiest way to do this is with grep
. Instead of deleting the beginning and ending of each line you can simply output the contents between both patterns. The -o
option in GNU grep
outputs only the matches:
grep -o 'consectetuer.*elit' file
Note: as mentioned, this only works if every line in the file can be parsed this way. Then again, that's 80% of all typical use-cases.