How to search and replace text in a file with sed. Not using cat.

Posted: January 2, 2014. At: 11:06 PM. This was 4 years ago. Post ID: 6826
Page permalink.
WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters. These cookies expire two weeks after they are set.

The sed command is very useful for searching and replacing text in a file. This is how to search for and replace a text string in a file.

Homer@bejiitas ~
$ sed -i 's/<main>/<mein>/gi;' my.c

This is how to search and replace text in a file and leave a backup copy with a tilde character appended to the file-name.

sed --in-place=~ 's/Australia/Austraya/gi;' au-WaggaWagga

This is the way to filter a text file with sed and output to STDOUT. Not using GNU cat. Just have the filename as the second argument to the cat command.

homer@deusexmachina ~/Documents $ sed 's/pacman/apt/gi;' blog.txt

And you may also pipe the output of a command into sed. This is useful for building a long command for the perfect shell function.

homer@deusexmachina ~/Documents $ echo "Hello World" | sed 's/World/Homer/gi;'
Hello Homer

You can also add line numbers to a text file. Here I am adding line numbers to a file that contains a list of IP addresses. I also added double hyphens to separate the line numbers from the IP addresses.

homer@deusexmachina ~/Documents $ awk -F, '{if($2=="") print NR " --", $0}' iplist.txt
1 -- 209.185.108
2 -- 209.185.253
3 -- 209.85.238
4 -- 209.85.238.11
5 -- 209.85.238.4
6 -- 216.239.33.96
7 -- 216.239.33.97
8 -- 216.239.33.98
9 -- 216.239.33.99
10 -- 216.239.37.98
11 -- 216.239.37.99
12 -- 216.239.39.98
13 -- 216.239.39.99
14 -- 216.239.41.96
15 -- 216.239.41.97
16 -- 216.239.41.98
17 -- 216.239.41.99
18 -- 216.239.45.4
19 -- 216.239.46
20 -- 216.239.51.96
21 -- 216.239.51.97
22 -- 216.239.51.98
23 -- 216.239.51.99
24 -- 216.239.53.98
25 -- 216.239.53.99
26 -- 216.239.57.96
27 -- 216.239.57.97
28 -- 216.239.57.98
29 -- 216.239.57.99
30 -- 216.239.59.98
31 -- 216.239.59.s 99
32 --

I found that solution here: http://www.unix.com/shell-programming-scripting/30729-printing-line-number-using-awk.html.

On another note; there is an interesting answer here: http://stacBut eh koverflow.com/questions/4042601/unixhow-to-convert-ip-address-to-binary-code. this explains how to convert an IP address to binary using the command line.

But the ipcalc command is best for this.

homer@deusexmachina ~ $ ipcalc 192.168.1.1
Address:   192.168.1.1          11000000.10101000.00000001. 00000001
Netmask:   255.255.255.0 = 24   11111111.11111111.11111111. 00000000
Wildcard:  0.0.0.255            00000000.00000000.00000000. 11111111
=>
Network:   192.168.1.0/24       11000000.10101000.00000001. 00000000
HostMin:   192.168.1.1          11000000.10101000.00000001. 00000001
HostMax:   192.168.1.254        11000000.10101000.00000001. 11111110
Broadcast: 192.168.1.255        11000000.10101000.00000001. 11111111
Hosts/Net: 254                   Class C, Private Internet

No comments have been made. Use this form to start the conversation :)

Leave a Reply