linux篇01-grep awk sed常用操作

本文主要列举了grep/awk/sed命令的常用操作,关于这三个伙伴的参数太多太杂,可以在点击打开链接上面查看,这里我把自己常用的命令都贴上来。目前缺少awk。明天再总结下

(1)grep

 *格式:grep 参数 要过滤的词汇(支持正则) 文件1 [文件2...]
 *主要参数
*-n 打印出行号
*-c 计算过滤出的行的数量
*-i 忽略大小写
*-e 匹配多个词汇样式
*-r 递归
*-E 匹配多个词汇样式,与-e用法不同,详见示例
*-w 匹配完成的单词,注:词汇如gf1-scd1中间有分隔符,算两个词汇。详见示例
*-x 整行匹配

*-v 匹配不包含

(2)sed
*工作模式:
处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(patternspace),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重定向存储输出。Sed主要用来自动编辑一个或多个文件;简化对文件的反复操作;编写转换程序等。
 *格式:sed 选项 命令 文件1 [文件2...]
 *主要选项:
*-n 仅显示处理后的结果
*-e 允许一行内执行多个命令
*-i 直接编辑文本,将输出结果写入文本
 *主要参数
*s 替换
*d 删除 cat test.txt|sed '/^$/d'
*w 写入文件 sed -n 's/books/book/w demo' test.txt
 *替换标记
*p 打印替换的行
*g 如果一行内有两个以上的要替换的单词,则进行全面替换,默认是从每行的第一处开始替换,如果需要从第二处开始,则2g
 *已匹配字符串标记&

*&表示已匹配到的字符串自己

(3)awk

1.格式:awk 'BEGIN{ commands } pattern{ commands } END{ commands }'
执行步骤:
第一步:执行BEGIN{ commands }语句块中的语句;
第二步:从文件或标准输入(stdin)读取一行,然后执行pattern{commands}语句块,它逐行扫描文件,从第一行到最后一行重复这个过程,直到文件全部被读取完毕。
第三步:当读至输入流末尾时,执行END{ commands }语句块。
注:三部分每一部分都是可选的。
2.内置变量(先记下来)
$n 当前记录的第n个字段,比如n为1表示第一个字段,n为2表示第二个字段。 
$0 这个变量包含执行过程中当前行的文本内容。
[N] ARGC 命令行参数的数目。
[G] ARGIND 命令行中当前文件的位置(从0开始算)。
[N] ARGV 包含命令行参数的数组。
[G] CONVFMT 数字转换格式(默认值为%.6g)。
[P] ENVIRON 环境变量关联数组。
[N] ERRNO 最后一个系统错误的描述。
[G] FIELDWIDTHS 字段宽度列表(用空格键分隔)。
[A] FILENAME 当前输入文件的名。
[P] FNR 同NR,但相对于当前文件。
[A] FS 字段分隔符(默认是任何空格)。
[G] IGNORECASE 如果为真,则进行忽略大小写的匹配。
[A] NF 表示字段数,在执行过程中对应于当前的字段数。
[A] NR 表示记录数,在执行过程中对应于当前的行号。
[A] OFMT 数字的输出格式(默认值是%.6g)。
[A] OFS 输出字段分隔符(默认值是一个空格)。
[A] ORS 输出记录分隔符(默认值是一个换行符)。
[A] RS 记录分隔符(默认是一个换行符)。
[N] RSTART 由match函数所匹配的字符串的第一个位置。
[N] RLENGTH 由match函数所匹配的字符串的长度。
[N] SUBSEP 数组下标分隔符(默认值是34)。
这里面我平时使用的是$n,$0,FS,NR,和NF。其他供了解
3.示例:
cat demo.txt |awk -F, '{print $1}'
cat demo.txt | awk  'BEGIN{FS=","} {print $2}'
cat demo.txt | awk 'BEGIN{FS=","} {print $2} END{print "lets see"}'
cat demo.txt | awk '{print NR,NF}'

以下是在linux操作的示例,懒得摘了,直接贴上

