Sed
Sed
- Sed is a Non-interactive stream-oriented editor.
- It allows you to edit files and save the output. This prevents you from permanently modifying an original file.
- You can keep sed commands in a file called a sed script if you need to perform mlutiple edits or if you do not want to worry about quoting sed commands at the shell command line.
- sed processed a file or input one line at a time and sends its output to the screen. It stores the line it is currently processing in a temporary buffer called pattern space. after sed has finished the line in the pattern space, the line is sent to the screen.
- sed uses addressing to specify which lines are to edited. The addressing can be in the form of numbers or regular expressions, or both. if a line is not specified, sed processes all the lines in the input file.
- When the address is a number, the number represents a line number. A dollar sign can be used to represent the last line of the input file. If the comma separates two line numbers, the addresses that are processed are within that range.
-n Suppresses all information normally written to standard output -f <Script file> Uses the ScriptFile variable as the source of the edit script -i or --in-place sed creates no output. But changes the source file itself.
s for substitution and /g for Global replacement
To replace "MA" with "Massachusetts in first occurance
$ sed 's/MA/Massachusetts/' list
To replace "MA" with "Massachusetts in all occurances
$ sed 's/MA/Massachusetts/g' list
/ as Delimeter
The character after the s is the delimiter. It is conventionally a slash, because this is what ed, more, and vi use. It can be anything you want, however. If you want to change a pathname that contains a slash, you could use the backslash to quote the slash:
sed 's/\/usr\/local\/bin/\/common\/bin/' <old >new
Gulp. It is easier to read if you use an underline instead of a slash as a delimiter:
sed 's_/usr/local/bin_/common/bin_' <old >new
Some people use commas, others use the "|" character. Pick one you like
Using & as the matched string
Sometimes you want to search for a pattern and add some characters, like parenthesis, around or near the pattern you found. It is easy to do this if you are looking for a particular string:
$ sed 's/abc/(abc)/' <old >new
This won't work if you don't know exactly what you will find. How can you put the string you found in the replacement string if you don't know what it is?
The solution requires the special character "&." It corresponds to the pattern found.
$ sed 's/[a-z]*/(&)/' <old >new
You can have any number of "&" in the replacement string. You could also double a pattern, e.g. the first number of a line:
$ echo "123 abc" | sed 's/[0-9]*/& & &/' 123 123 123 abc
sed -n noprinting and /p print
By default, sed prints every line. If it makes a substitution, the new text is printed instead of the old one. The "-n" option will not print anything unless a explicit request to print is found. When the "-n" option is used, the "p" flag will cause the modified line to be printed. Here is one way to duplicate the function of grep with sed:
$ sed -n 's/pattern/&/p' <file
Multiple commands with -e command
One method of combining multiple commands is to use a -e before each command:
$ sed -e 's/a/A/' -e 's/b/B/' <old >new
sed -f scriptname
If you have a large number of sed commands, you can put then into a file and use
$ sed -f sedscript <old >new
sed in shell script
If you have many commands and they won't fit neatly on one line, you can break up the line using a backslash:
sed -e 's/a/A/g' -e 's/e/E/g' -e 's/u/U/g' <old >new
A quote can cover several lines too:
#!/bin/sh sed ' s/a/A/g s/o/O/g s/u/U/g' <old >new
Exmaples:
Append a string "hello" to starting of each line
sed 's/^/hello /g' filesname > filename.out
Printing with p
If sed wasn't started with an "-n" option, the "p" command will duplicate the input
To print the first 10 lines of a file:
# sed -n '1,10 p' <file
To print the 10th line alone
# sed -n 10p <file
To print the lins between 10th and and the last line
# sed -n -e 's,$p' file_name
Deleting lines
$ sed '/The Netherlands/d` $ sed '/The Netherlands/!d` # deletes lines that don't contain The Netherlands
sed options:
c\ Replaces text in the current line with new text
a\ Appends on or more lines of text to the current line
d Deletes line
i\ Inserts text above the current line
s Subtitues on string for another
! Applies the command to all lines except the selected ones
h (H) Copies or appends the contests of the pattern space to the holding buffer
g (G) Retrives what is the holding buffer and copies it into a pattern buffer,
appending or overwriting what was there