Removing part of a file name and appending into a single file

I have two files like ABC_DEF_yyyyymmdd_hhmiss_XXX.txt and ABC_DEF_yyyyymmdd_hhmiss_YYY.txt. The date part is going to be changing everytime. How do i remove this date part of the file and create a single file like ABC_DEF_XXX.txt.

do you want to merge data from these type of files into one file ABC_DEF_XXX.txt?

I don't know what you are trying to accomplish exactly. Are you wanting to retain the contents of the file or just create new one without date?

Here ya go:

cat ABC_DEF_yyyyymmdd_hhmiss_XXX.txt > ABC_DEF.txt

Or....

mv ABC_DEF_yyyyymmdd_hhmiss_XXX.txt ABC_DEF.txt

Or....

Give some specifics, and I'm sure we can help you out!

---------- Post updated at 02:35 PM ---------- Previous update was at 02:32 PM ----------

If it is in a script that is reading this, maybe put this in:

sed -e -i 's/ABC_DEF.*$/ABC_DEF.txt/g' ABC_DEF_yyyyymmdd_hhmiss_XXX.txt

Okie.. i want to remove the dynamic date part of both the files and merge it to a single file abc_def_xxx.txt.

To be specific, this is being used in a shell script that will pass the initial name of the file ( say abc_def.txt). Now my search directory woud be having some files with the pattern, abc_def_yyyymmdd_hhmiss_XXX.txt . So i need to search the directory for this pattern. once the files are found in the target directory, i need to merge thses files into a single file which will be named as abc_def_xxx.txt.

Hope i am clear this time. Please suggest how to get this accomplished

Assuming you already have list of files needed to be merged. Following is one method you can follow:

>abc_def_xxx.txt
while read file
do
cat $file >> abc_def_xxx.txt
done < file_list

This is just a sample. I am sure you can modify it to fit your needs:

#!/bin/sh
filemerge=/path/merge
newfile=/path/abc_def_xxx.txt.
#
ls /path/directory/ | grep abc_def* > $filemerge
#
for j in `cat $filemerge`
do
cat $j >> $newfile
echo "adding $filemerge to $newfile"
done
rm $filemerge

How do i extract the date part of the existing file in the process to use it in the log flog_<date part extracted>. Also i want ot remove the original files after the merge

Use this:

#!/bin/sh 
filemerge=/path/merge 
newfile=/path/abc_def_xxx.txt. 
# 
ls /path/directory/ | grep abc_def* > $filemerge 
# 
for j in `cat $filemerge` 
do 
cat $j >> $newfile 
echo "adding $filemerge to $newfile"
rm $j
done 
rm $filemerge

---------- Post updated at 03:50 PM ---------- Previous update was at 03:44 PM ----------

#!/bin/sh 
filemerge=/path/merge # this is an arbitrary file that you are using. Just make up a path and filename
newfile=/path/abc_def_xxx.txt   #This is the new file that you want everything in 
# 
ls /path/directory/ | grep abc_def* > $filemerge     #this is doing an ls and putting all of the filenames 
                                                     #into a file that you will run through
# 
for j in `cat $filemerge`           #This is reading the file one line at a time and doing the following: 
do 
cat $j >> $newfile                  #adding info to newfile...note that it is appending everything to it
echo "adding $filemerge to $newfile"   #just letting you know where it is in process
rm $j                       #deleting old file
done 
rm $filemerge     #deleting file that had list of all filenames

Try this. $1 contains the first parameter to the script

pat=$1 
suffix=${pat##*.} 
first=${pat%.*}
for file in ${first}_*_*.${suffix}
do
  last=${file##*_}
  cat "$file" >> "${first}_${last}"
done

You can try removing the files by adding && rm "$file"

cat "$file" >> "${first}_${last}" && rm "$file"

You can write to a log by using echo statements in the loop and

done >> filemerge.log

Obviously this would need to be thoroughly tested first..

--

( Or, in ultra-compact form just for fun :))

for f in ${1%.*}_*_*.${1##*.}; do
  cat "$f" >> "${1%.*}_${f##*_}"
done