文本处理工具

时间:2017.11.21

作者:李强

参考:man,info,magedu讲义

声明:以下英文纯属个人翻译,英文B级,欢迎纠正,以下内容纯属个人理解,并没有对错,只是参考,盗版不纠,才能有限,希望不误人子弟为好。

1、 Output of entire files(整个文件内容的输出)

These commands read and write entire files, possibly transforming them in some way.

  • Menu:

  • cat :Concatenate and write files.
  • tac :Concatenate and write files in reverse.
  • nl :Number lines and write files.
  • od :Write files in octal or other formats.
  • base64 :Transform data into printable data.

cat:

NAME
       cat - concatenate files and print on the standard output

SYNOPSIS
       cat [OPTION]... [FILE]...

-n:显示行号
-b:不显示空行行号
-s:压缩连续的空行为一行
-A:显示所有控制符
-E:显示行结束符,可以用来检查空格tab等肉眼无法判断的字符。

tac:

NAME
       tac - concatenate and print files in reverse

SYNOPSIS
       tac [OPTION]... [FILE]...

nl:

NAME
       nl - number lines of files

SYNOPSIS
       nl [OPTION]... [FILE]...

rev:

NAME
     rev - reverse lines of a file or files

SYNOPSIS
     rev [file ...]

2、Summarizing files(整理文件)

  • wc :Print newline, word, and byte counts
  • sum :Print checksum and block counts
  • cksum :Print CRC checksum and byte counts
  • md5sum :Print or check MD5 digests
  • sha1sum :Print or check SHA-1 digests
  • sha2 :Print or check SHA-2 digests

wc:

NAME
       wc - print newline, word, and byte counts for each file

SYNOPSIS
       wc [OPTION]... [FILE]...
       wc [OPTION]... --files0-from=F

3、Operating on sorted files(对文件内容进行排序)

  • sort :Sort text files.
  • shuf :Shuffle text files.
  • uniq : Uniquify files.
  • comm :Compare two sorted files line by line.
  • ptx :Produce a permuted index of file contents.
  • tsort :Topological sort.

    sort:

NAME
       sort - sort lines of text files

SYNOPSIS
       sort [OPTION]... [FILE]...
       sort [OPTION]... --files0-from=F

-b --ignore-leading-blanks
-d --dictionary-order字典书序,仅考虑空格和字母数字
-f --ignore-case 忽略字母大小写
-g --general-numeric-sort按一般数值排序1-10 11 12
-i --ignore-noprinting 仅考虑打印字符,忽略非打印字符
-M --month-sort 以月份来排序,在月份排序的时候使用
-h --human-reading-numeric 以人类可读的数字,例如2k 1g ,在单位排序的时候可以使用
-n --numeric-sort
-R --random-sort
-r --reserve 反转排序

uniq:

NAME
       uniq - report or omit repeated lines

SYNOPSIS
       uniq [OPTION]... [INPUT [OUTPUT]]

4、Output of parts of files(文件部分的输出)

  • head : Output the first part of files
  • tail : Output the last part of files
  • split :Split a file into fixed-size pieces
  • csplit :Split a file into context-determined pieces

head:

    NAME
       head - output the first part of files

SYNOPSIS
       head [OPTION]... [FILE]...

-c:--bytes=[-]K 指定显示文件开头多少字节不是字符character,如果K前加dash(破折号),表示打印所有字符除了最后几个字符

-n:--lines=[-]K 指定显示文件开头多少行,如果K前加dash(破折号),表示打印所有除了最后几行

-#:指定从哪行开始显示就是-n的缩写,不写n了,建议最好写,容易混,比如当#为-1时,就必须用-n -1才行。

-q:--quiet,--silent ,当显示多个文件时,开头不显示文件名

-v: (默认)当显示多个文件时,开头显示文件名

当-c时,K可以加许多后缀:b 512, kB 1000, K 1024, MB 10001000, M 10241024, GB 100010001000, G 102410241024, T, P, E, Z, Y.等

tail:

 NAME
       tail - output the last part of files

SYNOPSIS
       tail [OPTION]... [FILE]...

-c:--bytes=[-]K 指定显示文件开头多少字节不是字符character,如果K前加dash(破折号),表示打印所有字符除了最后几个字符

-n:--lines=[-]K 指定显示文件开头多少行,如果K前加dash(破折号),表示打印所有除了最后几行

-#:指定从哪行开始显示就是-n的缩写,不写n了,建议最好写,容易混,比如当#为-1时,就必须用-n -1才行。

-q:--quiet,--silent ,当显示多个文件时,开头不显示文件名

-v: (默认)当显示多个文件时,开头显示文件名


以上和head一样,下面是tail的其他选项


--follow[=HOW]
--retry:主要用来配置--follow=name使用,当tail发现文件不存在了或者不可访问,它只会报告这个消息,不会继续监控文件尾部是否有追加内容了。
-f:(默认) 相当于 --follow=descriptor 跟踪显示文件fd新追加的内容,常用日志监控,但是如果文件已经不存在了或者被改名,他还是依然在监控。

-F:相当于 --follow=name --retry

--sleep-interval=NUMBER 轮询的时间,默认为1秒,可以修改

--pid=PID 例如make xxx >& makerr & tail --pid=$! -f makerr ,当编译结束时,pid结束,tail -f 也会自动结束追踪文件。

--max-unchanged-stats=N

5、Operating on fields(对字段的操作)

  • cut :Print selected parts of lines
  • paste :Merge lines of files
  • join :Join lines on a common field

    cut:

NAME
       cat - concatenate files and print on the standard output

SYNOPSIS
       cat [OPTION]... [FILE]...

With no FILE, or when FILE is -, read standard input.
-b --bytes=LIST
-c --characters=LIST
-d -delimiter=DELIM
-f --fields=LIST
-n 
--complement
-s 
LIST:n,n-,n-m.-m 区间,第n,第n-end 第n-第m,start-第m
DELIM(分隔符)
-c:
-n:

paste:

NAME
       paste - merge lines of files

SYNOPSIS
       paste [OPTION]... [FILE]...

默人按行把各个文件组合在组合在意,行与行之间默认用tab键作为分隔符
-d:--delimiters=LIST 用LIST中的分隔符,而不是使用默认的tab键,
-s:--serial 一次一个文件而不是每次一行来,就是一个文件的第一行和第二行用tab键来分隔然后和第三行tab键分隔,直到最后一样,就是把一个文件放到一行里面,几个文件就会生成几行,用管道传给cat -E -n 可以看得很清楚。可以用于什么呢?值得思考

组合使用

cat -n /etc/passwd | head -n 10 | tail -1 取第10行。



本文转自 lajifeiwomoshu 51CTO博客,原文链接:http://blog.51cto.com/lajifeiwomoshu/2045414

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值