Linux Shell 文本处理工具集锦

Standard

本文将介绍Linux下使用Shell处理文本时最常用的工具:
find、grep、xargs、sort、uniq、tr、cut、paste、wc、sed、awk;
提供的例子和参数都是最常用和最为实用的;
我对shell脚本使用的原则是命令单行书写,尽量不要超过2行;
如果有更为复杂的任务需求,还是考虑python吧;

find 文件查找

  • 查找txt和pdf文件
    <code><span class="pln">  find </span><span class="pun">.</span><span class="pln"> \( </span><span class="pun">-</span><span class="pln">name </span><span class="str">"*.txt"</span> <span class="pun">-</span><span class="pln">o </span><span class="pun">-</span><span class="pln">name </span><span class="str">"*.pdf"</span><span class="pln"> \) </span><span class="pun">-</span><span class="kwd">print</span></code>
  • 正则方式查找.txt和pdf
    <code><span class="pln">  find </span><span class="pun">.</span> <span class="pun">-</span><span class="pln">regex  </span><span class="str">".*\(\.txt|\.pdf\)$"</span></code>

    -iregex: 忽略大小写的正则

  • 否定参数
    查找所有非txt文本

    <code><span class="pln">   find </span><span class="pun">.</span> <span class="pun">!</span> <span class="pun">-</span><span class="pln">name </span><span class="str">"*.txt"</span> <span class="pun">-</span><span class="kwd">print</span></code>
  • 指定搜索深度
    打印出当前目录的文件(深度为1)

    <code><span class="pln">  find </span><span class="pun">.</span> <span class="pun">-</span><span class="pln">maxdepth </span><span class="lit">1</span> <span class="pun">-</span><span class="pln">type f  </span></code>

定制搜索

  • 按类型搜索:
    <code><span class="pln">  find </span><span class="pun">.</span> <span class="pun">-</span><span class="pln">type d </span><span class="pun">-</span><span class="kwd">print</span>  <span class="com">//只列出所有目录</span></code>

    -type f 文件 / l 符号链接

  • 按时间搜索:
    -atime 访问时间 (单位是天,分钟单位则是-amin,以下类似)
    -mtime 修改时间 (内容被修改)
    -ctime 变化时间 (元数据或权限变化)
    最近7天被访问过的所有文件:

    <code><span class="pln">  find </span><span class="pun">.</span> <span class="pun">-</span><span class="pln">atime </span><span class="lit">7</span> <span class="pun">-</span><span class="pln">type f </span><span class="pun">-</span><span class="kwd">print</span></code>
  • 按大小搜索:
    w字 k M G
    寻找大于2k的文件

    <code><span class="pln">  find </span><span class="pun">.</span> <span class="pun">-</span><span class="pln">type f </span><span class="pun">-</span><span class="pln">size </span><span class="pun">+</span><span class="lit">2k</span></code>

    按权限查找:

    <code><span class="pln">  find </span><span class="pun">.</span> <span class="pun">-</span><span class="pln">type f </span><span class="pun">-</span><span class="pln">perm </span><span class="lit">644</span> <span class="pun">-</span><span class="kwd">print</span> <span class="com">//找具有可执行权限的所有文件</span></code>

    按用户查找:

    <code><span class="pln">  find </span><span class="pun">.</span> <span class="pun">-</span><span class="pln">type f </span><span class="pun">-</span><span class="pln">user weber </span><span class="pun">-</span><span class="kwd">print</span><span class="com">// 找用户weber所拥有的文件</span></code>

