Summary of redirection, piping, and grep commands in Linux

Source: Internet
Author: User
Tags stdin

今天我们来讲下Linux中的重定向、管道和grep命令。由于重定向和管道知识点比较少,但是又比较重要所以和grep命令一起讲。 在将重定向我们先讲下系统的标准输入和输出。在Linux中标准输入(STDIN 0)对应设备:键盘;标准输出(STDOUT 1)和标准错误输出(STDERR 2)对应设备:显示器。重定向就是将标准输入输出重新定位到指定位置或者设备中(比如:文件)。 I/O重定向 set –C 禁止对已存在的文件进行覆盖重定向 强制覆盖>| set +C 关闭上述功能 输出重定向: >:输出重定向,会进行覆盖操作(原数据会被覆盖) >>:追加输出重定向,不进行覆盖会进行追加输出 2>:错误输出重定向 2>>:错误追加输出重定向 &>:重定向标准输出或者错误输出至一个文件         输入重定向: <:输入重定向文本内容的重定向示例:“passwd --stdin user10 < var.out” 相同效果”cat var.out | passwd --stdin user10” <<:Here Document  cat << END(EOF) END为结束符      示例:# cat >> var.out << EOF      > This first line      > The second      > EOF          管道 管道是将前一个命令的输出作为后一个命令的输入 命令1|命令2|命令3 命令1的输出作为命令2的输入,命令2的输出作为命令3的输入    示例:cat var.out | passwd --stdin user10     密钥生成示例:echo "redhat" | openssl passwd -1-salt "12345678" –stdin   正则表达式grep 依据模式去匹配对应的文件并将匹配到的内容显示出来 grep [options] PATTERN [FILE...] PATTERN 文本字符和元字符组合而成的匹配条件 选项有 -A 显示匹配的后几行 -B 显示匹配的前几行 -C 显示匹配的前后几行 -E 使用扩展正则表达式 -i 匹配时忽略大小写 -o 只显示匹配的部分 -r 递归查找匹配   元字符 .:匹配任意单个字符 []:匹配指定范围内的任意单个字符 [^]:匹配指定范围外的任意单个字符 匹配次数(正则表达式工作在贪婪模式下) *:匹配前面的字符任意长度 \?:匹配前面的字符0或1次 \{n,m\}:匹配前面的字符至少n次,至多m次 \{n\}=\{n,n\} 位置锚定 ^:锚定在行数,匹配后面的内容出现在行首 $:锚定在行尾,匹配后面的内容出现在行尾 ^$ 空白行 单词中间没有出现特殊字符的字符串 \<(\b):单词的词首   \>(\b):单词的词尾 分组 \(\):将一系列的表达式作为一组   后面调用前面的表达式\1,\2 \1:表示第一个小括号以及其对应的右括号对应的所有内容   扩展表达式: 下面列出与上面不同的地方。 ?:表示前面的字符出现一次或零次 +:表示前面的字符至少出现一次 {n,m}:表示前面的字符至少出现n次,至多出现m次 ():表示分组 |:表示或者,匹配其中一个即可   结语:今天讲的东西看似不是很多,但是他十分重要,在后面的shell编程中会大量用到管道和正则表达方式,所以需要多加练习。


This article is from the "11342100" blog, please be sure to keep this source http://11352100.blog.51cto.com/11342100/1916306

Summary of redirection, piping, and grep commands in Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.