天天看點

linux wc 修改檔案,linux的wc -l 指令統計檔案少一行(一般是windows檔案)

先簡單介紹

wc(Word Count)指令的功能為統計指定檔案中的位元組數、字數、行數,并将統計結果顯示輸出

格式:wc file

指令參數:

-c統計Bytes數(位元組數),并顯示檔案名

-l統計行數:使用換行符‘’作為行結束标志,實際是統計換行符個數

-m統計字元數。這個标志不能與-c标志一起使用。

-w統計字數。一個字被定義為由空白、跳格或換行字元分隔的字元串。

-L列印最長行的長度。

-help顯示幫助資訊

--version顯示版本資訊

執行個體:

wc test.txt

6  24 132test.txt

預設輸出:行,字數,位元組數

test.txt内容

Cat test.txt

test1 name1 age1 sex1

test2 name2 age2 sex2

test3 name3 age3 sex3

test4 name4 age4 sex4

test5 name5 age5 sex5

test6 name6 age6 sex6

問題:wc 統計行數時少一行:

因為wc –l是按

作為行結束符統計行數,是以最後一行如果沒有

的話會統計丢失。

執行個體:比如,在windows下生成同上面test.txt相同的檔案testtt.txt,上傳到linux下:

cat testtt.txt

test1 name1 age1 sex1

test2 name2 age2 sex2

test3 name3 age3 sex3

test4 name4 age4 sex4

test5 name5 age5 sex5

test6 name6 age6 sex6[[email protected] lmj]$

可以看出結尾有點奇怪。這是因為檔案末尾無

,而是直接用了檔案結束符EOF。這樣檔案使用wc統計就會少一行:

wc -l testtt.txt

5 24 136 testtt.txt

使用管道也不行:

cat testtt.txt | wc -l

5

為什麼linux下沒有這樣的問題?

因為vim編輯器會自動在檔案結尾加上

,在加上檔案結束符EOF。(linux下文本檔案主要按處理,是以vim會末行自動加

)

而對windows檔案用dos2unix轉化也不行:

[[email protected] lmj]$ dos2unix testtt.txt

dos2unix: converting file testtt.txt toUNIX format ...

[[email protected] lmj]$ wc testtt.txt

5  24131 testtt.txt

可以看出windows檔案在linux下還是有相容問題的。檔案字數沒變24,byte數少5個是windows下行結束符是回車+換行

。而linux下隻是換行

Vim二進制可以看到不同,

顯示為.,檔案結尾沒有