找到后的后续动作

  • 删除:
    删除当前目录下所有的swp文件:

    <code><span class="pln">  find </span><span class="pun">.</span> <span class="pun">-</span><span class="pln">type f </span><span class="pun">-</span><span class="pln">name </span><span class="str">"*.swp"</span> <span class="pun">-</span><span class="kwd">delete</span></code>
  • 执行动作(强大的exec)
    <code><span class="pln">  find </span><span class="pun">.</span> <span class="pun">-</span><span class="pln">type f </span><span class="pun">-</span><span class="pln">user root </span><span class="pun">-</span><span class="kwd">exec</span><span class="pln"> chown weber </span><span class="pun">{}</span><span class="pln"> \; </span><span class="com">//将当前目录下的所有权变更为weber</span></code>

    注:{}是一个特殊的字符串,对于每一个匹配的文件,{}会被替换成相应的文件名;
    eg:将找到的文件全都copy到另一个目录:

    <code><span class="pln">  find </span><span class="pun">.</span> <span class="pun">-</span><span class="pln">type f </span><span class="pun">-</span><span class="pln">mtime </span><span class="pun">+</span><span class="lit">10</span> <span class="pun">-</span><span class="pln">name </span><span class="str">"*.txt"</span> <span class="pun">-</span><span class="kwd">exec</span><span class="pln"> cp </span><span class="pun">{}</span><span class="pln"> OLD \;</span></code>
  • 结合多个命令
    tips: 如果需要后续执行多个命令,可以将多个命令写成一个脚本。然后 -exec 调用时执行脚本即可;

    <code>  <span class="pun">-</span><span class="kwd">exec</span> <span class="pun">./</span><span class="pln">commands</span><span class="pun">.</span><span class="pln">sh </span><span class="pun">{}</span><span class="pln"> \;</span></code>

-print的定界符

默认使用’\n’作为文件的定界符;
-print0 使用’\0’作为文件的定界符,这样就可以搜索包含空格的文件;

grep 文本搜索

grep match_patten file // 默认访问匹配行

  • 常用参数
    -o 只输出匹配的文本行 VS -v 只输出没有匹配的文本行
    -c 统计文件中包含文本的次数

    <code><span class="pln">  grep </span><span class="pun">-</span><span class="pln">c </span><span class="str">"text"</span><span class="pln"> filename</span></code>

    -n 打印匹配的行号
    -i 搜索时忽略大小写
    -l 只打印文件名

  • 在多级目录中对文本递归搜索(程序员搜代码的最爱):
    <code><span class="pln">  grep </span><span class="str">"class"</span> <span class="pun">.</span> <span class="pun">-</span><span class="pln">R </span><span class="pun">-</span><span class="pln">n</span></code>
  • 匹配多个模式
    <code><span class="pln">  grep </span><span class="pun">-</span><span class="pln">e </span><span class="str">"class"</span> <span class="pun">-</span><span class="pln">e </span><span class="str">"vitural"</span><span class="pln"> file</span></code>
  • grep输出以\0作为结尾符的文件名:(-z)
    <code><span class="pln">  grep </span><span class="str">"test"</span><span class="pln"> file</span><span class="pun">*</span> <span class="pun">-</span><span class="pln">lZ</span><span class="pun">|</span><span class="pln"> xargs </span><span class="pun">-</span><span class="lit">0</span><span class="pln"> rm</span></code>

xargs 命令行参数转换

xargs 能够将输入数据转化为特定命令的命令行参数;这样,可以配合很多命令来组合使用。比如grep,比如find;

  • 将多行输出转化为单行输出
    cat file.txt| xargs
    \n 是多行文本间的定界符
  • 将单行转化为多行输出
    cat single.txt | xargs -n 3
    -n:指定每行显示的字段数

xargs参数说明

-d 定义定界符 (默认为空格 多行的定界符为 \n)
-n 指定输出为多行
-I {} 指定替换字符串,这个字符串在xargs扩展时会被替换掉,用于待执行的命令需要多个参数时
eg:

<code><span class="pln">cat file</span><span class="pun">.</span><span class="pln">txt </span><span class="pun">|</span><span class="pln"> xargs </span><span class="pun">-</span><span class="pln">I </span><span class="pun">{}</span> <span class="pun">./</span><span class="pln">command</span><span class="pun">.</span><span class="pln">sh </span><span class="pun">-</span><span class="pln">p </span><span class="pun">{}</span> <span class="pun">-</span><span class="lit">1</span></code>

-0:指定\0为输入定界符
eg:统计程序行数

<code><span class="pln">find source_dir</span><span class="pun">/</span> <span class="pun">-</span><span class="pln">type f </span><span class="pun">-</span><span class="pln">name </span><span class="str">"*.cpp"</span> <span class="pun">-</span><span class="pln">print0 </span><span class="pun">|</span><span class="pln">xargs </span><span class="pun">-</span><span class="lit">0</span><span class="pln"> wc </span><span class="pun">-</span><span class="pln">l</span></code>

sort 排序

