sed
can be used to edit text/files "in-stream". It is quite handy in a chain of piped commands. The general format of the sed
command is:
$ sed [-n] [-e 'line_range action' ...] < input > output
-n
: Suppress default behavior to output all input lines.-e
: Execute the following line range and action combination. A singlesed
command may have multiple-e
flags.line_range
: specify the lines to take action upon in form of "start_line,endline". For example,3,5
means lines 3 through 5 inclusively. Ifline_range
is blank, it means the action applies to all alines in input.action
: the action to take. The two most common actions are:p
: print the line (usually combined with-n
command line flag)s/regex/new_value/g
: substitute all matchingregex
values with the given new value. See "Common Regex Patterns" section below.
Display lines 2 to 4 from input_users.txt
$ sed -n -e '2,4p' input_users.txt
3:5555:David
5:789:Carol
11:5555:Carol
Substitute all occurrences of "Carol" with "Susan".
$ sed -e 's/Carol/Susan/g' input_users.txt
USER_ID:ACCT_NUM:USER_NAME
3:5555:David
5:789:Susan
11:5555:Susan
1:1234:Frank
7:9876:Alice
4:666:Bob
7:54321:Alice
2:123:Eric
11:1234:Susan
4:5566:Bob
Only do the above substitution on lines 1-3.
$ sed -e '1,3s/Carol/Susan/g' input_users.txt
USER_ID:ACCT_NUM:USER_NAME
3:5555:David
5:789:Susan
11:5555:Carol
1:1234:Frank
7:9876:Alice
4:666:Bob
7:54321:Alice
2:123:Eric
11:1234:Carol
4:5566:Bob
Quote all numbers in file. The pattern [0-9][0-9]*
will match one or more digits and since the pattern is enclosed in \(\)
, it will be substituted in the output by the \1
marker.
$ sed -e 's/\([0-9][0-9]*\)/"\1"/g' input_users.txt
USER_ID:ACCT_NUM:USER_NAME
"3":"5555":David
"5":"789":Carol
"11":"5555":Carol
...
Convert :
field separator to |
.
$ sed -e 's/:/|/g' input_users.txt
USER_ID|ACCT_NUM|USER_NAME
3|5555|David
5|789|Carol
11|5555|Carol
1|1234|Frank
7|9876|Alice
4|666|Bob
7|54321|Alice
2|123|Eric
11|1234|Carol
4|5566|Bob
Add a column to the beginning and end of each line. The ^
and $
pattern match the beginning and end of line respectively.
$ sed -e 's/^/BAR:/' -e 's/$/:FOO/' input_users.txt
BAR:USER_ID:ACCT_NUM:USER_NAME:FOO
BAR:3:5555:David:FOO
BAR:5:789:Carol:FOO
BAR:11:5555:Carol:FOO
BAR:1:1234:Frank:FOO
BAR:7:9876:Alice:FOO
BAR:4:666:Bob:FOO
BAR:7:54321:Alice:FOO
BAR:2:123:Eric:FOO
BAR:11:1234:Carol:FOO
BAR:4:5566:Bob:FOO
Regex | Description | Example |
---|---|---|
`^` | Anchor to beginning of line | `^abc` |
`$` | Anchor to end of line | `xyz$` |
`[...]` | matches set of characters and ranges | `[abc]` : matches 'a' or 'b' or 'c' `[0-9a-z]` : matches all digits and lowercase letters. |
`[^...]` | negates match of characters/ranges | `[^abc]` matches anything other than 'a','b', or 'c' |
`.` | Matches any single character. | `a.c` : matches 'abc', 'aac', or any three characters that start with 'a' and end with 'c' |
`*` | matches 0 or more instances of previous character. | `[0-9]*` match 0 or more digits |
`+` | matches 1 or more instances of previous character. | `[0-9]+` match 1 or more digits |
`{n,m}` | matches n to m instances of previous character. Both n and m are optional and default to 0 and infinite respectively | `[0-9]{3,5}` : match 3 to 5 digits. `[0-9]{,4}` : match 0 to 4 digits. `[0-9]{2,}` : match 2 or more digits. |