sed's Examples

Replace single spaces and hyphens between spaces in file names - Test echo of filename

for file in *; do echo mv "$file" "$(echo $file | sed 's/ - /_/g ; s/ /-/g')" ; done

Replace single spaces and hyphens between spaces in file names - Remove first echo to commit changes.

for file in *; do mv "$file" "$(echo $file | sed 's/ - /_/g ; s/ /-/g')" ; done

It substitutes the old string with the new one mentioned.

sed "s/unix/linux/" geek

Duplicates the replaced line with /p flag.

sed "s/unix/linux/p" geek

Inserts one blank line after every line.

sed G geek

Inserts a blank line after each line that matches the string learn

sed '/learn/G' geek

insert one blank line after each line

$ sed G a.txt

delete blank lines and insert blank line after each line 

$ sed '/^$/d;G' a .txt

number each line of a file . "=" is used to number each line. "\" is used for tab between number and sentence

$ sed =a.txt \sed 'N: s /

number each line of file, only if line is not blank

sed ' /./=' a.txt | sed '/. / N: s/\N / /'

delete a particular line

sed 'sd' a.txt

delete the last line

sed '$d' a.txt

delete the last line

sed '$d' a.txt

delete line line from 3 to 5 lines

sed '3,5d' a.txt

delete the pattern matching line

sed '/life/d' a.txt

delete lines starting from nth line and every 2nd line from there

sed 'n~2d' file name

view a file from 2 to 5 line range

sed -n '2,5p, a.txt

print nth file of the file

sed -n '4' p a.txt

 print the line that matches the pattern

sed -n /every/ p a.txt

change the first occurance of the pattern

sed 's/life/leaves/' a.txt

replace the nth occurance of a pattern of line

sed 's/to/two/2' a.txt

replacing all the occurance of pattern of line

sed 's/life/learn/g' a.txt

replace a pattern with other except the nth line

sed -i '5! s /life/love /' a.txt

this command is used to replace the text in a file

$ sed /unix/linux/' geekfile.txt

replacing all the occurances of the pattern in a line

$ sed 's/unix/linux/g' geekfile.txt

replacing the string in a specific line

$ sed '3 s/unix/linux/' geekfile.txt

the /p orint flag prints the repalced line twice on the terminal.

$ sed 's/unix/linux/p' geekfile.txt


 printing only the replaced lines

$ sed -n 's/unix/linux/p' geekfile.txt


 printing only the replaced lines

$ sed -n 's/unix/linux/p' geekfile.txt

delete a particular line

$ sed 'nd' filename.txt

delete the last line

$ sed '$d' filename.txt

delete pattern matching line

$ sed '/pattern/d' filename.txt

it executes more than 2 or 3 sed commannds at a time

$ sed 'are ; hello' file1.txt

-s replaces red word to blue in the given file

$ sed -e 's/red/blue;' colors.txt

the above command will limit the sed command to operate on the 3rd line

$ sed '3s/red/blue' colors.txt

the above command will insert colors before are bright

echo "are bright" |sed 'i\colors'

the above command will append the text

echo "are bright" | sed 'a\colors'

the above command will append the text

echo "are bright" | sed 'a\colors'

the 2nd will be moified to manago is in yellow

sed '2c/mango is in yellow./ colors.txt

this command transforms ethe letters with abc to def 

sed 'y/abc/def/' colors.txt

gives the line numbers to the line in the color.txt fike

sed "=" colors.txt

gives the line number  in which mango word is there

sed -n '/mango=/' colors.txt

it modifies the apple is red with line updated  in colors.txt 

sed '/apple is/c line updated.' colors.txt

DevOpsSchool
Typically replies within an hour

DevOpsSchool
Hi there 👋

How can I help you?
×
Chat with Us