将多个文本文件附加到单个文件中的脚本 [英] Script to append multiple text files into single file

查看:35
本文介绍了将多个文本文件附加到单个文件中的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Unix 新手,我需要写一个脚本.有人可以帮助我解决我在目录中有文件列表的要求,如果文件名中的字符串模式匹配,我想合并文件吗?

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 

如果文件名的第二部分='555A'而第三部分由 ORANGE 组成,则所有 Oranges/555A 内容文件将合并为一个文件,文件名为 AAL_555a_Orange_date +"%Y%m%d".txt.

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.

如果文件名的第二部分='555B'而第三部分由 ORANGE/555B 组成,则所有 Oranges 内容文件将合并为一个文件,文件名为 AAL_555b_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.

如果文件名的第二部分='555A'而第三部分由 MANGO 组成,那么所有 Mango 内容文件将合并为一个文件,文件名为 AAL_555a_Mango_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.

请帮助..!

我知道以下命令可以将多个文件附加到一个 .但在这里我想根据文件名中存在的模式来做.

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

像这样

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

     fi         
   done     
done  

推荐答案

使用 Bash 变量替换.

脚本循环所有 AAAL 起始文件,将它们的名称存储到变量 $in 并使用变量替换来处理名称,直到只有第二和第三部分左(例如 AAAL_555A_ORANGE1_F190404.TXT -> 555A_ORANGE).

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

这篇关于将多个文本文件附加到单个文件中的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