[第三章]文檔操作的命令-更新....

來源:互聯網
上載者:User

標籤:

一、針對文檔操作的命令
  • 一般對文檔操作的命令比較常用:cat、less、head、tail、grep、cut、sort、wc、uniq、tee、tr、split、sed、awk 其中 grep、sed、awk是Regex必須掌握的工具
(1) cut 命令
  • cut命令用來截取某一個欄位,其格式為:“cut -d ‘分隔字元’ [-cf] n” ,這裡的"n" 是數字,這個命令擁有以下選項
  •     -d        <==後面跟分割字元,分割字元要用單引號括起來
  •     -c        <==後面接的是第幾個字元
  •     -f         <==後面接的是第幾個區塊
  • -d選項後面加冒號作為分隔字元,“-f 1”表示截取第一段,“-f”和“1”之間的空格可有可無。
[[email protected] ~]# cat /etc/passwd | cut -d ‘:‘ -f 1 | head -n5        <==查看密碼檔案,截取第一段,顯示五列rootbindaemonadmlp[[email protected] ~]# head -n2 /etc/passwd | cut -d ‘:‘ -f 3,5,8        <==查看密碼檔案,截取第三、五、八段0:root1:bin
  • -c選項後面可以是1個數字n,也可以是一個區間n1-n2,還可以是多個數字n1、n2和n3
[[email protected] ~]# head -n2 /etc/passwd | cut -c2        <==列印密碼檔案的前兩行並截取前兩行的第二段oi[[email protected] ~]# head -n2 /etc/passwd | cut -c1        <==列印密碼檔案的前兩行並截取前兩行的第一段rb[[email protected] ~]# head -n2 /etc/passwd | cut -c1-10        <==列印密碼檔案的前兩行並截取前兩行的第一段到第十段root:x:0:0bin:x:1:1:[[email protected] ~]# head -n2 /etc/passwd | cut -c5-10        <==列印密碼檔案的前兩行並截取前兩行的第五段到第十段:x:0:0x:1:1:[[email protected] ~]# head -n2 /etc/passwd | cut -c -3           <==列印密碼檔案的前兩行並截取前兩行的前三段roobin[[email protected] ~]# head -n2 /etc/passwd | cut -c 3-            <==列印密碼檔案的前兩行並截取前兩行的第三到末尾ot:x:0:0:root:/root:/bin/bashn:x:1:1:bin:/bin:/sbin/nologin[[email protected] ~]# head -n2 /etc/passwd | cut -c3,5-8         <==列印密碼檔案的前兩行並截取前兩行的第三段和第五段到第八段o:x:0nx:1:
(2)soft命令
  • sort命令用來做排序,其格式為:“sort [-t 分隔字元] [-kn1,n2] [-nru]”,其選項如下
  •     -t        <==後面跟分隔字元,作用跟cut的-d選項一樣
  •     -n        <==表示使用純數字排序
  •     -r        <==表示反向排序
  •     -u        <==表示去重複
  •     -kn1,n2        <==表示由n1區間排序到n2區間,可以唯寫-kn1,即對n1欄位排序
  • 如果sort不加任何選項,則從首字元向後依次按ASCII碼進行比較,最後他們將升序輸出
[[email protected] ~]# head -n5 /etc/passwd |sort         <==列印/etc/passwd檔案的前5行並sort排序,最後以ASCII碼進行排序,以升序輸出adm:x:3:4:adm:/var/adm:/sbin/nologinbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinroot:x:0:0:root:/root:/bin/bash
  • -t選項後面跟分隔字元,-k選項後面跟單個數字表示對第幾個地區的字串排序 ,-n選項則表示使用純數字排序
[[email protected] ~]# head -n5 /etc/passwd | sort -t: -k3 -n  root:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
  • -r選項則反向排序
[[email protected] ~]# cat 123.txt                <==查看並列印 123.txt11111111111133333333333222222222222444444444444[[email protected] ~]# cat 123.txt | sort -r                <==查看123.txt並反向排序44444444444433333333333222222222222111111111111

 

(3)WC命令
  • wc命令用於統計文檔的行數、字元數或單詞數。其選項如下
  •     -l        <==統計行數
  •     -m       <==統計字元數
  •     -w        <==統計單詞數
  •     注釋:如果不加任何選項則會把行數、字元數、單詞數依次輸出
[[email protected] ~]# wc /etc/passwd                <==不加任何參數會把行數、字元數、單詞數依次輸出 21  27 942 /etc/passwd[[email protected] ~]# wc -l /etc/passwd                <==顯示/etc/passwd文檔中的行數21 /etc/passwd[[email protected] ~]# wc -m /etc/passwd                <==顯示/etc/passwd文檔中的字元數942 /etc/passwd[[email protected] ~]# wc -m /etc/passwd                <==顯示/etc/passwd文檔中的單詞數942 /etc/passwd
(4)uniq命令
  • uniq命令用來重複資料刪除的行,該命令只有-c選項比較有常用,它表示統計重複的行數,並把行數寫在最前面
[[email protected] ~]# uniq test.txt               <==不加任何選項,正常輸出 test.txt11112222333322223333[[email protected] ~]# sort test.txt | uniq               <==先進行sort 排序然後再去除重複的行111122223333[[email protected] ~]# sort test.txt | uniq -c            <==先進行sort 排序然後再去除重複的行,並顯示多少條重複數      1 1111      2 2222      2 3333

[第三章]文檔操作的命令-更新....

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.