Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 7

/disk2/home/avasundh> cat > file1.

txt
spinach,pepperoni,feta
pepperoni,Feta,mushrooms
bacon,banana peppers,feta
cheese,feta

/disk2/home/avasundh> sed ' ' file1.txt


spinach,pepperoni,feta
pepperoni,Feta,mushrooms
bacon,banana peppers,feta
cheese,feta

/disk2/home/avasundh> sed 'p' file1.txt - explicitly starting to print lines , each


line will be printed twice.
spinach,pepperoni,feta
spinach,pepperoni,feta
pepperoni,Feta,mushrooms
pepperoni,Feta,mushrooms
bacon,banana peppers,feta
bacon,banana peppers,feta
cheese,feta
cheese,feta

/disk2/home/avasundh> sed -n 'p' file1.txt - Supress automatic printing of all


lines...Now each line will be print only once.
spinach,pepperoni,feta
pepperoni,Feta,mushrooms
bacon,banana peppers,feta
cheese,feta

/disk2/home/avasundh> sed '2p' file1.txt


spinach,pepperoni,feta
pepperoni,Feta,mushrooms
pepperoni,Feta,mushrooms
bacon,banana peppers,feta
cheese,feta

/disk2/home/avasundh> sed -n '4~2p' file1.txt - Print from line no.4 to end


leaving 2 lines .
cheese,feta

/disk2/home/avasundh> cat -n file2.txt | sed -n '2~2p' - Print from 2nd line to


end leaving 2 lines
2 good nice
4 good nice nice
6 text is tonice
8 have a nice text
10 text
12 this is a text
14 tonice is awesome
16 Day is the good DAY
18 nice is nice is nice is nice

/disk2/home/avasundh> cat > file2.txt


day is today
now lets have a party today
yu r good
have a nice day
today is tomorrow
day
the day is nice
this is a day

/disk2/home/avasundh> sed '4,$d' file2.txt - It will delete from 4th line till
end , print first 3 lines
day is today
now lets have a party today
yu r good

/disk2/home/avasundh> sed -n '4,$p' file2.txt - It will print form the 4th line
till end.
have a nice day
today is tomorrow
day
the day is nice
this is a day

/disk2/home/avasundh> sed '/today/p' file2.txt - It will print all the lines


which have today word.
day is today
day is today
now lets have a party today
now lets have a party today
yu r good
have a nice day
today is tomorrow
today is tomorrow
day
the day is nice
this is a day

/disk2/home/avasundh> sed '/today/d' file2.txt - It will print all the lines


which have not today word.
yu r good
have a nice day
day
the day is nice
this is a day

/disk2/home/avasundh> sed -n '/today/p' file2.txt - It will print all the lines


which have today word.
day is today
now lets have a party today
today is tomorrow

/disk2/home/avasundh> sed -n '/today/=;/today/p' file2.txt - Print the lines


along with numbers which have today word
1
day is today
2
now lets have a party today
5
today is tomorrow
/disk2/home/avasundh> sed '/today/=;/today/p' file2.txt
1
day is today
day is today
2
now lets have a party today
now lets have a party today
yu r good
have a nice day
5
today is tomorrow
today is tomorrow
day
the day is nice
this is a day
/disk2/home/avasundh> sed -n '=' file2.txt -> -> = used to print the line
numbers
1
2
3
4
5
6
7
8

/disk2/home/avasundh> sed '=' file2.txt -> -> = used to print the line numbers
1
day is today
2
now lets have a party today
3
yu r good
4
have a nice day
5
today is tomorrow
6
day
7
the day is nice
8
this is a day

/disk2/home/avasundh> sed -n 's/day/text/p' file2.txt -> Replace day with text


( S is used for substitution of a string/Replace string with given replace string))
text is today
now lets have a party totext
have a nice text
totext is tomorrow
text
the text is nice
this is a text

/disk2/home/avasundh> cat file2.txt


day is today
now lets have a party today
yu r good
have a nice day
today is tomorrow
day
the day is nice
this is a day

