Merge the multiple text files into one file

Hi All,

I am trying to merge all the text files into one file using below snippet

 
cat /home/Temp/Test/Log/*.txt >> all.txt

But it seems it is not working.
I have multiple files like Output_ServerName1.txt, Output_ServreName2.txt
I want to merge each file into one single file and after that will remove the text files.

Is it giving any error? It should work.. whats the OS?

It seems more logical to write a new file, not append to a file

cat /home/Temp/Test/Log/*.txt > all.txt

all.txt is the concatenation of all /home/Temp/Test/Log/*.txt files in alphabetical order.
If you think that all.txt is okay, you can add the file deletions like this

cat /home/Temp/Test/Log/*.txt > all.txt &&
rm -f /home/Temp/Test/Log/*.txt
1 Like

Hi,

Let me more specfic, It is appending the text but not in proper format.
Can I do something appending the text of each file but in between have something file name output like below:-

 
File 1
some text
#########
File 2
Some text
##########
File 3
Some text

With a Posix awk or nawk:

awk 'FNR==1 {x=FILENAME; gsub(".","#",x); print x ORS FILENAME ORS x} 1' /home/Temp/Test/Log/*.txt
1 Like

Thanks Man,

Could you please explain the code which you have provided so that I can modified a little bit like it is giviing me the whole path of the file name I just wanted the file name in the result

You can solve that on shell level

(cd  /home/Temp/Test/Log &&
awk 'FNR==1 {x=FILENAME; gsub(".","#",x); print x ORS FILENAME ORS x} 1' *.txt)

Or with more awk code

awk 'FNR==1 {fn=FILENAME; sub(".*/","",fn); x=fn; gsub(".","#",x); print x ORS fn ORS x} 1' /home/Temp/Test/Log/*.txt

The awk functions like sub() and gsub() and awk variables like FNR,FILENAME,ORS are explained in

man awk

The lonely 1 at the end is lazy for {print}. This actually copies the file contents.
The extra {code} is only executed if FNR==1 that's true at the first line of each file.

1 Like