sed commands

Example: To replace multiple blank spaces with a single space, using the output of ip route show:

ip route show | sed 's/ */ /g'

Expected Outcome: While I can’t provide the exact output without knowing the content of ip route show, the result would be the same routing table but with consistent single spacing between words and columns.

6. Replacing words or characters inside a range

Example: To replace the word “verified” with “checked” only within lines 8 through 12 in testfile.txt:

sed '8,12s/verified/checked/g' testfile.txt

Expected Outcome:

$ cat testfile.txt
...
Line 8: This is a verified statement.
Line 9: Another verified line.
...

$ sed '8,12s/verified/checked/g' testfile.txt
...
Line 8: This is a checked statement.
Line 9: Another checked line.
...

For a single line, say line 9:

sed '9s/verified/checked/g' testfile.txt

7. Using regular expressions (advanced substitution) – I

Example: To remove empty lines or those beginning with “#”:

sed '/^$/d; /^#/d' testfile.txt

Expected Outcome: Given that the content of SomeConfigFile.conf isn’t provided, the result would be the content of the file without any lines that are empty or start with a “#”.

$ cat testfile.txt
# This is a comment line

This is a text line.

$ sed '/^$/d; /^#/d' testfile.txt
This is a text line.

8. Using regular expressions (advanced substitution) – II

Example: To replace the word ending in “Sh” or “Txt” with “sh” in testfile.txt:

sed 's/\(Sh\|Txt\)$/sh/g' testfile.txt

Expected Outcome:

$ cat testfile.txt
This is a line with Txt
Another line with Sh

$ sed 's/\(Sh\|Txt\)$/sh/g' testfile.txt
This is a line with sh
Another line with sh

9. Using regular expressions (advanced substitution) – III

sed 's/\(TXT\|[Tt][Xx][Tt]\)$/sh/g' testfile.txt

Expected Outcome:

$ cat testfile.txt
This is a line with TXT
Another line with txt

$ sed 's/\(TXT\|[Tt][Xx][Tt]\)$/sh/g' testfile.txt
This is a line with sh
Another line with sh

Description: Initially, the sed command stands out as a potent tool for text manipulation. Furthermore, its effectiveness skyrockets when paired with regular expressions. In the example provided, we first turned to a regex pattern to pinpoint file extensions resembling “TXT”, irrespective of their case. This approach is especially beneficial when confronting files with erratic capitalization or diverse naming standards. Consequently, by harnessing such a command, administrators not only streamline their tasks but also guarantee uniformity in file extensions. As a result, files become more straightforward to recognize and handle.

For those interested in diving deeper into text manipulation using regular expressions, I highly recommend checking out our comprehensive guide: Mastering Text Manipulation Using AWK in Linux. This article provides a thorough walkthrough on filtering and processing texts or strings in files using AWK, another powerful text processing tool in Linux.

10. Viewing lines containing with a given pattern

Description: So, the sed command helps to pick out certain lines from a file that match a set pattern. In system administration, this is super handy. Think about big log files; it’s like finding a needle in a haystack. But with sed, if an admin wants to see logs from a specific day, they can easily do that. It’s like having a shortcut to find exactly what you need.

sed -n '/pattern/p' testfile.txt

Expected Outcome:

$ cat testfile.txt
This is a line.
This is another line with a pattern.

$ sed -n '/pattern/p' testfile.txt
This is another line with a pattern.

This mock output shows three log entries from the /var/log/secure file, all from “Aug 15”, detailing SSH login attempts to the server.

11. Inserting spaces in files

Description: The sed command can be used to insert blank lines in a file. This can be particularly useful for improving the readability of a file by adding spaces between sections or entries.

sed 'G' testfile.txt

Expected Outcome:

$ cat testfile.txt
Line 1
Line 2

$ sed 'G' testfile.txt
Line 1

Line 2

How to undo inserted blank lines:

Description: If you’ve added extra blank lines to a file using sed and wish to revert the file to its original state, you can use another sed command to remove those blank lines.

sed '/^$/d' testfile.txt

Insert double lines of spaces (blank lines) for each non-empty line in a file:

