- 浏览: 266078 次
- 性别:
- 来自: 苏州
文章分类
最新评论
-
di1984HIT:
这个是默认的。!
ElasticSearch (3) Java API -- put mapping to index -
di1984HIT:
谢谢,学习了~~~
ElasticSearch (3) Java API -- put mapping to index -
di1984HIT:
写的很好,谢谢啦
ElasticSearch (3) Java API -- put mapping to index -
swzzm:
.............文件加密了,密码了
Hive + Hbase
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
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
发表评论
-
Linux File descriptor limit
2012-11-01 13:38 30981. Find out what the current op ... -
grep 实用例子
2012-07-13 16:00 1574首先创建示例文件 $ cat demo_file THIS L ... -
sed系列:pattern buffer和hold buffer
2012-06-26 15:13 1390As its name implies, sed hold b ... -
sed系列:文件多行操作
2012-06-26 12:51 3449Sed reads line by line, removes ... -
sed系列:多命令执行
2012-06-22 17:01 5010Syntax: #sed -e 'command' -e 'c ... -
sed系列:行或者模式匹配删除特定行
2012-06-21 15:52 98487“p” command prints the buffer ( ... -
sed 系列: 分支操作
2012-06-21 11:47 1523无条件分支语法 $ sed ':label command(s ... -
sed 系列: 用正则表达式查找和替换
2012-06-19 20:55 33108The `s’ command is probably the ... -
sed 系列: 怎样用sed写文件
2012-06-19 20:24 2376In this article, let us review ... -
sed tool basic
2012-06-19 20:12 973语法 sed [options] '{command}' [f ... -
SSH password less login
2012-04-25 20:15 795http://www.thegeekstuff.com/200 ... -
Add environment variables for all users
2012-02-09 11:51 885/etc/profile example export ... -
Hadoop Cluster Setup
2012-01-13 16:11 433http://ankitasblogger.blogspot. ... -
linux下查找文件内容
2012-01-13 13:30 784#> find . -type f -exec gre ... -
FTP
2012-01-12 15:58 1003简单上传下载实例(/*.... ... -
CDH3
2012-01-05 11:46 1174CDH3 book O'Reilly's 'Hadoop ... -
Best Linux Books
2012-01-03 15:56 1387http://www.linuxlinks.com/artic ... -
Shell Command study
2012-01-03 15:25 799http://www.dedoimedo.com/comput ... -
GRUB
2012-01-03 12:22 838http://www.dedoimedo.com/comput ... -
linux 手动安装 oracle
2011-12-12 16:07 9446引用http://gohands.iteye.com/blog ...
相关推荐
处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并...
6. **脚本文件(Script File)**:复杂操作通常会编写`sed`脚本文件,用`-f script.sed`参数执行。脚本文件中可以包含多条命令,每行表示一个命令。 7. **其他高级特性**:包括使用`=`打印行号,`H`将活动缓冲区...
- **rfilename**: 读取指定文件的内容并追加到当前文件被模式匹配到的行后面。 - **wfilename**: 将模式空间中的内容保存到指定文件。 - **!**: 条件取反,通常用于模式后面,命令前面,如:`/pattern/!p`表示打印未...
`sed`(流编辑器,Stream Editor)是一个强大的文本处理工具,常用于对输入流(标准输入)或文件进行模式匹配和替换操作。`sedsed`在`sed`的基础上增加了一些特性,使得文本处理更加灵活和高效。 ### `sed`基础概念...
sed中的命令可以作用于特定的行或者行范围。这可以通过地址来指定,地址通常出现在命令之前,用空格分隔。例如: - `1`:指定第一行。 - `$`:指定最后一行。 - `/pattern/`:匹配正则表达式`pattern`的行。 - `...
- **非交互式**:sed不是交互式的编辑器,它不需要用户实时输入命令,而是通过命令行或者脚本文件来执行一系列预定义的编辑操作。 - **行编辑**:sed每次只处理一行文本,因此特别适合处理大型文件,因为它不需要将...
2. **匹配正则表达式**:使用`/pattern/`进行模式匹配。 3. **比较域**:通过比较两行进行操作。 4. **增加、附加、替换**:使用`a`、`i`和`s`命令进行文本添加、插入和替换。 5. **基本命令**:如`d`删除行,`p`...
它能够一次性处理文件中的一行内容,将每一行存储在一个临时区域,即“模式空间”(Pattern Space),然后通过一系列预定义的命令对这一行进行操作。操作完成后,该行内容会被输出到屏幕上,但原始文件内容不会被...
- **模式匹配**: 使用正则表达式来匹配行。 ##### 2.3 编辑指令 - **s**: 替换字符串。例如 `s/pattern/replacement/flags`。 - **d**: 删除行。 - **a**: 在匹配行之后添加文本。 - **i**: 在匹配行之前插入文本。...
`sed`一次处理一行内容,通过临时缓冲区(模式空间)进行操作,然后将处理结果输出。如果需要保存修改,通常需要重定向输出到新的文件。 ### Sed命令基本结构 `sed`命令的基本调用格式是: ```bash sed [options] ...
它能够按照脚本指令读取输入文件(标准输入或文本文件),对其进行模式匹配、替换、删除等操作,并将处理后的结果输出到标准输出或其他文件中。在日常工作中,`sed`常被用于自动化脚本编写、日志文件分析以及文本...
例如,你可以在处理大量日志文件时,用sed快速过滤出含有特定关键字的行,然后进行进一步分析。 总的来说,sed.exe是Windows环境下用于文本处理的一个实用工具,其强大的功能和灵活性使其成为开发者和系统管理员的...
Sed的强大之处在于它可以对文本进行模式匹配、替换、删除、插入等多种操作,并且可以实现批处理,非常适合处理大量文本文件。在本书中,你将了解到基本的sed命令语法,如使用正则表达式进行匹配,如何使用地址范围来...
地址可以是一个或两个数字,或者是一个模式,用于指定sed应该处理哪些行。例如: - `1`:只处理第一行。 - `/pattern/`:处理匹配特定模式的行。 - `1,3`:处理第一行到第三行。 **五、sed的调用** 基本的sed调用...
通过读取输入流(一个文件或管道)中的每一行,`sed`可以对文本进行模式匹配、替换、删除、插入等各种操作,并将处理结果输出到标准输出或重定向到其他文件。`sed`的工作方式是逐行处理输入,使得它非常适合对大量...
Sed擅长于进行模式匹配和替换操作,它通过正则表达式实现复杂的文本查找和替换功能,无需编写完整的程序就能完成许多文本处理任务。 Awk,全称为Aho, Weinberger, and Kernighan,是一种高级的数据处理语言。它的...
1. **模式匹配与替换**:使用正则表达式来查找和替换文本。例如,`sed 's/old/new/g'` 命令会将所有出现的 "old" 替换为 "new"。 2. **行操作**:可以删除、插入、追加文本。例如,`sed '/pattern/d'` 会删除所有...
sed的编辑操作基于这些空间进行,并且这些操作可以在命令行或者脚本文件中定义。 #### 二、使用sed **2.1 执行命令列上的编辑指令** 在命令行上直接使用sed进行文本编辑非常方便。例如,如果你想将文件`file.txt`...
`sed`的工作原理是读取输入文件的一行并将其放入模式空间,然后对模式空间中的内容进行一系列指定的操作,最后将处理结果输出到屏幕,然后继续处理下一行,直到文件结束。 `sed`命令的基本格式如下: ```bash sed ...