Previously we have written the first 10 GNU sed command examples. But surely, sed is very large, we may find more new commands soon. This article covers another 10 GNU sed commands around replacing (‘s’ command) and deleting (‘d’ command). So, this article is a continuation of our previous GNU sed commands article. If you are just starting with sed, we suggest you to read the previous article first.


Text Examples

 

We need more text patterns so here we provide you four different blocks of text. Save them in the /tmp directory and work with sed there.

text2.txt:

i have gnu gnu gnu
i have unixunix
i don't have BSD


text3.txt:

1 unix
2 bsd
3 gnu
4 windows
5 mac
6 android
7 darwin
8 minix
100 openvms
101 plan9
102 inferno
103 solaris
104 beos
105 os/2
1000 ios
1000000 reactos


text4.txt:

<- out
-> in
without a sign
just the same


text5.txt:

uNiX
UNix
unIx
uniX
uNiX
UNIx
BSD
BSD
BSD
GNU
GNU

 

11. Find & Replace (Specific Line)


Command Example:

  1. sed '3 s/bsd/\U&/g' text2.txt
  2. sed '1 s/gnu/\U&/g' text2.txt
  3. sed '2 s/unix/\U&/g' text2.txt

Output Example:

master@master:/tmp$ sed '3 s/bsd/\U&/g' text2.txt

i have gnu gnu gnu

i have unixunix

i don't have BSD

master@master:/tmp$ sed '1 s/gnu/\U&/g' text2.txt

i have GNU GNU GNU

i have unixunix

i don't have bsd

master@master:/tmp$ sed '2 s/unix/\U&/g' text2.txt

i have gnu gnu gnu

i have UNIXUNIX

i don't have bsd

master@master:/tmp$

Explanation:

See the number in every beginning of substitute command. This number is the indicator for sed to determine the line number to work with. So the example number 1 above, works with third line of the text. Example number 2 works with first line, and example number 3 works with second line of the text. 

12. Find & Replace (Column Number)


Command Examples:

  1. sed ‘s/gnu/\U&/1’ text2.txt

Output Examples:

master@master:/tmp$ sed 's/gnu/\U&/1' text2.txt

i have GNU gnu gnu

i have unixunix

i don't have bsd

master@master:/tmp$ sed 's/gnu/\U&/2' text2.txt

i have gnu GNU gnu

i have unixunix

i don't have bsd

master@master:/tmp$ sed 's/gnu/\U&/3' text2.txt

i have gnu gnu GNU

i have unixunix

i don't have bsd

master@master:/tmp$

Explanation:

See, the ‘g’ flag is being replaced with a number. That number is an indicator for sed to determine the “column” number, or to be more specific, the match “occurrence”. So if there is a string with repeated words like “gnu gnu gnu”, with the /1 flag, sed will only touch the first (the number 1) occurrence and the result would be “GNU gnu gnu” if it is an uppercase substitution. 

13. Find & Replace (Column Number & Specific Line)



Command Examples:


  1. sed ‘1 s/gnu/\U&/1’ text2.txt
  2. sed ‘2 s/unix/\U&/1’ text2.txt

Output Examples:

master@master:/tmp$ sed '1 s/gnu/\U&/1' text2.txt

i have GNU gnu gnu

i have unixunix

i don't have bsd

master@master:/tmp$ sed '2 s/unix/\U&/1' text2.txt

i have gnu gnu gnu

i have UNIXunix

i don't have bsd


Explanation:

This is a combination of specifying line number and column number. So you find the beginning number and the ending number here. It is similar with a “coordinate” in a map. 

14. Find & Replace (Column Number & ‘g’ Flag)


Command Example:

sed ‘1 s/gnu/\U&/g1’ text2.txt

Output Examples:

master@master:/tmp$ sed '1 s/gnu/\U&/g1' text2.txt

i have GNU GNU GNU

i have unixunix

i don't have bsd

master@master:/tmp$ sed '1 s/gnu/\U&/g2' text2.txt

i have gnu GNU GNU

i have unixunix

i don't have bsd

master@master:/tmp$

Explanation:

See, the ‘g’ flag is still there and there is a single number addition after it. It is no longer “merely column number”, but it means “starting column number until the end”. If there is a string with repeated same words “gnu gnu gnu”, using the flag `/g1` will change the first (the number 1) column until the end of columns (match occurences) and the result will be “GNU GNU GNU” if it is an uppercase replacement. Same thing goes with the flag `/g2`, so the result is starting with the second occurrence until the end “gnu GNU GNU”. 

15. Find & Replace (Duplicate Column)


Command Example:

  1. sed 's/\(bsd\)/\1 \1 \1/g' text2.txt
  2. sed 's/\(gnu\)/\1 \1 \1/g' text2.txt


Output Examples:


master@master:/tmp$ sed 's/\(bsd\)/\1 \1 \1/g' text2.txt

i have gnu gnu gnu

i have unixunix

i don't have bsd bsd bsd



master@master:/tmp$ sed 's/\(gnu\)/\1 \1 \1/g' text2.txt

i have gnu gnu gnu gnu gnu gnu gnu gnu gnu

i have unixunix

i don't have bsd
master@master:/tmp$

Explanation:

