博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Unix Sed Tutorial 1 : Printing File Lines using Address and Patterns
阅读量:2217 次
发布时间:2019-05-08

本文共 6133 字,大约阅读时间需要 20 分钟。

Let us review how to print file lines using address and patterns in this first part of sed tutorial.

We’ll be posting several awesome sed tutorials with examples in the upcoming weeks.

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 us first create thegeekstuff.txt file that will be used in all the examples mentioned below.

# cat thegeekstuff.txt1. Linux - Sysadmin, Scripting etc.2. Databases - Oracle, mySQL etc.3. Hardware4. Security (Firewall, Network, Online Security etc)5. Storage6. Cool gadgets and websites7. Productivity (Too many technologies to explore, not much time available)8. Website Design9. Software Development10.Windows- Sysadmin, reboot etc.

5 Sed ADDRESS Format Examples

Sed Address Format 1: NUMBER

This will match only Nth line in the input.

# sed -n ‘N’p filename

For example, 3p prints third line of input file thegeekstuff.txt as shown below.

# sed -n '3'p thegeekstuff.txt3. Hardware

Sed Address Format 2: NUMBER1~NUMBER2

M~N with “p” command prints every Nth line starting from line M.

# sed -n ‘M~N’p filename

For example, 3~2p prints every 2nd line starting from 3rd line as shown below.

# sed -n '3~2'p thegeekstuff.txt3. Hardware5. Storage7. Productivity (Too many technologies to explore, not much time available)9. Software Development

Sed Address Format 3: START,END

M,N with “p” command prints Mth line to Nth line.

# sed -n ‘M,N’p filename

For example, 4,8p prints from 4th line to 8th line from input file thegeekstuff.txt

# sed -n '4,8'p thegeekstuff.txt4. Security (Firewall, Network, Online Security etc)5. Storage6. Cool gadgets and websites7. Productivity (Too many technologies to explore, not much time available)8. Website Design

Sed Address Format 4: ‘$’ Last Line

$ with “p” command matches only the last line from the input.

# sed -n ‘$’p filename

For example, $p prints only the last line as shown below.

# sed -n '$'p thegeekstuff.txt10.Windows- Sysadmin, reboot etc.

Sed Address Format 5: NUMBER,$

N,$ with “p” command prints from Nth line to end of file.

# sed -n ‘N,$p’ filename

For example 4,$p prints from 4th line to end of file.

# sed -n '4,$p' thegeekstuff.txt4. Security (Firewall, Network, Online Security etc)5. Storage6. Cool gadgets and websites7. Productivity (Too many technologies to explore, not much time available)8. Website Design9. Software Development10.Windows- Sysadmin, reboot etc.

6 Sed PATTERN Format Examples

Sed Pattern Format 1: PATTERN

PATTERN could be unix regular expression. The below command prints only the line which matches the given pattern.

# sed -n /PATTERN/p filename

For example, following prints the line only which matches the pattern “Sysadmin”.

# sed -n /Sysadmin/p thegeekstuff.txt1. Linux - Sysadmin, Scripting etc.10.Windows- Sysadmin, reboot etc.

Sed Pattern Format 2: /PATTERN/,ADDRESS


# sed -n ‘/PATTERN/,Np’ filename

For example, following prints lines which matches the pattern to Nth line, from input. 3rd line matches the pattern “Hardware”, so it prints from 3rd line to 6th line.

# sed -n '/Hardware/,6p' thegeekstuff.txt3. Hardware4. Security (Firewall, Network, Online Security etc)5. Storage6. Cool gadgets and websites

Sed Pattern Format 3: ADDRESS,/PATTERN/

It prints from the Nth line of the input, to the line which matches the pattern. If the pattern doesnt match, it prints upto end of the input.

# sed -n ‘N,/PATTERN/p’ filename

For example, 4th line matches the pattern “Security”, so it prints from 3rd line to 4th line.

# sed -n '3,/Security/p' thegeekstuff.txt3. Hardware4. Security (Firewall, Network, Online Security etc)

Sed Pattern Format 4: /PATTERN/,$

It prints from the line matches the given pattern to end of file.

# sed -n ‘/PATTERN/,$p’ filename

# sed -n '/Website/,$p' thegeekstuff.txt8. Website Design9. Software Development10.Windows- Sysadmin, reboot etc.

Sed Pattern Format 5: /PATTERN/,+N

It prints the lines which matches the pattern and next N lines following the matched line.

# sed -n ‘/PATTERN/,+Np’ filename

For example, following prints the 5th line which matches the pattern /Storage/ and next two lines following /Storage/.

# sed -n '/Storage/,+2p' thegeekstuff.txt5. Storage6. Cool gadgets and websites7. Productivity (Too many technologies to explore, not much time available)

Sed Pattern Format 6: /PATTERN/,/PATTERN/

Prints the section of file between two regular expression (including the matched line ).

# sed -n ‘/P1/,/P2/p’ filename

For example, 5th line matches “Storage” and 8th line matches “Design”, so it prints 5th to 8th.

# sed -n '/Storage/,/Design/p' thegeekstuff.txt5. Storage6. Cool gadgets and websites7. Productivity (Too many technologies to explore, not much time available)8. Website Design
 

自己补充一个

Sed Pattern Format 7:  address!  排除这一行

sed -n '1!p' thegeekstuff.txt   不打印第一行
sed -n '$!p' thegeekstuff.txt  不打印最后一行
sed -n  '1,5!p' thegeekstuff.txt  不打印1~5行

转载地址:http://ruffb.baihongyu.com/

你可能感兴趣的文章
【雅思】雅思需要购买和准备的学习资料
查看>>
【雅思】雅思写作作业(1)
查看>>
【雅思】【大作文】【审题作业】关于同不同意的审题作业(重点)
查看>>
【Loadrunner】通过loadrunner录制时候有事件但是白页无法出来登录页怎么办?
查看>>
【English】【托业】【四六级】写译高频词汇
查看>>
【托业】【新东方全真模拟】01~02-----P5~6
查看>>
【托业】【新东方全真模拟】03~04-----P5~6
查看>>
【托业】【新东方托业全真模拟】TEST05~06-----P5~6
查看>>
【托业】【新东方托业全真模拟】TEST09~10-----P5~6
查看>>
【托业】【新东方托业全真模拟】TEST07~08-----P5~6
查看>>
solver及其配置
查看>>
JAVA多线程之volatile 与 synchronized 的比较
查看>>
Java集合框架知识梳理
查看>>
笔试题(一)—— java基础
查看>>
Redis学习笔记(三)—— 使用redis客户端连接windows和linux下的redis并解决无法连接redis的问题
查看>>
Intellij IDEA使用(一)—— 安装Intellij IDEA(ideaIU-2017.2.3)并完成Intellij IDEA的简单配置
查看>>
Intellij IDEA使用(二)—— 在Intellij IDEA中配置JDK(SDK)
查看>>
Intellij IDEA使用(三)——在Intellij IDEA中配置Tomcat服务器
查看>>
Intellij IDEA使用(四)—— 使用Intellij IDEA创建静态的web(HTML)项目
查看>>
Intellij IDEA使用(五)—— Intellij IDEA在使用中的一些其他常用功能或常用配置收集
查看>>