Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
163 views
in Technique[技术] by (71.8m points)

linux - Script to append multiple text files into single file

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  
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...