17 Important sed command examples with tutorial

1 Write a sed command that will delete line 7 through line 11 inclusively from the file called sedfile?

You have to use number line addressing to specify the lines that you want to delete. d command is used for deleting lines in sed editor.

$sed '7,11d' sedfile

2 Write a sed command that will replace all occurrences of X and x with s within the file called sedfile?

s command is used for replacing text. The question stats that we have to replace all the occurrences of X and x, we have to use g flag to achieve this. The pipe symbol(|) or character class([]) is suitable for this problem but the pipe symbol is not recognized as a regular expression by sed command so are left with the character class.

$sed 's/[Xx]/s/g' sedfile

3 Write a sed command line string that will delete any line that begins with the string 'directory' within the file called sedfile.

Caret character(^) is used to match the line that begins with the string 'directory'.

$sed '/^directory/d' sedfile

4 Write a sed command that is used to add the line '++++' at the end of the file called sedfile?

a command is used to append a new line after the specified line. $ represents the end of the file.

$sed '$a\+++++' sedfile

5 Write a sed command to print all lines except the first line?

! is used to apply commands on all lines except those who are specified before ! symbol.

$sed -n '1!p' filename

6 Write a one-line sed command that will replace all multiple spaces in a file called names.txt by only one space.

$sed 's/   *//g' names.txt

There are three spaces between the first forward slash and * symbol. If you want you can use a special character class [[:space:]] instead of using a space in sed command.

$sed 's/[[:space:]][[:space:]][[:space:]]*/ /g' names.txt

7 Write a sed command that will replace all occurrences of the name Mary with Sue in lines 2 through 7 in a file called names.txt

$sed '2,7s/Mary/Sue/g' names.txt

8 Write a sed command to replace ALL the occurrences of Hallowein with Halloween from line 3 to the end of the file in the file called halloween

$sed '3,$s/Hallowein/Halloween/g' halloween

9 Write a sed command to replace all phone numbers that look like 1234567890 with (123)456-7890 in a file called phonebook.txt.

$sed 's/\([0-9][0-9][0-9]\)\([0-9][0-9][0-9]\)\([0-9][0-9][0-9][0-9]\)/(\1)\2-\3/g' phonebook.txt

10 Give a sed command that would output every line in a file that did not start with the letter 'a' and end with .html

$sed -n '/^[^a]/p' main.sh | sed -n '/\.html$/p'

11 Write a sed command to convert dates in the form mm-dd-yy to mm-dd-yyyy, assuming the years are all in the 21st century.

$sed -E 's/([0-9][0-9])-([0-9][0-9])-([0-9][0-9])/\2-\1-20\3/' dates.txt

12 Write a sed command that changes all pipe delimited data of a file pipes.csv to tilde delimited data, and save it to a file tildelimited.csv

$sed 's/|/~/g' pipes.csv > tildelimited.csv

13 Write a sed command that will replace "mon" to "Monday", "tues" to "Tuesday" and so on in a file calendar and save the output

$sed 's/mon/Monday/; s/tues/Tuesday/; s/wed/Wednesday/; s/thurs/Thursday/; s/fri/Friday/; s/sat/Saturday/; s/sun/Sunday/' calendar

14 Write a sed command which will switch two words. That is echo foo bar | sed should produce bar foo. Do not hard code your script to use foo and bar. It should swap any two words.

read -p "Enter first word: " word1
read -p "Enter first word: " word2
echo $word1 $word2 | sed 's/\(..*\) \(..*\)/\2 \1/'

15 Write a line in sed to find all sequences of 10 digits in a row (a raw phone number) and replaces it with a more nicely formatted one. For example, it would replace 1234567890 with (123)456-7890

$sed 's/\([0-9][0-9][0-9]\)\([0-9][0-9][0-9]\)\([0-9][0-9][0-9][0-9]\)/(\1)\2-\3/'

16 How to remove blank lines from a file in Bash Shell?

$sed '/^[[:space:]]*$/d' filename

17 Write a bash script that does the following with sed commands:

  1. reads input
  2. removes first 2 characters then prints the result
  3. swaps the first two characters
  4. swaps the first and last two characters

Example output

AWESOME
ESOME
WAESOME
EWESOMA
read input
echo $input
echo $input | sed 's/\(..\)\(..*\)/\2/'
echo $input | sed 's/\(.\)\(.\)\(..*\)/\2\1\3/'
echo $input | sed 's/\(.\)\(..*\)\(.\)/\3\2\1/'

Recommended Posts