Sed

From wiki
Revision as of 21:25, 13 October 2019 by Hdridder (talk | contribs) (→‎Tricks)
Jump to navigation Jump to search

Check the APO unix pages for more.

Tricks

sed 's/\r//' filename > newfilename
dos2unix substitute, remove all dos line endings.
sed -i 'commands' filename
Edit the file directly (inplace) by default the result is sent to standard output

Basics

sed 's/<old_string>/<new_string>/g' <file>
Substitute, no whitespace is allowed after the command (s), the first character following s is the fieldseperator./g substitutes all matching patterns in a line.
sed 's/pattern(with)sub(patterns)/\1 newpartofstring \2/' <file>
\1 and \2 refer to the subpatterns between brackets ().
sed '/<pattern>/d'
Delete matching line. !d negates the match => delete all lines not matching.