Another personal reference
Two basic forms of use.
sed [-n] [-e] 'command(s)' files
sed [-n] -f scriptfile files
First one uses inline commands. The second one uses a script file. A command like delete or print can be executed based on an address/line range or a pattern. Lets look at the commands themselves in no particular order just to recollect things. All concepts are mixed and mashed.
pstands for print and it prints the lines that are specified. ‘1p’ will print the first line.
'1,5 p'will print the lines from one to five.
'2, +3 p'will print line 2 and the next 3 lines as well.
'2, ~4 p'will print line 2 and every fourth line from that. Meaning, 2, 6, 10, 14, 18, 22…
- Address and Pattern matching can be used together.
'/hello/, 5 p'looks for the line that has ‘hello’ and then prints five lines that follow it.
'/hello/, $ p'prints from the line that has the first occurence of hello till the end
'/pattern1/ /pattern2/ p'prints all the lines that contains pattern 1 upto the line that contains pattern 2.
dis for deleting a line. Similar to p, for printing.
wcan be used to write to a file after filtering some lines. Or with no filters at all, which would make it work like a cp command.
'w newfile'will make a copy of the target file into the new file
'2,5 w newfile'will just write the lines 2 to 5 of the target file to the new file. Pattern matching can also be used instead of Address matching.
acan be used to append a line to a file.
'4 a text to be appended'appends the text to be appended to the file after line 4.
ccan be used for replacing or changing patterns or lines.
'4 c text line to be replaced'will replace the fourth line with the text line to be replaced. Multiple lines can be replaced with single lines as well.
istands for insert.
- Syntax for y
[address1[,address2]]lshows hidden characters
[address]q [value]for quit
[address]r fileread and insert contents of file into address of the target file specified
[address1[,address2]]e [command]execute bash command on the address
's/Paulo Coelho/PAULO COELHO/w junk.txt'replace and write to new file
More to be added to this post.