-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shell
47 lines (35 loc) · 1.32 KB
/
Shell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
1. Replace text across multiple lines
E.g. replace text between <<< and >>> to text "COMMENT" in a test.txt file with below info
file content
aabbcc<<<comment part 1
comment part 2>>>
ddeeff
Create a replace.sed file including code below
:begin
/<<</,/>>>/ {
/>>>/! {
$! {
N;
b begin
}
}
s/<<<.*>>>/COMMENT/;
}
Execute command: sed -f replace.sed test.txt
2. Delete a pattern-matched line and N lines after it in a text file.
sed -i '/<replac with PATTERN>/I, +<replace with N> d' <replace with input filename>
3. Delete a pattern-matched line and N lines before it in a text file.
tac <replace with input filename> | sed '/<replac with PATTERN>/I, +<replace with N> d' |tac > <replace with output filename>
4. Delete a pattern-matched line and M lines before it and N lines after it in a text file.
grep -v "$(grep -B <replace with M> -A <replace with N> '<replac with PATTERN>' <replace with input filename>)" <replace with input filename> > <replace with output filename>
5. Read ini config file.
File format:
[section_1]
key_1=value_1
key_2=value_2
...
[section_2]
key_1=value_1
key_2=value_2
...
grep -Pzo "(?s)(\s*)\N*\[<replace with section name>].*?\[|\[<replace with section name>\].*" <replace with ini filename>