sed系列:用地址或者模式匹配输出文件行

Unix Sed Introduction
sed is a “non-interactive” stream-oriented editor. Since its an “non-interactive” it can be used to automate editing if desired.
The name sed is an abbreviation for stream editor, and the utility derives many of its commands from the ed line-editor (ed was the first UNIX text editor).
This allows you to edit multiple files, or to perform common editing operations without ever having to open vi or emacs.
sed reads from a file or from its standard input, and outputs to its standard output.
sed has two buffers which are called pattern buffer and hold buffer. Both are initially empty.

Unix Sed Working methodology
This is called as one execution cycle. Cycle continues till end of file/input is reached.
1.Read a entire line from stdin/file.
2.Removes any trailing newline.
3.Places the line, in its pattern buffer.
4.Modify the pattern buffer according to the supplied commands.
5.Print the pattern buffer to stdout.

Printing Operation in Sed
Linux Sed command allows you to print only specific lines based on the line number or pattern matches. “p” is a command for printing the data from the pattern buffer.
To suppress automatic printing of pattern space use -n command with sed. sed -n option will not print anything, unless an explicit request to print is found.

Syntax:
# sed -n 'ADDRESS'p filename
# sed -n '/PATTERN/p' filename

let's first create example file thegeekstuff.txt
# cat thegeekstuff.txt
1. Linux - Sysadmin, Scripting etc.
2. Databases - Oracle, mySQL etc.
3. Hardware
4. Security (Firewall, Network, Online Security etc)
5. Storage
6. Cool gadgets and websites
7. Productivity (Too many technologies to explore, not much time available)
8. Website Design
9. Software Development
10.Windows- Sysadmin, reboot etc.

例子1:打印特定行
# sed -n ‘N’p filename
$sed -n '3'p thegeekstuff.txt
3. Hardware
$sed -n '3p' thegeekstuff.txt
3. Hardware
$
例子2:间隔行打印
# sed -n ‘M~N’p filename
M~N with “p” command prints every Nth line starting from line M.
$sed -n '3~2'p thegeekstuff.txt
3. Hardware
5. Storage
7. Productivity (Too many technologies to explore, not much time available)
9. Software Development
$sed -n '3~2p' thegeekstuff.txt
3. Hardware
5. Storage
7. Productivity (Too many technologies to explore, not much time available)
9. Software Development
$
例子3:起始,结束行打印
# sed -n ‘M,N’p filename
M,N with “p” command prints Mth line to Nth line.
$sed -n '4,8'p thegeekstuff.txt
4. Security (Firewall, Network, Online Security etc)
5. Storage
6. Cool gadgets and websites
7. Productivity (Too many technologies to explore, not much time available)
8. Website Design
$sed -n '4,8p' thegeekstuff.txt
4. Security (Firewall, Network, Online Security etc)
5. Storage
6. Cool gadgets and websites
7. Productivity (Too many technologies to explore, not much time available)
8. Website Design
$
例子4:输出最后一行$
# sed -n ‘$’p filename
$ with “p” command matches only the last line from the input.

$sed -n '$p' thegeekstuff.txt
10.Windows- Sysadmin, reboot etc.
$sed -n '$'p thegeekstuff.txt
10.Windows- Sysadmin, reboot etc.
$
例5:从特定行到最后一行
# sed -n ‘N,$p’ filename
$sed -n '4,$'p thegeekstuff.txt
4. Security (Firewall, Network, Online Security etc)
5. Storage
6. Cool gadgets and websites
7. Productivity (Too many technologies to explore, not much time available)
8. Website Design
9. Software Development
10.Windows- Sysadmin, reboot etc.
$sed -n '4,$p' thegeekstuff.txt
4. Security (Firewall, Network, Online Security etc)
5. Storage
6. Cool gadgets and websites
7. Productivity (Too many technologies to explore, not much time available)
8. Website Design
9. Software Development
10.Windows- Sysadmin, reboot etc.
$

Sed PATTERN Format Examples
PATTERN could be unix regular expression. The below command prints only the line which matches the given pattern.
例6:模式匹配输出
Sed Pattern Format 2: /PATTERN/,ADDRESS
Sed Pattern Format 2: /PATTERN/,ADDRESS
$sed -n '/Sysadmin/p' thegeekstuff.txt
1. Linux - Sysadmin, Scripting etc.
10.Windows- Sysadmin, reboot etc.
$sed -n '/Sysadmin/'p thegeekstuff.txt
1. Linux - Sysadmin, Scripting etc.
10.Windows- Sysadmin, reboot etc.
$
例7:模式匹配首行,到特定行
Sed Pattern Format 2: /PATTERN/,ADDRESS
# sed -n ‘/PATTERN/,Np’ filename
$sed -n '/Hardware/,6p' thegeekstuff.txt
3. Hardware
4. Security (Firewall, Network, Online Security etc)
5. Storage
6. Cool gadgets and websites
对比下面结果
$sed -n '/Sysadmin/,5'p thegeekstuff.txt
1. Linux - Sysadmin, Scripting etc.
2. Databases - Oracle, mySQL etc.
3. Hardware
4. Security (Firewall, Network, Online Security etc)
5. Storage
10.Windows- Sysadmin, reboot etc.
例8:从特定行到模式匹配行
Sed Pattern Format 3: ADDRESS,/PATTERN/
# sed -n ‘N,/PATTERN/p’ filename
$sed -n '3,/Security/p' thegeekstuff.txt
3. Hardware
4. Security (Firewall, Network, Online Security etc)
$
例9:从匹配行到最后一行
Sed Pattern Format 4: /PATTERN/,$
# sed -n ‘/PATTERN/,$p’ filename
$sed -n '/Website/,$p' thegeekstuff.txt
8. Website Design
9. Software Development
10.Windows- Sysadmin, reboot etc.
$
例10:从模式匹配航开始到以后的n行
Sed Pattern Format 5: /PATTERN/,+N
# sed -n ‘/PATTERN/,+Np’ filename
$sed -n '/Storage/,+2p' thegeekstuff.txt
5. Storage
6. Cool gadgets and websites
7. Productivity (Too many technologies to explore, not much time available)
$
例11:从匹配行到匹配行
Sed Pattern Format 6: /PATTERN/,/PATTERN/
# sed -n ‘/P1/,/P2/p’ filename
$sed -n '/Storage/,/Design/p' thegeekstuff.txt
5. Storage
6. Cool gadgets and websites
7. Productivity (Too many technologies to explore, not much time available)
8. Website Design
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值