Ubuntu/Commands/sed
Contents |
sed
Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed's ability to filter text in a pipeline which particularly distinguishes it from other types of editors.
Command Syntax
- -e
- adds a script or regular expression pattern
- -i
- edits files in place (makes backup if extension supplied)
- s/regexp/replacment/
- Attempt to match regexp against the pattern space. If successful, replace that portion matched with replacement. The replacement may contain the special character & to refer to that portion of the pattern space which matched, and the special escapes \1 through \9 to refer to the corresponding matching sub-expressions in the regexp.
- /g
- global, meaning that the match will apply to as many matches on the line as available, instead of just the first
Examples
Looks for the pattern "Length" and everything after that (greedy match) and replaces it with a whitespace
sed -i 's/Length.*/ /g' TrillianLogFile_Highlighted2.CSV
Inserts a new line after every line into file test
sed -i -e 's/.*/\ &/' test
Append a new line with specific text after every line
sed '/.*/a\test' test
Add new text to the beginning of each line
sed 's/^/| /g' test
Extra
Finding all email addresses in a text file using Perl. While not related to sed, this is the best section for documentation of it for.
perl -wne'while(/[\w\.]+@[\w\.]+/g){print "$&\n"}' test.txt
The above should find all email addresses within a text file, regardless of where they exist. It does assume there will be a word boundary before and after the email address.