天天看點

文本處理工具1、 Output of entire files(整個檔案内容的輸出)2、Summarizing files(整理檔案)3、Operating on sorted files(對檔案内容進行排序)4、Output of parts of files(檔案部分的輸出)5、Operating on fields(對字段的操作)組合使用

時間: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]...           

以上和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]...           

paste:

NAME
       paste - merge lines of files

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

組合使用

繼續閱讀