- Published on
sed
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
sed 's/^.*\(consectetuer.*elit\).*$/\1/' text
consectetuer adipiscing elit
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
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 |
find . -type f -exec sed -i -e 's/apple/orange/g' {} \; | Find and replace all text in current directory and subdirectories |