点击(此处)折叠或打开
- #!/bin/bash
- #程序描述:进行多个文件, 列数据合并
- #作者: 程晓鹏
- #日期: 2017.10.17
- #例如
- #文件 1.txt内容如下(不含下面两行的‘#’字符)
- #private string id;
- #private String name;
- #文件 2.txt内容如下 (不含下面两行的‘#’字符)
- # //注释1
- # //注释2
- #使用命令 ./MergeFileColumn.sh 1.txt 2.txt
- #执行结果如下:
- #private string id; //注释1
- #private String name; //注释2
- #合并文件列
- #参数1:第一个文件,第一列
- #参数2:第二个文件,第二列
- #参数3:计算结果,文件路径
- function mergeFileColumn()
- {
- v_file1=$1;
- v_file2=$2;
- v_out=$3;
- awk 'BEGIN{OFS="";FS=""} NR==FNR{a[NR]=$0} NR>FNR{print a[FNR],$0}' $v_file1 $v_file2 > $v_out;
- }
- #创建临时文件
- #返回值:临时路径路径
- function createTmpFile
- {
-
- v_time=`date +%Y%m%d%H%M%S`; #系统时间
- v_rand=$RANDOM; #随机数
- v_file="/tmp/mergecolumn_tmpfile_${v_time}_${v_rand}";
- echo $v_file;
- }
- #开始运行
- function run()
- {
- v_tmp_file=$(createTmpFile); #临时结果文件路径
- v_tmp_buffer="${v_tmp_file}_buffer"; #结果数据缓冲区,做数据缓冲转移功能
- mergeFileColumn $1 $2 $v_tmp_buffer;
- cat $v_tmp_buffer > $v_tmp_file;
- shift;
- shift;
- while [ $# -gt 0 ];
- do
- mergeFileColumn $v_tmp_file $1 $v_tmp_buffer;
- cat $v_tmp_buffer > $v_tmp_file;
- shift;
- done;
- cat $v_tmp_file; #通过查看临时文件,输出结果
- rm -rf $v_tmp_file; #删除临时文件
- rm -rf $v_tmp_buffer; #删除临时缓冲区
- }
- if [ $# -eq 1 ]; then
- cat $1;
- elif [ $# -gt 1 ]; then
- run $*;
- else
- echo "错误:文件列合并,入参必须传递文件路径.";
- fi;