sed

sed is a Unix utility that parses and transforms text, using a simple, compact programming language.

Using named blocks to delete text


cat << EOF > text
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut eu metus id lectus vestibulum ultrices. Maecenas rhoncus.
EOF
(out)
sed 's/^.*\(consectetuer.*elit\).*$/\1/' text
(out)
(out) consectetuer adipiscing elit

  • 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

Quick reference

Command Description
sed '/^$/d' Remove blank lines
sed '/^ /d' Remove lines starting with two spaces
sed 's/^textToRemove//g' Remove text
echo "text::to::remove [TECAN,Text to Keep]" | sed "s/text::to::remove \[TECAN,//" | sed "s/\]//" Remove text