Linux tail命令

Linux:tail 命令

  tail命令與head命令用法類似,tail命令用於查看文檔的尾端指定數量的字符塊,默認顯示文檔的最後 10 行,python

若是給定的文件不止一個,則在顯示的每一個文件前面加一個文件名標題。express

tail命令的幫助文檔ubuntu

DESCRIPTION
       Print  the  last  10  lines of each FILE to standard output.  With more than one FILE, precede each
       with a header giving the file name.

       With no FILE, or when FILE is -, read standard input.

       Mandatory arguments to long options are mandatory for short options too.

       -c, --bytes=[+]NUM
              output the last NUM bytes; or use -c +NUM to output starting with byte NUM of each file

       -f, --follow[={name|descriptor}]
              output appended data as the file grows;

              an absent option argument means 'descriptor'

       -F     same as --follow=name --retry

       -n, --lines=[+]NUM
              output the last NUM lines, instead of the last 10; or use -n +NUM to output starting with line NUM

       --max-unchanged-stats=N
              with --follow=name, reopen a FILE which has not

              changed  size after N (default 5) iterations to see if it has been unlinked or renamed (this
              is the usual case of rotated log files); with inotify, this option is rarely useful

二、命令選項app

-c, --bytes=K                    k,顯示文檔結尾的前 k 字節,+k,不顯示文檔開始的前 k-1 字節
-f, --follow[={name|descriptor}]     動態監視文檔最新追加的內容
-n, --lines=K                    k,顯示文檔結尾的 k 行,+k,不顯示文檔開始的前 k-1 行
-q, --quiet, --silent              當有多個文件參數時,不輸出各個文件名
-s, --sleep-interval=N              與「-f」選項連用,指定監視文件變化時間隔的秒數
--help	    	                 顯示此幫助信息並退出
--version                       顯示版本信息並退出

實例:post

  • 顯示  lines.txt 最後 5 行內容
ubuntu:~/test$ cat lines.txt 
01
02
03
04
05
06
07
08
09
10
11
12
ubuntu:~/test$ tail -5 lines.txt 
08
09
10
11
12
ubuntu:~/test$ 
  • 顯示 lines.txt 除了 前 9 行內容,從第10行開始
ubuntu:~/test$ tail +10 lines.txt
10
11
12
ubuntu:~/test$ 

Linux:awk命令 NR, FNR,NF

  NR表示從awk開始執行後,按照記錄分隔符讀取的數據次數,默認的記錄分隔符爲換行符,所以默認的就是讀取的數據行數,NR能夠理解爲Number of Record的縮寫。ui

  在awk處理多個輸入文件的時候,在處理完第一個文件後,NR並不會從1開始,而是繼續累加,所以就出現了FNR,每當處理一個新文件的時候,FNR就從1開始計數,FNR能夠理解爲File Number of Record。this

  NF表示目前的記錄被分割的字段的數目,NF能夠理解爲Number of Field。spa

Linux:cut命令詳解

  • 文件內容查看

  • 顯示行中的指定部分,刪除文件中指定字段

  • 顯示文件的內容,相似於下的type命令。

-b:僅顯示行中指定直接範圍的內容;

-c:僅顯示行中指定範圍的字符;

-d:指定字段的分隔符,默認的字段分隔符爲「TAB」;

-f:顯示指定字段的內容;

-n:與「-b」選項連用,不分割多字節字符;

--complement:補足被選擇的字節、字符或字段;

--out-delimiter=<字段分隔符>:指定輸出內容是的字段分割符;

--help:顯示指令的幫助信息;

--version:顯示指令的版本信息。

Linux:sed命令詳解

Linux sed 命令是利用腳原本處理文本文件。code

sed 可依照腳本的指令來處理、編輯文本文件。blog

Sed 主要用來自動編輯一個或多個文件、簡化對文件的反覆操做、編寫轉換程序等。

語法

sed [-hnV][-e<script>][-f<script文件>][文本文件]

參數說明

  • -e<script>或--expression=<script> 以選項中指定的script來處理輸入的文本文件。
  • -f<script文件>或--file=<script文件> 以選項中指定的script文件來處理輸入的文本文件。
  • -h或--help 顯示幫助。
  • -n或--quiet或--silent 僅顯示script處理後的結果。
  • -V或--version 顯示版本信息。

動做說明

  • a :新增, a 的後面能夠接字串,而這些字串會在新的一行出現(目前的下一行)~
  • c :取代, c 的後面能夠接字串,這些字串能夠取代 n1,n2 之間的行!
  • d :刪除,由於是刪除啊,因此 d 後面一般不接任何咚咚;
  • i :插入, i 的後面能夠接字串,而這些字串會在新的一行出現(目前的上一行);
  • p :打印,亦即將某個選擇的數據印出。一般 p 會與參數 sed -n 一塊兒運行~
  • s :取代,能夠直接進行取代的工做哩!一般這個 s 的動做能夠搭配正規表示法!例如 1,20s/old/new/g 就是啦!

 

給定一個文本文件 file.txt,請只打印這個文件中的第十行。

tail -n +10 file.txt | head -1
awk 'NR==10' file.txt
sed -n 10p file.txt
cat -n file.txt | grep -E "^[[:space:]]{4}10" | cut -f1 --complement
相關文章
相關標籤/搜索