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  

(完)

发布了169 篇原创文章 · 获赞 332 · 访问量 48万+

猜你喜欢

转载自blog.csdn.net/nirendao/article/details/104092939