shell的一些文件操作

合并2个文件

  1. a在上,b在下
cata.txt b.txt>c.txt
  1. a在左,b在右
pastea.txt b.txt>c.txt

以下也是a在左,b在右

cata.txt|paste- b.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(default2)--additional-suffix=SUFFIX append an additional SUFFIX tofilenames -b,--bytes=SIZE put SIZE bytes per outputfile-C, --line-bytes=SIZE put atmostSIZE bytes of records per outputfile-l,--lines=NUMBER put NUMBER lines/records per outputfile-n,--number=CHUNKS generate CHUNKS output files

截取一个文件的部分

  1. 取a文件的前x行到b文件
head-nx a.txt>b.txt
  1. 取a文件的后x行到b文件
tail-nx 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

(END)