Description: To further enhance readability, you might want to insert two blank lines between each entry or section in a file.

sed 'G;G' testfile.txt

12. Adding spaces selectively

Description: Sometimes, you might want to insert blank lines only at specific places in a file, rather than between every line.

sed '3G;7,8G' testfile.txt

Expected Outcome:

$ cat testfile.txt
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8

$ sed '3G;7,8G' testfile.txt
Line 1
Line 2
Line 3

Line 4
Line 5
Line 6
Line 7

Line 8

Note: The above command inserts a blank line after the third line of the file.

How to undo inserted blank lines:

Description: This command will remove any consecutive blank lines in the file named “testfile.txt”, leaving only one blank line between content.

sed '/^$/N;/^\n$/D' somefile.txt

Expected Outcome:

Line1 Line2 Line3 Line4 Line5 ...

To insert one blank line between line 3 and 4, and double lines between lines 7 and 8, of a plain text file in testfile.txt, do:

Description: This command will insert a blank line between lines 3 and 4, and two blank lines between lines 7 and 8 in the file named “testfile.txt”.

sed -e '3a\\' -e '7a\\' -e '7a\\' somefile.txt

Expected Outcome:

Line1 Line2 Line3 Line4 Line5 Line6 Line7 Line8 ...

13. Switching words

Description: This command switches the order of first names and last names in the file “testfile.txt”, which contains full names in the format “First name Last name”.

sed 's/\(.*\) \(.*\)/\2 \1/' testfile.txt

Expected Outcome:

$ cat testfile.txt
First Last

$ sed 's/\(.*\) \(.*\)/\2 \1/' testfile.txt
Last First

14. Switching timestamp format

Description: This command switches the timestamp format from US (Month Day, Year) to European (Day Month Year) in the file “testfile.txt”.

sed 's/\([0-9]\+\) \([a-zA-Z]\+\), \([0-9]\+\)/\1 \2 \3/' testfile.txt

Expected Outcome:

$ cat testfile.txt
January 1, 2022

$ sed 's/\([0-9]\+\) \([a-zA-Z]\+\), \([0-9]\+\)/\1 \2 \3/' testfile.txt
1 January 2022

15. Replacing words in entire text

Description: This command replaces all instances of the name “Maximilian” with “Constantine” in the file “testfile.txt”.

sed 's/Maximilian/Constantine/g' testfile.txt

Expected Outcome:

$ cat testfile.txt
Hello Maximilian

$ sed 's/Maximilian/Constantine/g' testfile.txt
Hello Constantine

16. Replacing words only if a separate match is found

Description: This command is used to replace the word “unverified” with “verified” in the file “testfile.txt” but only on lines where the word “maxTextMarch1.Txt” is also found.

sed '/maxTextMarch1.Txt/s/unverified/verified/' testfile.txt

Expected Outcome:

$ cat testfile.txt
This is unverified text.
maxTextMarch1.Txt
This is also unverified text.

$ sed ‘/maxTextMarch1.Txt/s/unverified/verified/’ testfile.txt
This is unverified text.
maxTextMarch1.Txt
This is also verified text.

17. Combined replacements of words selectively, only if a separate match is found

Description: This command is used to replace the word “unverified” with “verified” and “Maximilian” with “Constantine” in the file “testfile.txt” but only on lines where the word “maxTextMarch1.Txt” is also found.

Sed Command:

sed '/maxTextMarch1.Txt/s/\(unverified\|Maximilian\)/verified\|Constantine/' testfile.txt

Expected Outcome:

$ cat testfile.txt
This is unverified text.
maxTextMarch1.Txt
This is also Maximilian text.

$ sed '/maxTextMarch1.Txt/s/\(unverified\|Maximilian\)/verified\|Constantine/' testfile.txt
This is unverified text.
maxTextMarch1.Txt
This is also verified|Constantine text.

18. Using sed commands with other Linux tools for filtering

Description: This command combines the nmcli tool with sed to filter and display only the public IP addresses available on the server.

Leave a Reply

Your email address will not be published. Required fields are marked *