字段说明:
-n 按数字进行排序 VS -d 按字典序进行排序
-r 逆序排序
-k N 指定按第N列排序
eg:

<code><span class="pln">sort </span><span class="pun">-</span><span class="pln">nrk </span><span class="lit">1</span><span class="pln"> data</span><span class="pun">.</span><span class="pln">txt
sort </span><span class="pun">-</span><span class="pln">bd data </span><span class="com">// 忽略像空格之类的前导空白字符</span></code>

uniq 消除重复行

  • 消除重复行
    <code><span class="pln">  sort unsort</span><span class="pun">.</span><span class="pln">txt </span><span class="pun">|</span><span class="pln"> uniq </span></code>
  • 统计各行在文件中出现的次数
    <code><span class="pln">  sort unsort</span><span class="pun">.</span><span class="pln">txt </span><span class="pun">|</span><span class="pln"> uniq </span><span class="pun">-</span><span class="pln">c</span></code>
  • 找出重复行
    <code><span class="pln">  sort unsort</span><span class="pun">.</span><span class="pln">txt </span><span class="pun">|</span><span class="pln"> uniq </span><span class="pun">-</span><span class="pln">d</span></code>

    可指定每行中需要比较的重复内容:-s 开始位置 -w 比较字符数

用tr进行转换

  • 通用用法
    <code><span class="pln">  echo </span><span class="lit">12345</span> <span class="pun">|</span><span class="pln"> tr </span><span class="str">'0-9'</span> <span class="str">'9876543210'</span> <span class="com">//加解密转换,替换对应字符</span><span class="pln">
      cat text</span><span class="pun">|</span><span class="pln"> tr </span><span class="str">'\t'</span> <span class="str">' '</span>  <span class="com">//制表符转空格</span></code>
  • tr删除字符
    <code><span class="pln">  cat file </span><span class="pun">|</span><span class="pln"> tr </span><span class="pun">-</span><span class="pln">d </span><span class="str">'0-9'</span> <span class="com">// 删除所有数字</span></code>

    -c 求补集

    <code><span class="pln">  cat file </span><span class="pun">|</span><span class="pln"> tr </span><span class="pun">-</span><span class="pln">c </span><span class="str">'0-9'</span> <span class="com">//获取文件中所有数字</span><span class="pln">
      cat file </span><span class="pun">|</span><span class="pln"> tr </span><span class="pun">-</span><span class="pln">d </span><span class="pun">-</span><span class="pln">c </span><span class="str">'0-9 \n'</span>  <span class="com">//删除非数字数据</span></code>
  • tr压缩字符
    tr -s 压缩文本中出现的重复字符;最常用于压缩多余的空格

    <code><span class="pln">  cat file </span><span class="pun">|</span><span class="pln"> tr </span><span class="pun">-</span><span class="pln">s </span><span class="str">' '</span></code>
  • 字符类
    tr中可用各种字符类:
    alnum:字母和数字
    alpha:字母
    digit:数字
    space:空白字符
    lower:小写
    upper:大写
    cntrl:控制(非可打印)字符
    print:可打印字符
    使用方法:tr [:class:] [:class:]

    <code><span class="pln">  eg</span><span class="pun">:</span><span class="pln"> tr </span><span class="str">'[:lower:]'</span> <span class="str">'[:upper:]'</span></code>

cut 按列切分文本

  • 截取文件的第2列和第4列:
    <code><span class="pln">  cut </span><span class="pun">-</span><span class="pln">f2</span><span class="pun">,</span><span class="lit">4</span><span class="pln"> filename</span></code>
  • 去文件除第3列的所有列:
    <code><span class="pln">  cut </span><span class="pun">-</span><span class="pln">f3 </span><span class="pun">--</span><span class="pln">complement filename</span></code>
  • -d 指定定界符:
    <code><span class="pln">  cat </span><span class="pun">-</span><span class="pln">f2 </span><span class="pun">-</span><span class="pln">d</span><span class="str">";"</span><span class="pln"> filename</span></code>
  • cut 取的范围
    N- 第N个字段到结尾
    -M 第1个字段为M
    N-M N到M个字段
  • cut 取的单位
    -b 以字节为单位
    -c 以字符为单位
    -f 以字段为单位(使用定界符)
  • eg:
    <code><span class="pln">  cut </span><span class="pun">-</span><span class="pln">c1</span><span class="pun">-</span><span class="lit">5</span><span class="pln"> file </span><span class="com">//打印第一到5个字符</span><span class="pln">
      cut </span><span class="pun">-</span><span class="pln">c</span><span class="pun">-</span><span class="lit">2</span><span class="pln"> file  </span><span class="com">//打印前2个字符</span></code>

