Sed
Categories:
6 minute read
SED: Delete lines from file (delete command)
Description | Command | Input | Output |
---|---|---|---|
Delete the line 2 and 5 from the file my.txt |
sed -e '2d' -e '5d' my.txt
Or sed -e '2d;5d' my.txt
|
line - one |
line - one |
Delete range of lines e.g. delete lines 2 to 4 | sed -e 2, 4 d' my.txt |
line - one |
line - one |
Remove lines containing a specific word e.g. remove lines containing rocket |
sed '/three/d' my.txt |
line - one |
line - one |
Remove lines between two words | sed '/two/,/four/d' my.txt |
line - one |
line - one |
SED: Extract specific lines from a file (print command)
To extract line 3 and 5
sed -n -e '3p' -e '5p' my.txt
or semi colon separated
sed -n -e '3p;5p' my.txt
Extract range of lines - e.g. get lines from 2 to 5
sed -n -e '2,5 p' my.txt
Extract last line of the file
sed -n -e '$ p' my.txt
Extract range of lines: from 3 to last line
sed -n -e '3,$ p' my.txt
Extract no. of lines from a specific line. e.g. get four lines starting from line 2
sed -n -e '2,+4 p' my.txt
Extract every nth line starting form a specific line. e.g. get every 2nd line starting from first line
send -n -e '1~2 p' my.txt
Extract lines containing specific word. e.g. lines containing the word rocket
sed -n -e '/rocket/ p' my.txt
Extracts lines starting with first match of rocket to the nth line
sed -n -e '/rocket/, 5 p' my.txt # till 5th line
sed -n -e '/rocket/, $ p' my.txt # till last line
sed -n -e '/rocket/, +5 p' my.txt # next 5 lines after the match
Lines between two words
sed -n -e '/rocket/, /science/ p' my.txt
SED: Extract and write to file (write command)
This one exactly same as p command described above, except here we give a filename and output is written to the file.
e.g. to extract all lines containing the word rocket into a file rocket.txt
sed -n -e '/rocket/ w rocket.txt' my.txt
interestingly, following writes each patterns into separate file. i.e. lines containing rocket written into rocket.txt and lines containing the word science are written into science.txt
sed -n -e '/rocket/ w rocket.txt' -e '/science/ w science.txt' my.txt
SED: Append command
Append a line after nth line e..g insert a new line ’this is some line’ after 4th line
sed -e '4 a this is some line' my.txt
Insert after the last line
send -e '$ a this is some line' my.txt
Insert after specific pattern (Note: inserted for each match)
send -e '/rocket/ a this is some line' my.txt
SED: Insert command
Insert command works same way as append command mentioned above, the only difference it inserts before the line
SED: Read command
Read command similar to append except it get the contents from file
Append content from file after nth line e..g insert the content of my2.txt after 4th line of my.txt
sed -e '4 r my2.txt' my.txt
SED: Change command
Replace a line with new line e.g. replace line 3 with ’this is new line'
sed -e '3 c this is new line' my.txt
Replace a line containing word
sed -e '/rocket/ c this is new line' my.txt
Replace range of lines e.g. replace lines 2 to 4 with ’this is new line'
sed '4, 6 c this is new line' my.txt
SED: Translate command
Translate characters
echo 'this is some text' | sed 'y/abcdefghij/1234567890/'
this outputs t89s 9s som5 t5xt
SED L command
Replace white space with escape chars
sed -n 'l' my.txt
this command replaces tab with \t and end of line with $, etc
To wrap lines with n chars
send -n -e 'l 5' my.txt
SED: quit command
Print first 3 lines
sed -e '3 q' my.txt
print until pattern match
sed -e '/rocket/ q' my.txt
quit with exit status e.g. exit with status code 1
sed -e '/rocket/ q 11' my.txt
after executing the above check the status echo $? this prints 11
SED: Execute command
Executes external command
e.g. insert date at line 3
sed '3 e date' my.txt
insert data where it matches a word
sed '/rocket/ e date' my.txt
SED: = command
The = command writes the line number followed by its contents
sed '=' my.txt
Line number for first 3 lines
sed '1, 3=' my.txt
SED: & command
& gives the matched pattern value
e.g following replaces science with rocket science
sed 's/science/rocket &/' my.txt
SED: Substitute command
Replace a pattern with a value
e.g. change first occurrences of rocket with science in each line.
sed -e 's/rocket/science/' my.txt
on all occurrences
sed -e 's/rocket/science/g' my.txt
restrict by pattern. e.g. replace rocket with science where a line contains the word extreme
sed -e '/extreme/ s/rocket/science/g' my.txt
Replace the second occurrence
sed -e '/s/rocket/science/2' my.txt
print only changed lines
sed -e '/s/rocket/science/p' my.txt
Write only changed lines to a different file
sed -e '/s/rocket/science/w new.txt' my.txt
SED: command index
- a = append
- b = branch
- c = change text
- d = delete lines
- D = delete first line (in patspace)
- e = execute on the command line
- g = copy from holdspace
- G = append from hold space
- h = copy to hold space
- H = append to hold space
- i = insert
- l = print substitute line feeds
- n = next input line
- N = append next input line
- p = print
- P = print first line
- q = quit
- Q = quit immediately
- r = read file
- R = read line from file
- s = substitute
- t = test for substitution
- T = test for no substitution
- y = simple character replace
- w = write to file
- W = write line to file
- z = clear line (zap)
- = = print line number
SED: Options
-n, –quiet, –silent: Same as standard -n option.
-e script, –expression=script: Same as standard -e option.
-f script-file, –file=script-file: Same as standard -f option.
--follow-symlinks: If this option is provided, the SED follows symbolic links while editing files in place.
-i[SUFFIX], –in-place[=SUFFIX]: This option is used to edit file in place. If suffix is provided, it takes a backup of the original file, otherwise it overwrites the original file.
-l N, –line-lenght=N: This option sets the line length for l command to N characters.
--posix: This option disables all GNU extensions.
-r, –regexp-extended: This option allows to use extended regular expressions rather than basic regular expressions.
-u, –unbuffered: When this option is provided, the SED loads minimal amount of data from the input files and flushes the output buffers more often. It is useful for editing the output of “tail -f” when you do not want to wait for the output.
-z, –null-data: By default, the SED separates each line by a new-line character. If NULL-data option is provided, it separates the lines by NULL characters.