[BEGIN] 2018/6/21 星期四 上午 8:46:00
// test1:/home/test/conf/msgbus % grep gf1  msg_node.conf
2:gf1-scd1=1
3:gf1-scd2=2
4:gf1-scd3=3
5:gf1-scd4=4
6:gf1-net1=5
7:gf1-net2=6
8:gf1-net3=7
9:gf1-net4=8
// test1:/home/test/conf/msgbus % grep -n gf1 msg_node.conf
2:gf1-scd1=1
3:gf1-scd2=2
4:gf1-scd3=3
5:gf1-scd4=4
6:gf1-net1=5
7:gf1-net2=6
8:gf1-net3=7
9:gf1-net4=8
// test1:/home/test/conf/msgbus % grep -c gf1 msg_node.conf
8
// test1:/home/test/conf/msgbus % grep -v gf1 msg_node.conf
1:[MSG_NODE]
// test1:/home/test/conf/msgbus % grep -E "scd1|scd2" msg_node.conf
2:gf1-scd1=1
3:gf1-scd2=2
// test1:/home/test/conf/msgbus % grep -w gf1-scd1 msg_node.conf
2:gf1-scd1=1
// test1:/home/test/conf/msgbus % grep -w gf msg_node.conf
// test1:/home/test/conf/msgbus % grep -w gf1 msg_node.conf
2:gf1-scd1=1
3:gf1-scd2=2
4:gf1-scd3=3
5:gf1-scd4=4
6:gf1-net1=5
7:gf1-net2=6
8:gf1-net3=7
9:gf1-net4=8
// test1:/home/test/conf/msgbus % grep -x gf1 msg_node.conf
// test1:/home/test/conf/msgbus % grep -x gf1-scd1=2 msg_node.conf
// test1:/home/test/conf/msgbus % grep -x gf1-scd1=1 msg_node.conf
2:gf1-scd1=1
// test1:/home/test/conf/msgbus % grep -x gf1-scd1 msg_node.conf
// test1:/home/test/conf/msgbus % grep -i msg msg_node.conf
1:[MSG_NODE]
// test1:/home/test/conf/msgbus % grep  msg msg_node.conf
// test1:/home/test/conf/msgbus % grep -e "scd1" -e "scd2" msg_node.conf
2:gf1-scd1=1
3:gf1-scd2=2
// test1:/home/test/conf/msgbus % sed -V
sed:无效选项 -- V
用法: sed [选项]... {脚本(如果没有其他脚本)} [输入文件]...

  -n, --quiet, --silent
                 取消自动打印模式空间
  -e 脚本, --expression=脚本
                 添加“脚本”到程序的运行列表
  -f 脚本文件, --file=脚本文件
                 添加“脚本文件”到程序的运行列表
  --follow-symlinks
                 直接修改文件时跟随软链接
  -i[SUFFIX], --in-place[=SUFFIX]
                 edit files in place (makes backup if SUFFIX supplied)
  -c, --copy
                 use copy instead of rename when shuffling files in -i mode
  -b, --binary
                 does nothing; for compatibility with WIN32/CYGWIN/MSDOS/EMX (
                 open files in binary mode (CR+LFs are not treated specially))
  -l N, --line-length=N
                 指定“l”命令的换行期望长度
  --posix
                 关闭所有 GNU 扩展
  -r, --regexp-extended
                 在脚本中使用扩展正则表达式
  -s, --separate
                 将输入文件视为各个独立的文件而不是一个长的连续输入
  -u, --unbuffered
                 从输入文件读取最少的数据,更频繁的刷新输出
  -z, --null-data
                 separate lines by NUL characters
  --help
                 display this help and exit
  --version
                 output version information and exit

如果没有 -e, --expression, -f 或 --file 选项,那么第一个非选项参数被视为
sed脚本。其他非选项参数被视为输入文件,如果没有输入文件,那么程序将从标准
输入读取数据。
GNU sed home page: <http://www.gnu.org/software/sed/>.
General help using GNU software: <http://www.gnu.org/gethelp/>.
// test1:/home/test/conf/msgbus % cd 
// gf1-scd1:/home/guofen % 
// gf1-scd1:/home/guofen % 
                                                                             
// test1:/home/test % cat test.txt 
book
this book is good
// test1:/home/test % sed 's/book/books/g' test.txt 
books
this books is good
// test1:/home/test % more test.txt 
book
this book is good
// test1:/home/test % vi test.txt 
book
this book is good

"test.txt" 4L, 35C 已写入                                                                                     
// test1:/home/test % sed 's/book/books/g' test.txt
books
this books is good

just do it
// test1:/home/test % sed -n 's/book/books/g' test.txt
// test1:/home/test % sed -n 's/book/books/p' test.txt
books
this books is good
// test1:/home/test % sed -n 's/book/books/gp' test.txt
books
this books is good
// test1:/home/test % vi test.txt 
book
this book is good

just do it
~
"test.txt" 4L, 55C 已写入                                                                                     
// test1:/home/test % sed -n 's/book/books/p' test.txt
books
this books is good,the book looks good
// test1:/home/test % sed -n 's/book/books/gp' test.txt
books
this books is good,the books looks good
// test1:/home/test % sed -n 's/book/books/w' test.txt
sed: 无法打开文件 : 没有那个文件或目录
// test1:/home/test % sed -n 's/book/books/w' test.txt ddd
sed: 无法打开文件 : 没有那个文件或目录
// test1:/home/test % sed -n -i  's/book/books/g' test.txt
// test1:/home/test % vi test.txt 
~
// test1:/home/test % vi test.txt
~
// test1:/home/test % ls -lrt
总用量 0
-rw-rw-r--. 1 guofen guofen 0 6月  21 09:20 test.txt
// test1:/home/test % vi test.txt 
~
"test.txt" 4L, 52C 已写入                                                                                     
// test1:/home/test % ls -lrt
总用量 4
-rw-rw-r--. 1 guofen guofen 52 6月  21 09:21 test.txt
// test1:/home/test % sed   's/book/books/g' test.txt
books
this is a books.the books looks good

just do it
// test1:/home/test % sed -n 's/book/books/g' test.txt
// test1:/home/test % sed -i 's/book/books/g' test.txt
// test1:/home/test % cat test.txt 
books
this is a books.the books looks good

just do it
// test1:/home/test % cat test.txt|sed '/^$/d' 
books
this is a books.the books looks good
just do it
// test1:/home/test % cat test.txt
books
this is a books.the books looks good

just do it
// test1:/home/test % sed -i 's/books/[&]/g' test.txt
// test1:/home/test % cat test.txt 
[books]
this is a [books].the [books] looks good

just do it
// test1:/home/test % sed  's/books/book/g' test.txt
[book]
this is a [book].the [book] looks good

just do it
// test1:/home/test % sed -n 's/books/book/g' test.txt
// test1:/home/test % sed -n 's/books/book/p' test.txt
[book]
this is a [book].the [books] looks good
// test1:/home/test % sed -n 's/books/book/w demo' test.txt
// test1:/home/test % ls -lrt
总用量 8
-rw-rw-r--. 1 guofen guofen 61 6月  21 09:29 test.txt
-rw-rw-r--. 1 guofen guofen 47 6月  21 09:36 demo
// test1:/home/test % more demo 
[book]
this is a [book].the [books] looks good

[END] 2018/6/21 星期四 上午 9:46:55

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值