paste 按列拼接文本

将两个文本按列拼接到一起;

<code><span class="pln">cat file1
</span><span class="lit">1</span>
<span class="lit">2</span><span class="pln">

cat file2
colin
book

paste file1 file2
</span><span class="lit">1</span><span class="pln"> colin
</span><span class="lit">2</span><span class="pln"> book</span></code>

默认的定界符是制表符,可以用-d指明定界符
paste file1 file2 -d “,”
1,colin
2,book

wc 统计行和字符的工具

wc -l file // 统计行数
wc -w file // 统计单词数
wc -c file // 统计字符数

sed 文本替换利器

  • 首处替换
    <code><span class="pln">  seg </span><span class="str">'s/text/replace_text/'</span><span class="pln"> file   </span><span class="com">//替换每一行的第一处匹配的text</span></code>
  • 全局替换
    <code><span class="pln">   seg </span><span class="str">'s/text/replace_text/g'</span><span class="pln"> file</span></code>

    默认替换后,输出替换后的内容,如果需要直接替换原文件,使用-i:

    <code><span class="pln">  seg </span><span class="pun">-</span><span class="pln">i </span><span class="str">'s/text/repalce_text/g'</span><span class="pln"> file</span></code>
  • 移除空白行:
    <code><span class="pln">  sed </span><span class="str">'/^$/d'</span><span class="pln"> file</span></code>
  • 变量转换
    已匹配的字符串通过标记&来引用.

    <code><span class="pln">echo </span><span class="kwd">this</span> <span class="kwd">is</span><span class="pln"> en example </span><span class="pun">|</span><span class="pln"> seg </span><span class="str">'s/\w+/[&amp;]/g'</span><span class="pln">
    $</span><span class="pun">&gt;[</span><span class="kwd">this</span><span class="pun">]</span>  <span class="pun">[</span><span class="kwd">is</span><span class="pun">]</span> <span class="pun">[</span><span class="pln">en</span><span class="pun">]</span> <span class="pun">[</span><span class="pln">example</span><span class="pun">]</span></code>
  • 子串匹配标记
    第一个匹配的括号内容使用标记 \1 来引用

    <code><span class="pln">  sed </span><span class="str">'s/hello\([0-9]\)/\1/'</span></code>
  • 双引号求值
    sed通常用单引号来引用;也可使用双引号,使用双引号后,双引号会对表达式求值:

    <code><span class="pln">  sed </span><span class="str">'s/$var/HLLOE/'</span> </code>

    当使用双引号时,我们可以在sed样式和替换字符串中指定变量;

    <code><span class="pln">eg</span><span class="pun">:</span><span class="pln">
    p</span><span class="pun">=</span><span class="pln">patten
    r</span><span class="pun">=</span><span class="pln">replaced
    echo </span><span class="str">"line con a patten"</span> <span class="pun">|</span><span class="pln"> sed </span><span class="str">"s/$p/$r/g"</span><span class="pln">
    $</span><span class="pun">&gt;</span><span class="pln">line con a replaced</span></code>
  • 其它示例
    字符串插入字符:将文本中每行内容(PEKSHA) 转换为 PEK/SHA

    <code><span class="pln">  sed </span><span class="str">'s/^.\{3\}/&amp;\//g'</span><span class="pln"> file</span></code>

