sed action [files]
sed -e action1 -e action2 [files]
sed -f scriptfile [files]
actions specified on the command line are almost always enclosed in single quotes to prevent shell interpretation of special characters
Ex:-
cat demofile | sed 's/foo/bar/'
's/foo/bar/g' ---> global substitution
a range of line numbers can be specified
ex:- 1,10s/foo/bar/ 40,$s/foo/bar/
$ means last line in the file
deleting lines:-
11,20d - delete the second 10 lines of input
/hopscotch/d - delete all lines with the word hopscotch
sed '/foo/d' samplefile
sed '99,$!d' samplefile
deleting blank lines:-
sed '/^$/d'
----------------------------------------------------
executable sed script:-
#!/bin/sed -f
s/mvirtue/THe author of this course/
/grumpy/d
----------------------------------------------------
sed examples:-
1) showing the first 2 lines with sed & quiting
user@ubuntu:~$ sed 2q /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
2) sed -n /bash/p /etc/passwd
this command finds all the matching lines in /etc/passwd containing the text bash
/bash/ specifies the text you are looking for.
-p means to print
-n is used to suppress automatic printing of pattern space. Without this option, you would see every matching line twice
eamples
sed -n /^bash/p /etc/passwd
sed -n /./p /etc/passwd
sed -n /\./p /etc/passwd
sed -n /me\/le/p /etc/passwd
who am i | sed 's/ .* / /'
3) sed s/John/myname/g employee.txt
will search for the string John in the file employee.txt and replace it with myname
ex:-

sed ‘s/us.archive.ubuntu.com/mirrors.kernel.org/g’ /etc/apt/sources.list
4) grep == sed -n '/pattern/p'
grep -v == sed -n '/pattern/!p'
sed -e action1 -e action2 [files]
sed -f scriptfile [files]
actions specified on the command line are almost always enclosed in single quotes to prevent shell interpretation of special characters
Ex:-
cat demofile | sed 's/foo/bar/'
's/foo/bar/g' ---> global substitution
a range of line numbers can be specified
ex:- 1,10s/foo/bar/ 40,$s/foo/bar/
$ means last line in the file
deleting lines:-
11,20d - delete the second 10 lines of input
/hopscotch/d - delete all lines with the word hopscotch
sed '/foo/d' samplefile
sed '99,$!d' samplefile
deleting blank lines:-
sed '/^$/d'
----------------------------------------------------
executable sed script:-
#!/bin/sed -f
s/mvirtue/THe author of this course/
/grumpy/d
----------------------------------------------------
sed examples:-
1) showing the first 2 lines with sed & quiting
user@ubuntu:~$ sed 2q /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
2) sed -n /bash/p /etc/passwd
this command finds all the matching lines in /etc/passwd containing the text bash
/bash/ specifies the text you are looking for.
-p means to print
-n is used to suppress automatic printing of pattern space. Without this option, you would see every matching line twice
eamples
sed -n /^bash/p /etc/passwd
sed -n /./p /etc/passwd
sed -n /\./p /etc/passwd
sed -n /me\/le/p /etc/passwd
who am i | sed 's/ .* / /'
3) sed s/John/myname/g employee.txt
will search for the string John in the file employee.txt and replace it with myname
ex:-

sed ‘s/us.archive.ubuntu.com/mirrors.kernel.org/g’ /etc/apt/sources.list
4) grep == sed -n '/pattern/p'
grep -v == sed -n '/pattern/!p'
No comments:
Post a Comment