The Sed Command
The Sed Command
Though a man page will help you in this, some common options in sed are listed
below
sed -e filename - Executes an inline sed-script on the input file (although it can
be used to execute multiple line sed commands by using \ separator, the wise way
is to use a sed script file for that).
sed -f filename - Execute the series of sed commands in sed-script-file on the
input file
sed -n filename - Doesn't print the result of the sed command to the console by
default, instead one should use the "/p" flag inside the sed command/script to print
the desired output.
sed command can be used to search and manipulate input text in many ways,
though mainly used for substitution, it can also do numerous other pattern
matching tasks like searching and printing lines which matches a pattern, removing
lines which matches a pattern, deleting specific lines in the input file, etc, some
common uses of sed command were shown below.
For the sake of simplicity and to make life easy, we won't be using any of the sed
command line options described above, starting from simple ones, here we go.
The text file used to illustrate the below sed commands is shown below.
To delete the lines 2 through 4 in the above file, use the following sed command
the 'd' flag is used to delete lines, though the single quote is not mandatory in the
above commands (you can simply use sed 1d textfile.txt to delete the first line in
the input file), its advisable to use it as a good practice and will be useful when you
use the -e option at times to execute a series of sed commands inline.
In the above text file, if you need to replace all occurrences of the pattern "line"
with the text "line number", the following sed command can be used (the 's' flag
in the sed command is used to substitute text which matches a pattern).
This is a simple application of the /s flag where the target string is none.
For example, to remove all occurrences of word "line" in the above file, the sed
command would be
Note that the word to be replaced upon matching the pattern is none in the above
command ('/s/line//')
To remove all occurrences of the character space in the above input file, the sed
command would be
Also note that if you don't use single quotes in the above sed command, then the
space pattern should be enclosed in quotes for the command to work.
To delete lines which matches a pattern, use the /d flag along with the pattern to be
matched, for example, to delete lines matching the pattern "line 1" in the above
file, the command would be
Now using the above commands, one can also delete the first and last line in a file
which matches a pattern.
5. Deleting the first line which matches a pattern using sed command
In the above command, '1d' is used in braces to indicate that its a command (to
delete the specific line, which matches a pattern), this is the way to separate sed
flags in command/scripts.
6. Deleting the last line which matches a pattern using sed command
Hope you got to know some common sed command functionality, the next time if
you think about sed instead of grep/sed pipe for text processing, you have improved
a level in UNIX shell scripting.