Shell reads multiple files at the same time

Source: Internet
Author: User

Read two files at the same time, and merge their peers into one line to write to a merged file

Shell script:

#!/bin/bash

#set
-x CD ' dirname ' $ '/. /
inas_script_root= ' pwd '



function Make_if_no_existis_dir ()
{local

   dir=$1

   if [!-e ' ${dir} '  ]
   Then

           mkdir  -P "${dir}"
   fi 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 $line 1 $line 2 >> ${union_dir}/union.txt
 done


}

Union2 $

A.txt content:

AAA
BBB
CCC
ddd
Eee
FFF

B.txt content:

AAA
BBB
CCC
DDD
EEE
FFF
GGG

Run the script

SH union_file.sh  a.txt b.txt

Get the merged file Union.txt

AAA Triple-A BBB, CCC
CCC
ddd ddd
eee Eee
FFF FFF
GGG GGG

Reference Link: https://www.cnblogs.com/tangxin-blog/p/6531812.html











Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.