OSDN Git Service

Regular updates
[twpd/master.git] / sed.md
1 ---
2 title: sed
3 category: CLI
4 layout: 2017/sheet
5 intro: |
6   Here's some hints on using sed.
7 ---
8
9 ## In place replacements
10
11 ### In-place replacement (GNU)
12
13 ```bash
14 sed -i -e 's/foo/bar/' example.md
15 ```
16
17 In GNU sed: use `-i` without arg.
18
19 #### In-place replacement (BSD)
20
21 ```bash
22 sed -i '' -e 's/foo/bar/' example.md
23 ```
24
25  In OSX, `-i ''` is required.
26
27 ## File regions
28 {:.-three-column}
29
30 ### Print until a certain line is met
31
32 ```bash
33 sed '/begin api/q'
34 ```
35
36 ### Print until a certain line is met, but not that line
37
38 ```bash
39 sed '/^# begin/,$d'
40 ```
41
42 ### Print everything after a given line
43
44 ```bash
45 sed -n '/end api/,$p'
46 ```
47
48 Print after a given line is found.
49
50 ### Print everything except matching
51
52 ```bash
53 sed -n '/regex/d;'
54 ```
55
56 Print everything except lines matching regex. Useful for printing files with comments.