|
Sed Command in Unix and Linux Examples |
Sed is a Stream Editor used for modifying the files in unix (or linux). Whenever you want to make changes to the file automatically, sed comes in handy to do this. Most people never learn its power; they just simply use sed to replace text. You can do many things apart from replacing text with sed. Here I will describe the features of sed with examples.
|
Linux Sed Commands |
Linux Sed Command Description |
|
sed 's/Nick/John/g' report.txt |
Replace every occurrence of Nick with John in report.txt |
|
sed 's/Nick|nick/John/g' report.txt |
Replace every occurrence of Nick or nick with John |
|
sed 's/Nick/John/2' report.txt |
Command Replaces the second occurrence of the word "Nick" with "John" in a line. |
|
sed 's/Nick/John/3g' report.txt |
Replacing from nth occurrence to all occurrences in a line,In This example 3rd ,4rth,5th ….. |
|
sed '3 s/unix/linux/' file.txt |
Replacing string on a specific line number. |
|
sed '/unix/ a "Add a new line"' file.txt |
The sed command can add a new line after a pattern match is found. The "a" command to sed tells it to add a new line after a match is found |
|
sed '/unix/ i "Add a new line"' file.txt |
The sed command can add a new line before a pattern match is found. The "i" command to sed tells it to add a new line before a match is found. |
|
sed '/unix/ c "Change line"' file.txt |
The sed command can be used to replace an entire line with a new line. The "c" command to sed tells it to change the line. |
|
sed 's/^/ /' file.txt >file_new.txt |
Add 8 spaces to the left of a text for pretty printing |
|
sed -n '/Of course/,/attention you \ pay/p' myfile.
|
Display only one paragraph, starting with "Of course" and ending in "attention you pay"
|
|
sed -n 12,18p file.txt |
Show only lines 12-18 of file.txt |
|
sed 12,18d file.txt |
Show all of file.txt except for lines from 12 to 18 |
|
sed -n -e '5,7p' -e '10,13p' myfile.txt
|
Display lines 5-7 and 10-13 from myfile.txt: |
|
sed -f script.sed file.txt |
Write all commands in script.sed and execute them |
|
sed '$d' file.txt |
Delete the last line |
|
sed '/[0-9]\{3\}/p' file.txt |
Print only lines with three consecutive digits |
|
sed '17,/disk/d' file.txt |
Delete all lines from line 17 to 'disk' |
|
sed 's/^[ ^t]*//' file.txt |
Delete all spaces in front of every line of file.txt |
|
sed 's/[ ^t]*$//' file.txt |
Delete all spaces at the end of every line of file.txt |
|
sed 's/^[ ^t]*//;s/[ ^]*$//' file.txt |
Delete all spaces in front and at the end of every line of file.txt
|
|
ip route show | sed 's/ */ /g' |
To replace multiple blank spaces with a single space, we will use the output of ip route show and a pipeline |
|
sed 's/foo/bar/' file.txt |
Replace foo with bar only for the first instance in a line |
|
sed 's/foo/bar/4' file.txt |
Replace foo with bar only for the 4th instance in a line.
|
|
sed 's/foo/bar/g' file.txt |
Replace foo with bar for all instances in a line.
|
|
sed '30,40 s/version/story/g' myfile.txt |
Replacing words only within a line range (30 through 40, for example) |
|
sed '/baz/s/foo/bar/g' file.txt |
Only if line contains baz, substitute foo with bar |
|
sed -n '/^Jul 1/ p' /var/log/secure |
Viewing the authorization and authentication activities that took place on July 2, as per the /var/log/secure log in a CentOS 7 server |
|
sed '/^#\|^$\| *#/d' httpd.conf |
To remove empty lines or those beginning with # from the Apache configuration file, |
|
sed '/./,/^$/!d' file.txt |
Delete all consecutive blank lines except for EOF |
|
sed '/^$/N;/\n$/D' file.txt |
Delete all consecutive blank lines, but allows only top blank line |
|
sed '/./,$!d' file.txt |
Delete all leading blank lines |
|
sed -e :a -e '/^\n*$/{$d;N;};/\n$/ba' \ file.txt
|
Delete all trailing blank lines |
|
sed '1,20 s/Johnson/White/g' file.txt |
Do replacement of Johnson with White only on lines between 1 and 20 |
|
sed '1,20 !s/Johnson/White/g' file.txt |
Do replacement of Johnson with White (match all except lines 1-20) |
|
sed -i'.orig' 's/this/that/gi' myfile.txt |
suffix following the -i option (inside single quotes) to be used to rename the original file.
Replace all instances of this or This (ignoring case) with that in myfile.txt, and we will save the original file as myfile.txt.orig.
|
|
sed -i 's/that/this/gi;s/line/verse/gi' myfile.txt |
Performing two or more substitutions at once |
|
sed 's/unix/linux/p' file.txt |
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 -n 's/unix/linux/p' file.txt |
Use the -n option along with the /p print flag to display only the replaced lines. Here the -n option suppresses the duplicate rows generated by the /p flag and prints the replaced lines only one time |
|
sed '/./{H;$!d;};x;/regex/!d' file.txt |
Print paragraphs only if they contain regex |
|
sed -e '/./{H;$!d;}' -e 'x;/RE1/!d;\ /RE2/!d;/RE3/!d' file.txt
|
Print paragraphs only if they contain RE1, RE2 and RE3
|
|
sed 's/\(.*\)foo/bar/' file.txt |
Replace only the last match of foo with bar
|
|
sed '/regexp/!d' file.txt |
grep equivalent |
|
sed -n '/regexp/{g;1!p;};h' file.txt |
Print the line before the one matching regexp, but not the one containing the regexp |
|
sed -n '/regexp/{n;p;}' file.txt |
Print the line after the one matching the regexp, but not the one containing the regexp |
|
sed '/pattern/d' file.txt |
Delete lines matching pattern |
|
sed '/./!d' file.txt |
Delete all blank lines from a file |
|
sed '/^$/N;/\n$/N;//D' file.txt |
Delete all consecutive blank lines except for the first two |
|
sed -n '/^$/{p;h;};/./{x;/./p;}'\ file.txt |
Delete the last line of each paragraph |
|
sed 's@/usr/bin@&/local@g' path.txt |
Replace /usr/bin with /usr/bin/local in path.txt |
|
sed -e '/^#/d' /etc/services | more |
View the services file without the commented lines |
|
sed '/regex/{x;p;x;G;}' file.txt |
Insert blank line above and below |
|
sed 's/^[ ^t]*//' file.txt |
Delete all spaces in front of every line of file.txt |
|
sed 's/[ ^t]*$//' file.txt |
Delete all spaces at the end of every line of file.txt |
|
sed 's/^[ ^t]*//;s/[ ^]*$//' file.txt |
Delete all spaces in front and at the end of every line of file.txt |
|
sed 's/unix/linux/' file.txt| sed 's/os/system/' |
Sed provides -e option to run multiple sed commands in a single sed command. The above output can be achieved in a single sed command as shown below. |
|
sed -e 's/#.*//;/^$/d' thegeekstuff.txt |
Eliminate Comments and Empty Lines Using sed |
|
sed -i '1d' /tmp/passwd |
Editing the Source file by using ‘-i’ option. Above command will delete the first line of source file /tmp/passwd
|
|
root@nextstep4it:~# sed -i.bak '1d' /tmp/passwd root@nextstep4it:~# ls -l /tmp/passwd* -rw-r--r-- 1 root root 2229 Nov 24 22:36 /tmp/passwd -rw-r--r-- 1 root root 2261 Nov 24 22:35 /tmp/passwd.bak
|
In the above sed command , 1st line of file /tmp/passwd will be deleted but before that sed command takes the backup of /tmp/passwd as /tmp/passwd.bak
|
|
$ sed -n 's/Linux/Linux-Unix/gpw output' thegeekstuff.txt 1. Linux-Unix Sysadmin, Linux-Unix Scripting etc. 4. Storage in Linux-Unix $ cat output 1. Linux-Unix Sysadmin, Linux-Unix Scripting etc. 4. Storage in Linux-Unix
|
Write Changes to a File and Print the Changes Using sed s//gpw |
|
ip route show |sed -n '/src/p' | sed -e 's/ *//g'| cut -d ' '-f9 |
Combining sed and other commands
extract our IP address from the output of the ip route command. We will begin by printing only the line where the word src is. Then we will convert multiple spaces into a single one. Finally, we will cut the 9th field (considering a single space as field separator), which is where the IP address is:
|
No comments:
Post a Comment