/disk2/home/avasundh> sed -n 's/\bday\b/text/p' file2.txt -> for exact match


text is today
have a nice text
text
the text is nice
this is a text

/disk2/home/avasundh> sed -n 's/day/text/gp' file2.txt


text is totext
now lets have a party totext
have a nice text
totext is tomorrow
text
the text is nice
this is a text

/disk2/home/avasundh> sed -n 's/\bday\b/text/gp' file2.txt


text is today
have a nice text
text
the text is nice
this is a text

/disk2/home/avasundh> sed -n 's/day/text/gip' file2.txt -> g- globally, i-


ignoring case , p =print
text is totext
now lets have a party totext
have a nice text
totext is tomorrow
text
the text is nice
this is a text

/disk2/home/avasundh> sed -n 's/\bday\b/text/gip' file2.txt > exact match


text is today
have a nice text
text
the text is nice
this is a text
/disk2/home/avasundh> sed -n '4,6s/\bday\b/text/gip' file2.txt -> print from 4to
6 lines
have a nice text
text

/disk2/home/avasundh> cat file2.txt


day is today
now lets have a party today
yu r good
have a nice day
today is tomorrow
day
the day is nice
this is a day

/disk2/home/avasundh> sed -i -n 's/day/text/gp' file2.txt -> to edit the original


file.
/disk2/home/avasundh> cat file2.txt
text is totext
now lets have a party totext
have a nice text
totext is tomorrow
text
the text is nice
this is a text

/disk2/home/avasundh> sed -i 's/day/nice/gp' file2.txt


/disk2/home/avasundh> cat file2.txt
good nice
good nice
good nice nice
good nice nice
text is tonice
text is tonice
now lets have a party totext
have a nice text
totext is tomorrow
text
the text is nice
this is a text
tonice is awesome
tonice is awesome

/disk2/home/avasundh> cat file3.txt


text is tomorrow
text
the text is nice
this is a text
tonice is awesome
tonice is awesome
DAY is DAy
Day is the good DAY
/disk2/home/avasundh> sed -n 's/text/hi/p' file3.txt ->print only changed lines
hi is tomorrow
hi
the hi is nice
this is a hi

/disk2/home/avasundh> cat file3.txt


text is tomorrow
text
the text is nice
this is a text
tonice is awesome
tonice is awesome
DAY is DAy
Day is the good DAY
nice is nice is nice is nice
nice is nice is nice is nice

/disk2/home/avasundh> sed -i -n 's/text/hi/p' file3.txt

/disk2/home/avasundh> cat file3.txt -> print only changed lines


hi is tomorrow
hi
the hi is nice
this is a hi

/disk2/home/avasundh> cat >>file4.txt


text is tomorrow
text
the text is nice
this is a text
tonice is awesome
tonice is awesome
DAY is DAy
Day is the good DAY
nice is nice is nice is nice
nice is nice is nice is nice

/disk2/home/avasundh> sed -i'.bak' 's/text/hi/gi' file4.txt -> Create a backup of


original file and then edit the file
/disk2/home/avasundh> cat file4.txt
hi is tomorrow
hi
the hi is nice
this is a hi
tonice is awesome
tonice is awesome
DAY is DAy
Day is the good DAY
nice is nice is nice is nice
nice is nice is nice is nice

/disk2/home/avasundh> cat file4.txt.bak -> the original content will be


available in a back up file.
text is tomorrow
text
the text is nice
this is a text
tonice is awesome
tonice is awesome
DAY is DAy
Day is the good DAY
nice is nice is nice is nice
nice is nice is nice is nice

/disk2/home/avasundh> sed 'G' file1.txt -> G -Add the 1 line space bw the
lines
spinach,pepperoni,feta

pepperoni,Feta,mushrooms

bacon,banana peppers,feta

cheese,feta

/disk2/home/avasundh> sed 'G;G' file1.txt -> G;G used to add 2 lines of spaces
spinach,pepperoni,feta

pepperoni,Feta,mushrooms

bacon,banana peppers,feta

cheese,feta

You might also like