shell的一些文件操作

合併2個文件

  1. a在上,b在下
    cat a.txt b.txt > c.txt 
  1. a在左,b在右
    paste a.txt b.txt > c.txt 

split分割文件

split [-d] [-l line_num]  <src file> <target file>
split [-d] [-b bytes_num] <src file> <target file>

-a, --suffix-length=N   generate suffixes of length N (default 2)
    --additional-suffix=SUFFIX  append an additional SUFFIX to file names
-b, --bytes=SIZE        put SIZE bytes per output file
-C, --line-bytes=SIZE   put at most SIZE bytes of records per output file
-l, --lines=NUMBER      put NUMBER lines/records per output file
-n, --number=CHUNKS     generate CHUNKS output files

截取一個文件的部分

  1. 取a文件的前x行到b文件
    head -n x a.txt > b.txt 
  1. 取a文件的後x行到b文件
    tail -n x a.txt > b.txt 
  1. 用 awk 可以自由分割
# 將a文件的1-10行放到c01文件,將11行及以後放到c02文件
awk '{if (NR<=10) print $0 >"c01.txt";if (NR>10) print $0>"c02.txt"}' a.txt
    
# 截取a文件的3-10行放到k文件
awk '{if (NR>=3 && NR <=10) print $0 >"k.txt"}' a.txt
  1. 用 sed 截取文件中間若干行
    sed -n '<start_line>, <last_line>p' src_file > dst_file

uniq的另外幾種用法

-d, --repeated        only print duplicate lines, one for each group  // 只打印重複行,且每組重複行只打印一行
-D                    print all duplicate lines   // 打印所有重複行
-u, --unique          only print unique lines     // 只打印非重複行
-i, --ignore-case     ignore differences in case when comparing

xargs 使用標準輸入作爲指定命令的參數

# 將當前目錄下的所有txt文件copy到/tmp下
ls ./*.txt | xargs -I{} cp {} /tmp  

(完)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章