Here, we see the [REGEX] and the [REPLACEMENT].

  • In the [REGEX], we recognize parenthesis (`()`) surrounding the whole string “bsd”. This pair of parenthesis is important, because it selects the string as a single whole string. And every parenthese should be escaped (translated) with backslash (`\`) so sed will understand a pair of them as a special thing, not as part of the string. By using this regex, we select the string as a whole.
  • In the [REPLACEMENT], we see the special sequence (`\1`). It is a replacement variable for the selected string. Using this, we just need to type this special sequence to represent the selected string. So, the selected string, will be duplicated as many as the number of `\1` special sequences we determine.
  • As the result, `\1 \1 \1` replaced with `gnu gnu gnu` because the selected string is “gnu”. 
     

16. Find & Duplicate (Decorate Column)


Command Examples:

  1. sed 's/\(bsd\)/[\1]/g' text2.txt
  2. sed 's/\(gnu\)/{\1}/g' text2.txt

Output Examples:

master@master:/tmp$ sed 's/\(bsd\)/[\1]/g' text2.txt

i have gnu gnu gnu

i have unixunix

i don't have [bsd]

master@master:/tmp$ sed 's/\(gnu\)/{\1}/g' text2.txt

i have {gnu} {gnu} {gnu}

i have unixunix

i don't have BSD

master@master:/tmp$ sed 's/\(gnu\)/<\1>/g' text2.txt

i have <gnu> <gnu> <gnu>

i have unixunix

i don't have BSD

master@master:/tmp$ sed 's/\(gnu\)/>\1</g' text2.txt

i have >gnu< >gnu< >gnu<

i have unixunix

i don't have BSD

master@master:/tmp$

Explanation:

Another advantage of using `\1` special sequence is you are free to append any other character. So like this example, we can surround every replacement string with characters. 

17. Find & Replace (Delete All Line Numbers)


Command Example:

  1. sed 's/^[0-9]*[0-9].//g' text3.txt

Output Example:

master@master:/tmp$ sed 's/^[0-9]*[0-9].//g' text3.txt

unix

bsd

gnu

windows

mac

android

darwin

minix

openvms

plan9

inferno

solaris

beos

os/2

ios

reactos

master@master:/tmp$

Explanation:

Here, we see the [REGEX] and the [REPLACEMENT]. We just need the correct regex for every line number plus one whitespace character, then delete it by replacing it with none.

  • In the [REGEX], which consists of three parts. The first part represents the beginning of every line. This part can be written in regex `^`. This regex matches every beginning of line. Another regex following this regex will be considered as the regex for beginning of line.
  • Still in the [REGEX], we see this sequence `[0-9]*[0-9].` This second part represents the number, any number starting with 1 (single digit) until infinite (infinite digits). This part can be written in regex [0-9]*[0-9]. This regex will match any number starting with a number (first [0-9]) and ending with a number (second [0-9]) with any digit (`*` = anything).
  • Still in the [REGEX], the last part represents the whitespace. See the text3.txt example, there is only one single whitespace in every line, between the number and the operating system name. This part can be written in regex `.` (a period or a dot). This `.` regex character match any single character, including a single whitespace.
  • In the [REPLACEMENT], there is no character. So it means the selected regex will be deleted.
  • As the result, any line number, single digit or six digits, in the beginning of line (not in the center nor in the end), will be selected and then deleted.
     
Note: my regex is not perfect so you may look for another better regex.

18. Find & Replace (Case Insensitive)



Command Examples:

  1. sed 's/bsd/changed/Ig' text2.txt

Output Examples:

master@master:/tmp$ cat text2.txt

i have gnu gnu gnu

i have unixunix

i don't have BSD

master@master:/tmp$ sed 's/bsd/changed/Ig' text2.txt

i have gnu gnu gnu

i have unixunix

i don't have changed

master@master:/tmp$

Explanation:

It is basically the same with any substitution for command except the `I` flag. This `I` flag indicates case insensitive search. It is just the same with using any internet search engine, you just type “opensuse” to find “OpenSUSE” (see the uppercases and lowercases). You don’t need to type correctly towards the uppercases and lowercases. This will ease you if you for example want to write a program to search string based on GNU sed. 

19. Delete Line (Case Insensitive)


Command Examples:

  1. sed '/unix/Id' text5.txt
  2. sed '/bsd/Id' text5.txt
  3. sed '/gnu/Id' text5.txt

Output Examples:

master@master:/tmp$ cat text5.txt

uNiX

UNix

unIx

uniX

uNiX

UNIx

BSD

BSD

BSD

GNU

GNU

master@master:/tmp$ sed '/unix/Id' text5.txt

BSD

BSD

BSD

GNU

GNU

master@master:/tmp$

Explanation:

This is basically the same with the previous substitution command. This just shows that the ‘I’ flag can be used in delete command. This makes searching simpler if you want to delete same words with different cases. 

20. Delete Line (OR Logic Regex)


Command Examples:

  1. sed '/<-\|->/d' text4.txt
  2. sed '/sign\|just/d' text4.txt
  3. sed '/sign\|just\|in/d' text4.txt

Output Examples:

master@master:/tmp$ sed '/<-\|->/d' text4.txt

without a sign

just the same

master@master:/tmp$ sed '/sign\|just/d' text4.txt

<- out

-> in

master@master:/tmp$ sed '/sign\|just\|in/d' text4.txt

<- out

master@master:/tmp$

Explanation:

This example emphasizes a special character called pipe (`|`), a character belongs to the backslash button. This character means OR (logical OR) in the regex. So the example number one matches either “< -” or “- >” and then delete the line containing it. So the result is only two lines don’t contain either “< -” or “- >”. Same goes with example number 2 and three.


, , , , , ,