awk 数据流处理工具

  • awk脚本结构
    awk ‘ BEGIN{ statements } statements2 END{ statements } ‘
  • 工作方式
    1.执行begin中语句块;
    2.从文件或stdin中读入一行,然后执行statements2,重复这个过程,直到文件全部被读取完毕;
    3.执行end语句块;
  • 使用不带参数的print时,会打印当前行;
    <code><span class="pln">  echo </span><span class="pun">-</span><span class="pln">e </span><span class="str">"line1\nline2"</span> <span class="pun">|</span><span class="pln"> awk </span><span class="str">'BEGIN{print "start"} {print } END{ print "End" }'</span> </code>
  • print 以逗号分割时,参数以空格定界;
    <code><span class="pln">echo </span><span class="pun">|</span><span class="pln"> awk </span><span class="str">' {var1 = "v1" ; var2 = "V2"; var3="v3"; \
    print var1, var2 , var3; }'</span><span class="pln">
    $</span><span class="pun">&gt;</span><span class="pln">v1 V2 v3</span></code>
  • 使用-拼接符的方式(””作为拼接符);
    <code><span class="pln">echo </span><span class="pun">|</span><span class="pln"> awk </span><span class="str">' {var1 = "v1" ; var2 = "V2"; var3="v3"; \
    print var1"-"var2"-"var3; }'</span><span class="pln">
    $</span><span class="pun">&gt;</span><span class="pln">v1</span><span class="pun">-</span><span class="pln">V2</span><span class="pun">-</span><span class="pln">v3</span></code>

特殊变量: NR NF $0 $1 $2

NR:表示记录数量,在执行过程中对应当前行号;
NF:表示字段数量,在执行过程总对应当前行的字段数;
$0:这个变量包含执行过程中当前行的文本内容;
$1:第一个字段的文本内容;
$2:第二个字段的文本内容;

<code><span class="pln">echo </span><span class="pun">-</span><span class="pln">e </span><span class="str">"line1 f2 f3\n line2 \n line 3"</span> <span class="pun">|</span><span class="pln"> awk </span><span class="str">'{print NR":"$0"-"$1"-"$2}'</span></code>
  • 打印每一行的第二和第三个字段:
    <code><span class="pln">  awk </span><span class="str">'{print $2, $3}'</span><span class="pln"> file</span></code>
  • 统计文件的行数:
    <code><span class="pln">  awk </span><span class="str">' END {print NR}'</span><span class="pln"> file</span></code>
  • 累加每一行的第一个字段:
    <code><span class="pln">  echo </span><span class="pun">-</span><span class="pln">e </span><span class="str">"1\n 2\n 3\n 4\n"</span> <span class="pun">|</span><span class="pln"> awk </span><span class="str">'BEGIN{num = 0 ;
      print "begin";} {sum += $1;} END {print "=="; print sum }'</span></code>

传递外部变量

<code><span class="kwd">var</span><span class="pun">=</span><span class="lit">1000</span><span class="pln">
echo </span><span class="pun">|</span><span class="pln"> awk </span><span class="str">'{print vara}'</span><span class="pln"> vara</span><span class="pun">=</span><span class="pln">$var </span><span class="com">#  输入来自stdin</span><span class="pln">
awk </span><span class="str">'{print vara}'</span><span class="pln"> vara</span><span class="pun">=</span><span class="pln">$var file </span><span class="com"># 输入来自文件</span></code>

用样式对awk处理的行进行过滤

awk ‘NR < 5’ #行号小于5
awk ‘NR==1,NR==4 {print}’ file #行号等于1和4的打印出来
awk ‘/linux/’ #包含linux文本的行(可以用正则表达式来指定,超级强大)
awk ‘!/linux/’ #不包含linux文本的行

设置定界符

使用-F来设置定界符(默认为空格)
awk -F: ‘{print $NF}’ /etc/passwd

读取命令输出

使用getline,将外部shell命令的输出读入到变量cmdout中;

<code><span class="pln">echo </span><span class="pun">|</span><span class="pln"> awk </span><span class="str">'{"grep root /etc/passwd" | getline cmdout; print cmdout }'</span> </code>

在awk中使用循环

for(i=0;i<10;i++){print $i;}
for(i in array){print array[i];}

eg:
以逆序的形式打印行:(tac命令的实现)

<code><span class="pln">seq </span><span class="lit">9</span><span class="pun">|</span><span class="pln"> \
awk </span><span class="str">'{lifo[NR] = $0; lno=NR} \
END{ for(;lno&gt;-1;lno--){print lifo[lno];}
} '</span></code>

awk实现head、tail命令

  • head:
    <code><span class="pln">  awk </span><span class="str">'NR&lt;=10{print}'</span><span class="pln"> filename</span></code>
  • tail:
    <code><span class="pln">  awk </span><span class="str">'{buffer[NR%10] = $0;} END{for(i=0;i&lt;11;i++){ \
      print buffer[i %10]} } '</span><span class="pln"> filename</span></code>

