In this tutorial:
Sed (short for ‘stream’ ‘editor’ is a special editor that is used for modifying files automatically. It is not an interactive editor, so it can be used if you are wanting to write a program to make changes in a file. Follow along below as we go through several examples of the sed command.
Command Structure
Command: sed
Synopsis: sed [OPTION]… {script-only-if-there-is-no-other-script} [input-file]…
List of Options:
Short Name | Long name | Description |
---|---|---|
-n | –quiet –silent | This is quiet mode, where it does not print out anything unless a specific print request exists. |
-e script | –expression=script | Add the script to the commands to be executed. |
-f script-file | –file=script-file | This adds the contents of the ‘script-file’ to the commands to be executed |
–follow-symlinks | Follows symlinks when processing. The hard links will still be broken. | |
-i[SUFFIX] | –in-place[=SUFFIX] | This will edit the files in place and make backups if an extension is supplied. |
-c | –copy | This is used when shuffling files using the -i option. It avoids breaking links, both hard and symlinks. –follow-symlinks is usually a better option, however, as it is both more secure and faster. |
-l N | –line-length=N | Use this command to specify thelength of a line, N, before it wraps. |
–posix | THis disables al the GNU extensions. | |
-r | –regexp-extended | Command to use extended regular expressions in the script. |
-s | –separate | Consider each file separately as opposed to one long stream. |
-u | –unbuffered | Load minimal amounts of data form the input files and flus the output buffers more often. |
–version | Prints out the version information |
List of Commands
Structure | Description |
---|---|
= | Used to print line numbers where the supplied parameter is found. |
a \text | Append text. This command inserts a new line and any specified text AFTER the specified parameter. |
c \text | This command is used to find the line the specified parameter is on and then change it to the text provided. |
i \text | Insert text. This command inserts a new line and any specified text BEFORE the specified parameter. |
s /regexp/replacement | This looks through the file looking to match the regexp parameter. Once found, it replaces it with the replacement parameter. |
Examples
Using the commands and options above, you can create many different combinations to tailor sed to filter your files. Below are several examples using the command and options.
Substitution with sed s
This is one of the most often used options. This example below uses sed to look through a file named old.txt and substitute the word ‘day’ for ‘night’, saving the result to a file named ‘new.txt.’
# sed 's/day/night/' old.txt > new.txt
When we cat the original ‘old.txt’ file, you can see it has two words.
# cat old.txt day night
After running our command, when we cat the ‘new.txt’ file, you can see the word ‘day’ has been replaced with ‘night’.
# cat new.txt night night
Changing the delimiter for the s command
When using the s command in the example above, we preceded the parameters with a forward slash /. This works fine in many cases, but if you are wanting to use filepaths in your parameters, you may want to choose a different delimiter. To do this, simply precede the first parameter with your chosen parameter. For example, below we want to change the apth /home/userna5/public_html to /home/userna5/public_html/testfolder.
You could still use the slash delimiter, but you would need to escape all the slashes in your path, so it would end up looking like this:
# sed '/\/home\/userna5\/public_html/\/home\/userna5\/public_html\/testfolder/' old.txt > new.txt
This is quite ugly and can be confusing when trying to read it.
Now, by using a different delimiter, it is much easier to read. Below we display delimiters using a colon : and a pipe |. These are also commonly used delimiters.
# sed ':/home/userna5/public_html:/home/userna5/public_html/testfolder:' old.txt > new.txt
# sed '|/home/userna5/public_html|/home/userna5/public_html/testfolder|' old.txt > new.txt
Adding a line with the a command
If you want to insert some information on a new line, you can do so with the ‘a‘ command. Here we want to add a new line with the sentence ‘This is new’ after the word ‘day’ from the ‘old.txt’ file and save it as ‘new.txt.’
# sed '/day/ a\This is new' old.txt > new.txt
When we nano the ‘new.txt’ file, we see the newly added content was inserted correctly.
# cat new.txt day This is new night
Changing a line with the c command
Rather than inserting a line, you can also change an existing line using the c command. In the example below, we use sed to change a line with the number 2 in it to say ‘ne new new!’. You do need to be careful because as you notice, it also changed the lines numbered 12 and 20 because they also had a 2 in them. Sed can be pretty greedy and make changes whenever possible, so be as accurate as you can when specifying what to change.
# sed '/2/ c\new new new!' old.txt > new.txt
# cat new.txt 1 day new new new! 3 4 5 6 7 8 9 10 11 new new new! 13 14 15 16 17 18 19 new new new!
Finding the line number for a parameter using =
You can use sed to find the line number for a given parameter. For example, if I want to search a file named ‘new.txt’ for the word ‘new’, it would return a list of line numbers that contain that word. In the example below, we also use the option -n so that it only prints out the line numbers involved.
# sed -n '/new/ =' new.txt 2 12 20
As you can see, the word new was found on lines 2, 12, and 20.