Anyone who uses a Linux distribution and an operating system in general Unix-like can take advantage of the services of sed, a powerful tool for manipulating text that is particularly useful in multiple contexts. The command it can be used to replace text, delete lines and perform other operations automatically. If you are looking for a specific string of characters in a file, even a large one, and you want to replace it with something else, if that is the tool to use. Although his skills certainly don’t stop there.
What is sed and how does it work
The appellation sed is short for stream editor: This is a utility at the command line which proves to be very useful on many occasions. We’ve already told you about the difficulty many text editors have with large files, right? The sed command acts like a sort of director: the lines of text are the actors, the instructions you provide the script.
The specialty of sed lies, as we have already highlighted, in replacement of words and therefore in the transformation of strings. From simply replacing a string throughout the text to eliminating blank lines, se fits any role. Speak a language indeed, a mixture of imperative commands and regular expressions.
The sed command can also “time travel,” allowing the creation of backup copies and facilitating recovery, so as to enable the user to restore the contents of any file in the event of problems or errors.
Because sed is also useful in Windows
Unfortunately, Windows operating systems do not integrate a command as powerful as sed. There is
findstr but it limits itself to examining a single line while PowerShell offers the cmdlet
Get-Content and the expression
-replace but in both cases it is necessary to be ingenious with the use of specific batches and scripts.
By activating WSL (Windows Subsystem for Linux) on Windows 10 and Windows 11, it is possible to use Linux in Windows benefiting from perfect integration with the Microsoft operating system.
For example, by installing Ubuntu in WSL (or any other Linux distribution), you have the possibility to access the sed command even from Windows. Pressing
\\WSL$ then taking you to the directory
home of your Linux user account, you can copy text files to be processed from the WSL window with the sed command.
Examples of using the sed command
The best way to learn how a command works is to start with concrete examples. The following statement replaces all occurrences of
sed -i 's/stringaA/stringaB/g' file.txt
In the place of
stringaB: obviously the words to be replaced in the file must be indicated
file.txt. The option
-i indicates to sed of edit the text file directly, without creating a backup file. The interventions are then made on the content of the called object
file.txt and stored in the folder you are in.
Suppose we take the text of The Betrothed and save it as a text file: it is a file that weighs approximately 1.2 MB and contains just under 3,000 lines. By typing the following, sed creates a new file (note the absence of the option
-i) with the name Renzo replaced by Mario:
sed 's/Renzo/Mario/g' promessi-sposi.txt > promessi-sposi-modificato.txt
In the text file
promessi-sposi-modificato.txt, Renzo Tramaglino became Mario Tramaglino. The good Manzoni will excuse us.
In case you want to make the appropriate substitutions without considering the difference between uppercase and lowercase lettersjust use the syntax
gi in the command:
sed -i 's/stringadatrovare/stringadasostituire/gi' file.txt
g: Stands for “global” and indicates to carry out the replacement in a way global within each line.
i: Stands for “case-insensitive” (not case sensitive). This means that the replacement will be case insensitive.
How to replace text that is on specific lines
In the previous examples, we saw how to make automatic replacements on the entire contents of a text file. The following command ensures that sed only takes into consideration the contents of lines from number 50 to number 150: the range can obviously be customized to your liking.
sed -i '50,150s/stringadatrovare/stringadasostituire/g' file.txt
Insert text after a certain line
The sed command is like a “Swiss army knife”: it adapts to every need related to file management of text. Without having to open any editor, you can ask sed to add text after a certain line. To do this, just type the following:
sed -i '50i\3rd line' file.txt
The education part
50i\ indicates that you want it import into text file what is specified after the slash, immediately above line number 50.
Extract the contents of a few lines of the text file
We often have to deal with text files big dimensions. It can be complex to extract the contents of specific lines of text or even locate only the lines of text that contain specific strings.
If we return to the text of the Betrothed, the following allows us to extract the content of only lines 1000-1010:
sed -n '1000,1010p' promessi-sposi.txt
The following command, however, allows you to extract and display only the lines that contain the strings Renzo o Lucia:
sed -n '/Renzo\|Lucia/p' promessi-sposi.txt
What is represented in quotes is a regular expression that matches all the lines that contain the string “Renzo” or “Lucia”. The operator
\| it works like a logical operator OR.
Delete all lines that contain a string
sed '100,110d' file.text > nuovo-file.txt allows you to delete lines from 100 to 110 by creating a new text file while the following command allows you to delete all the lines that contain the indicated character string:
sed -i '/stringa/d' file.txt
It is always good to pay maximum attention when using the option
-i which, as explained, applies the changes directly to the original text file, without creating backup copies.
Per delete blank lines from a text file, however, you can use the following instruction:
sed '/^$/d' file.txt
How to add new information to the end of a file
When working with text files, it can be useful to add new lines without opening an “ad hoc” editor. This is especially useful with i configuration file large.
The simple command
sed -i '$a\nuova riga da aggiungere' file.txt allows you to add a new line to the end of the file while for example
sed '15a\nuova riga da aggiungere' file.txt add after the 15th line.