3.14-19 wc、iconv、dos2unix、diff、vimdiff、rev
生活随笔
收集整理的這篇文章主要介紹了
3.14-19 wc、iconv、dos2unix、diff、vimdiff、rev
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
3.14 wc:統(tǒng)計(jì)文件的行數(shù)、單詞數(shù)或字節(jié)數(shù)
wc命令用于統(tǒng)計(jì)文件的行數(shù)、單詞數(shù)或字節(jié)數(shù)。 -c 統(tǒng)計(jì)字節(jié)數(shù) -w 統(tǒng)計(jì)單詞數(shù) -l 統(tǒng)計(jì)行數(shù)?? ? -L 打印最長(zhǎng)行的長(zhǎng)度 -m?統(tǒng)計(jì)字符數(shù) 查看文件的字節(jié)數(shù)、字?jǐn)?shù)、行數(shù)等 [root@cs7-sm ~]# wc /etc/inittab 17 80 511 /etc/inittab [root@cs7-sm ~]# wc /etc/inittab 17 80 511 /etc/inittab [root@cs7-sm ~]# wc -c /etc/inittab 511 /etc/inittab [root@cs7-sm ~]# wc -l /etc/inittab 17 /etc/inittab [root@cs7-sm ~]# wc -m /etc/inittab 511 /etc/inittab [root@cs7-sm ~]# wc -w /etc/inittab 80 /etc/inittab [root@cs7-sm ~]# wc -L /etc/inittab 86 /etc/inittab 選項(xiàng)-L的使用 [root@cs7-sm ~]# for word in I am oldboy teacher welcome to oldboy training class.;do [ `echo $word|wc -L` -le 6 ] && echo $word;done I am oldboy to oldboy class. 查看登錄系統(tǒng)的用戶數(shù) [root@cs7-sm ~]# who root pts/0 2019-05-19 16:42 (10.0.0.1) [root@cs7-sm ~]# who|wc -l 13.15 iconv:轉(zhuǎn)換文件的編碼格式
iconv命令用于轉(zhuǎn)換文件的編碼格式。 iconv [options] [-f from-encoding]? [-t to-encoding]?[inputfile] iconv [選項(xiàng)]? ? [原編碼]?? ??? ??? ??? [新編碼]?? ??? ?[輸入文件] -f? ?encodingA 從編碼A轉(zhuǎn)換 -l? ?顯示系統(tǒng)支持的編碼 -t? ?encodingB 轉(zhuǎn)換成編碼B -o?? 將輸出輸入到指定文件 [root@cs7-sm ~]# iconv -f gb2312 -t utf-8 gb2312.txt Hello World3.16 dos2unix:將DOS格式文件轉(zhuǎn)換成UNIX格式
將DOS(Windows系統(tǒng))格式文件轉(zhuǎn)換成UNIX格式(DOS/MAC to UNIX text file format converter)。DOS下的文本文件是以“\r\n”作為換行標(biāo)志的,而UNIX下的文本文件是以“\n”作為換行標(biāo)志的。所以在Linux中使用Windows的文本文件時(shí),常常會(huì)出現(xiàn)錯(cuò)誤。為了避免這種錯(cuò)誤,Linux提供了兩種文本格式相互轉(zhuǎn)化的命令:dos2unix和unix2dos,dos2unix把Windows文件的“\r\n”轉(zhuǎn)化成Linux文件的“\n”,unix2dos把Linux文件的“\n”轉(zhuǎn)化成Windows文件的“\r\n”。這個(gè)命令也挺好記的,dos to unix→dos 2(two→to)unix→dos2unix 處理由Windows 系統(tǒng)創(chuàng)建的腳本文件。 [root@cs7-sm ~]# cat test_win.sh #!/bin/bashexportfs_usage() {cat <<ENDUSAGE:$0 {start|stop|monitor|status|validate-all} END } exportfs_usage[root@cs7-sm ~]# bash test_win.sh test_win.sh: line 2: $'\r': command not found test_win.sh: line 3: syntax error near unexpected token `$'\r'' 'est_win.sh: line 3: `exportfs_usage() [root@cs7-sm ~]# cat -A test_win.sh #!/bin/bash^M$ ^M$ exportfs_usage()^M$ {^M$cat <<END^M$USAGE:$0 {start|stop|monitor|status|validate-all}^M$ END^M$ }^M$ exportfs_usage[root@cs7-sm ~]# dos2unix test_win.sh dos2unix: converting file test_win.sh to Unix format ... [root@cs7-sm ~]# cat -A test_win.sh #!/bin/bash$ $ exportfs_usage()$ {$cat <<END$USAGE:$0 {start|stop|monitor|status|validate-all}$ END$ }$ exportfs_usage[ro [root@cs7-sm ~]# cat -A test_win.sh #!/bin/bash$ $ exportfs_usage()$ {$cat <<END$USAGE:$0 {start|stop|monitor|status|validate-all}$ END$ }$ exportfs_usage[root@cs7-sm ~]# ^C [root@cs7-sm ~]# sh test_win.shUSAGE:test_win.sh {start|stop|monitor|status|validate-all}3.17 diff:比較兩個(gè)文件的不同
diff命令可以逐行比較純文本文件的內(nèi)容,并輸出文件的差異。 只能同時(shí)比較2個(gè)文件。 -y? 以并列的方式顯示文件的異同之處 -c? 使用上下文的輸出格式 -W? 在使用-y參數(shù)時(shí),指定顯示寬度 -u? 使用統(tǒng)一格式輸出 [root@cs7-sm ~]# cat >test1 1 2 3 4 5 6 [root@cs7-sm ~]# cat >test2 4 5 6 7 8 [root@cs7-sm ~]# diff test1 test2 1,3d0 < 1 < 2 < 3 6a4,5 > 7 > 8 以下是命令結(jié)果說明,diff默認(rèn)的顯示格式有如下三種提示。 a?? ?-add c?? ?-change d?? ?-delete 例如:在1,3d0和6a4,5中,字母d/a前面的數(shù)字是文本1的行號(hào),字母后面的是文本2的行號(hào)。其中以“<”打頭的行屬于文件1,以“>”打頭的行屬于文件2。 并排格式輸出 [root@cs7-sm ~]# diff -y test1 test2 #<==使用一參數(shù)就可以并排輸出。 1 < 2 < 3 < 4 4 5 5 6 6> 7> 8[root@cs7-sm ~]# diff -y -W 30 test1 test2 1 < 2 < 3 < 4 4 5 5 6 6> 7> 8 上下文輸出格式 [root@cs7-sm ~]# diff -c test1 test2 *** test1 2019-05-19 21:06:47.567530785 +0800 --- test2 2019-05-19 21:07:01.624530484 +0800 *************** *** 1,6 **** - 1 - 2 - 3456 --- 1,5 ----456 + 7 + 8"-"表示test2比test1少的行數(shù)。 "+"表示test2比test1多的行數(shù)。 統(tǒng)一格式輸出 [root@cs7-sm ~]# diff -u test1 test2 --- test1 2019-05-19 21:06:47.567530785 +0800 +++ test2 2019-05-19 21:07:01.624530484 +0800 @@ -1,6 +1,5 @@ -1 -2 -3 4 5 6 +7 +8 比較兩個(gè)目錄 [root@cs7-sm ~]# diff /etc/rc3.d/ /etc/rc6.d/ Only in /etc/rc6.d/: K90network Only in /etc/rc3.d/: S10network3.18 vimdiff:可視化比較工具
vimdiff調(diào)用vim打開文件,可以同時(shí)打開2個(gè)、3個(gè)或4個(gè)文件,最多4個(gè)文件,并且會(huì)以不同的顏色來區(qū)分文件的差異。 最多對(duì)比4個(gè)文件。3.19 rev:反向輸出文件內(nèi)容
rev命令可以按行反向輸出文件內(nèi)容。 字符串反轉(zhuǎn) [root@cs7-sm ~]# echo {1..10} 1 2 3 4 5 6 7 8 9 10 [root@cs7-sm ~]# echo {1..10}|rev 01 9 8 7 6 5 4 3 2 1 文本反轉(zhuǎn) [root@cs7-sm ~]# rev gb2312.txt 界世好你 ! dlroW olleH總結(jié)
以上是生活随笔為你收集整理的3.14-19 wc、iconv、dos2unix、diff、vimdiff、rev的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 3.21-22 od、tee
- 下一篇: 3.20 tr:替换或删除字符