Read two files at the same time, and combine their peers into one line Write to a combined file
shell script:
#!/bin/bash #set -x cd `dirname "$0"`/../ INAS_SCRIPT_ROOT=`pwd` function make_if_no_existis_dir() { local dir=$1 if [ ! -e "${dir}" ] then mkdir -p "${dir}" be return 0 } function union2() { local file_name1=$1 local file_name2=$2 local union_dir=${INAS_SCRIPT_ROOT}/file/union make_if_no_existis_dir ${union_dir} >${union_dir}/union.txt exec 3< "${INAS_SCRIPT_ROOT}/file/${file_name1}" exec 4< "${INAS_SCRIPT_ROOT}/file/${file_name2}" while read line1<&3 && read line2<&4 do echo $line1 $line2 >> ${union_dir}/union.txt done } union2 $1 $2
a.txt content:
aaa bbb ccc ddd eee fff
b.txt content:
AAA BBB CCC DDD EEE FFF GGG
run script
sh union_file.sh a.txt b.txt
get the merged file union.txt
aaa aaa BBB BBB ccc CCC ddd DDD eee EEE fff FFF ggg GGG
Reference link: https://www.cnblogs.com/tangxin-blog/p/6531812.html