打印指定列

  • awk方式实现:
    <code><span class="pln">  ls </span><span class="pun">-</span><span class="pln">lrt </span><span class="pun">|</span><span class="pln"> awk </span><span class="str">'{print $6}'</span></code>
  • cut方式实现
    <code><span class="pln">  ls </span><span class="pun">-</span><span class="pln">lrt </span><span class="pun">|</span><span class="pln"> cut </span><span class="pun">-</span><span class="pln">f6</span></code>

打印指定文本区域

  • 确定行号
    <code><span class="pln">  seq </span><span class="lit">100</span><span class="pun">|</span><span class="pln"> awk </span><span class="str">'NR==4,NR==6{print}'</span></code>
  • 确定文本
    打印处于start_pattern 和end_pattern之间的文本;

    <code><span class="pln">  awk </span><span class="str">'/start_pattern/, /end_pattern/'</span><span class="pln"> filename</span></code>

    eg:

    <code><span class="pln">seq </span><span class="lit">100</span> <span class="pun">|</span><span class="pln"> awk </span><span class="str">'/13/,/15/'</span><span class="pln">
    cat </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">passwd</span><span class="pun">|</span><span class="pln"> awk </span><span class="str">'/mai.*mail/,/news.*news/'</span></code>

awk常用内建函数

index(string,search_string):返回search_string在string中出现的位置
sub(regex,replacement_str,string):将正则匹配到的第一处内容替换为replacement_str;
match(regex,string):检查正则表达式是否能够匹配字符串;
length(string):返回字符串长度

<code><span class="pln">echo </span><span class="pun">|</span><span class="pln"> awk </span><span class="str">'{"grep root /etc/passwd" | getline cmdout; print length(cmdout) }'</span> </code>

printf 类似c语言中的printf,对输出进行格式化
eg:

<code><span class="pln">seq </span><span class="lit">10</span> <span class="pun">|</span><span class="pln"> awk </span><span class="str">'{printf "-&gt;%4s\n", $1}'</span></code>

迭代文件中的行、单词和字符

1. 迭代文件中的每一行

  • while 循环法
    <code><span class="kwd">while</span><span class="pln"> read line</span><span class="pun">;</span>
    <span class="kwd">do</span><span class="pln">
    echo $line</span><span class="pun">;</span>
    <span class="kwd">done</span> <span class="pun">&lt;</span><span class="pln"> file</span><span class="pun">.</span><span class="pln">txt
    </span><span class="pun">改成子</span><span class="pln">shell</span><span class="pun">:</span><span class="pln">
    cat file</span><span class="pun">.</span><span class="pln">txt </span><span class="pun">|</span> <span class="pun">(</span><span class="kwd">while</span><span class="pln"> read line</span><span class="pun">;</span><span class="kwd">do</span><span class="pln"> echo $line</span><span class="pun">;</span><span class="kwd">done</span><span class="pun">)</span></code>
  • awk法:
    cat file.txt| awk ‘{print}’

2.迭代一行中的每一个单词

<code><span class="kwd">for</span><span class="pln"> word </span><span class="kwd">in</span><span class="pln"> $line</span><span class="pun">;</span>
<span class="kwd">do</span><span class="pln"> 
echo $word</span><span class="pun">;</span>
<span class="kwd">done</span></code>

3. 迭代每一个字符

${string:start_pos:num_of_chars}:从字符串中提取一个字符;(bash文本切片)
${#word}:返回变量word的长度

<code><span class="kwd">for</span><span class="pun">((</span><span class="pln">i</span><span class="pun">=</span><span class="lit">0</span><span class="pun">;</span><span class="pln">i</span><span class="pun">&lt;</span><span class="pln">$</span><span class="pun">{#</span><span class="pln">word</span><span class="pun">};</span><span class="pln">i</span><span class="pun">++))</span>
<span class="kwd">do</span><span class="pln">
echo $</span><span class="pun">{</span><span class="pln">word</span><span class="pun">:</span><span class="pln">i</span><span class="pun">:</span><span class="lit">1</span><span class="pun">);</span>
<span class="kwd">done</span></code>

原文

 

Leave a Reply

Your email address will not be published. Required fields are marked *