Sed Command for CentOS/RHEL
sed, short for “stream editor”, used for modifying the files in unix (or linux). At whatever point you need to change the file automatically, sed proves to be useful to do this. You can do numerous things separated from replacing content with sed. Sed doesn’t typically modify an original input file ; instead we can send contents of our file through a pipe to be processed by sed. In this post we will discuss different way to use sed command.
Consider the below text file as an input file.
# cat sed.txt
Output:
Wants to learn different operating system. linux is opensource OS. linux is free OS. linux perform task fast than windows. linuxwindows which one you choose. http://techoism.com
Some sed Command Examples
Replacing string
With sed command we replace the text in a file.
# sed 's/linux/windows/' sed.txt
Output:
Wants to learn different operating system. windows is opensource OS. linux is free OS. windows perform task fast than windows. windowswindows which one you choose. http://techoism.com
In the above sed command we replaces the word “linux” with “windows” in the file.
Note:
“s” consider files as separate rather than as a single continuous long stream.
“/” are delimiters.
“linux” is the search pattern.
“windows” is the replacement string.
Replacing all the string in a file
Use flag /g (global replacement) specifies the sed command to replace all the occurrences of the string in the line.
# sed 's/linux/windows/g' sed.txt
Output:
Wants to learn different operating system. windows is opensource OS. windows is free OS. windows perform task fast than windows. windowswindows which one you choose. http://techoism.com
Changing the slash (/) delimiter
Use any delimiter rather than the slash. Suppose you want to change the web url then use following command:
# sed 's/http:\/\//www./' sed.txt
Output:
Wants to learn different operating system. linux is opensource OS. linux is free OS. linux perform task fast than windows. linuxwindows which one you choose. www.techoism.com
Using too many backslashes makes the sed command typical. In this case we can change the delimiter to another character as shown in the below example.
# sed 's_http://_www._' file.txt # sed 's|http://|www.|' file.txt
Output:
Wants to learn different operating system. linux is opensource OS. linux is free OS. linux perform task fast than windows. linuxwindows which one you choose. www.techoism.com
Using “&” as the matched string
In some cases where you want to search string and replace it with adding some extra characters. In that case use following command:
# sed 's/linux/(&)/' sed.txt
Output:
Wants to learn different operating system. (linux) is opensource OS. linux is free OS. (linux) perform task fast than windows. (linux)windows which one you choose. http://techoism.com
# sed 's/linux/(&&)/' sed.txt
Output:
Wants to learn different operating system. (linuxlinux) is opensource OS. linux is free OS. (linuxlinux) perform task fast than windows. (linuxlinux)windows which one you choose. http://techoism.com
Using \1, \2 and so on
If you want to replace the string “linux” in a line with thrice as the string like “linuxlinuxlinux” use the sed command as below:
# sed 's/\(linux\)/\1\1\1/' sed.txt
Output:
Wants to learn different operating system. linuxlinuxlinux is opensource OS. linux is free OS. linuxlinuxlinux perform task fast than windows. linuxlinuxlinuxwindows which one you choose. http://techoism.com
If you want to switch the strings “linuxwindows” as “windowslinux”, use the sed command as below:
# sed 's/\(linux\)\(windows\)/\2\1/' sed.txt
Output:
Wants to learn different operating system. linux is opensource OS. linux is free OS. linux perform task fast than windows. windowslinux which one you choose. http://techoism.com
If you want to switch the first three characters in a line then use sed command as below:
# sed 's/^\(.\)\(.\)\(.\)/\3\2\1/' sed.txt
Output:
naWts to learn different operating system. nilux is opensource OS. linux is free OS. nilux perform task fast than windows. niluxwindows which one you choose. tthp://techoism.com
Duplicating the replaced line
The /p print flag prints the replaced line twice on the terminal. If a line does not have the search pattern and is not replaced, then the /p prints that line only once.
# sed 's/OS/operating system/p' sed.txt
Output:
Wants to learn different operating system. linux is opensource operating system. linux is free OS. linux is opensource operating system. linux is free OS. linux perform task fast than windows. linuxwindows which one you choose. http://techoism.com