Script to append multiple text files into single f

2019-09-19 12:59发布

问题:

I am new to Unix , I need to write a script. Can someone help me with a requirement where I have list of files in a directory, I want to Merge the files if a pattern of string matches in filenames?

AAAL_555A_ORANGE1_F190404_D190408.TXT.freshfruits_20190422-115617       
AAAL_555A_ORANGE2_F190404_D190408.TXT.freshfruits_20190422-115617       
AAAL_555A_ORANGE3_F190404_D190408.TXT.freshfruits_20190422-115617       
AAAL_555A_ORANGE4_F190404_D190408.TXT.freshfruits_20190422-115617     
AAAL_555B_ORANGE5_F190404_D190408.TXT.freshfruits_20190422-115617       
AAAL_555B_Orange6_F190404_D190408.TXT.freshfruits_20180422-115617 

If second part of filename='555A' and third part consists of ORANGE then all Oranges/555A content files will merger into one file with filename as AAL_555a_Orange_date +"%Y%m%d".txt.

If second part of filename='555B' and third part consists of ORANGE/555B then all Oranges content files will merger into one file with filename as AAL_555b_Orange_date +"%Y%m%d".txt.

If second part of filename='555A' and third part consists of MANGO then all Mango content files will merger into one file with filename as AAL_555a_Mango_date +"%Y%m%d".txt.

Kindly help ..!

I know the below command to appended multiple files into one . But here i want to do based on patterns present in filename.

cat File1 file2 file3 >> final.txt

Something like this

for i in *.TXT; do    
   while field separator='_' read -r str1 num str2 ; do    
     if [ "$str1" = "RTG" ]; then    

     fi         
   done     
done  

回答1:

Using Bash variable substitutions.

Script loops all the AAAL starting files, stores their names to variable, $in and uses variable substitutions to process the names until there are only the second and third parts left (for example AAAL_555A_ORANGE1_F190404.TXT -> 555A_ORANGE).

See the comments in the script for a step-by-step example and linked documentation for substitution explanation:

$ for in in AAAL*
  do                              # for example       AAAL_555A_ORANGE1_F190404.TXT
      out=${in#*_}                # remove AAAL_        -> 555A_ORANGE1_F190404.TXT
      out=${out%_*}               # remove _F190404.TXT -> 555A_ORANGE1
      out=${out%[0-9]}            # remove 1            -> 555A_ORANGE
      cat $in >> AAAI_$out.txt
  done
$ ls -1 AAAI*
AAAI_555A_MANGO.txt
AAAI_555A_ORANGE.txt
AAAI_555B_APPLE.txt
AAAI_555B_Orange.txt
AAAI_555B_ORANGE.txt


标签: linux shell unix