simon / sed

Last active 1 month ago

simon revised this gist 1 month ago · 4cd1b2c

1 file changed, 37 insertions

sed.md (file created)
@@ -0,0 +1,37 @@
1 + # sed
2 +
3 + * insert a line at the start of a file:
4 +
5 + sed -i '1s/^/your text\\n/' file
6 +
7 + * replace newline char:
8 +
9 + sed ':a;N;$!ba;s/\\n/NEWCHAR/g' filename
10 +
11 + * remove two adjacent lines (`N;` joins each line to the following line):
12 +
13 + sed 'N;s/line 1\\nline 2/replacement/g' filename
14 +
15 + * cut file between lines:
16 +
17 + `sed -n '3598627,3607135p' /var/log/syslog`
18 +
19 + * insert a hash at the start of every line:
20 +
21 + `sed 's/^/#/' file.txt`
22 +
23 + * and then remove the hash:
24 +
25 + `sed 's/^#//g' file.txt`
26 +
27 + * insert a line after a match:
28 +
29 + `sed -i '/string_to_match.*/a string_to_insert' /file`
30 +
31 + * match pattern and print including trailing three lines:
32 +
33 + `sed -n '/somestring/,+3 p' filename`
34 +
35 + * match pattern and delete including trailing three lines:
36 +
37 + `sed '/somestring/,+3 d' filename`