Merge the multiple text files into one file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Merge the multiple text files into one file
# 1  
Old 07-01-2013
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

Code:
 
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.
# 2  
Old 07-01-2013
Is it giving any error? It should work.. whats the OS?
# 3  
Old 07-01-2013
It seems more logical to write a new file, not append to a file
Code:
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
Code:
cat /home/Temp/Test/Log/*.txt > all.txt &&
rm -f /home/Temp/Test/Log/*.txt

This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 07-02-2013
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:-

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

# 5  
Old 07-02-2013
With a Posix awk or nawk:
Code:
awk 'FNR==1 {x=FILENAME; gsub(".","#",x); print x ORS FILENAME ORS x} 1' /home/Temp/Test/Log/*.txt

This User Gave Thanks to MadeInGermany For This Post:
# 6  
Old 07-02-2013
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
# 7  
Old 07-02-2013
You can solve that on shell level
Code:
(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
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
Code:
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.
This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to merge the multiple data files as a single file?

Hi Experts, I have created multiple scripts and send the output to new file, getting this output to my mailbox on daily basis. I would like to send the all outputs to a single file, need to merge all file outputs on a single file. For example, Created script for df -h > df.doc grep... (7 Replies)
Discussion started by: seenuvasan1985
7 Replies

2. Shell Programming and Scripting

Need to merge multiple text files vertically and place comma between fields

Hello expert friends, I'm writing a script to capture stats using sar and stuck up at report generation. I have around 10 files in a directory and need to merge them all vertically based on the time value of first column (output file should have only one time value) and insert comma after... (6 Replies)
Discussion started by: prvnrk
6 Replies

3. Shell Programming and Scripting

Split a text file into multiple text files?

I have a text file with entries like 1186 5556 90844 7873 7722 12 7890.6 78.52 6679 3455 9867 1127 5642 ..N so many records like this. I want to split this file into multiple files like cluster1.txt, cluster2.txt, cluster3.txt, ..... clusterN.txt. (4 Replies)
Discussion started by: sammy777
4 Replies

4. UNIX for Dummies Questions & Answers

Changing text in multiple files, but with different text for each file

Hello, I have a situation where I want to change a line of text in multiple files, but the problem is that I want to change the text to something unique for each file. For example, let's say I have five files named bob.txt, joe.txt, john.txt, tom.txt, and zach.txt. Each of these files has a... (5 Replies)
Discussion started by: Scatterbrain26
5 Replies

5. Shell Programming and Scripting

Merge multiple files found in config file

I have a config file with a bunch of these type of blocks: <concat destfile="${standard.js.file}" append="true"> <filelist dir="${js.dir}/foo" files="foo.js, foo2.js"/> <filelist dir="${js.dir}" files="foo3.js"/> <filelist dir="${js.dir}/bar/js"... (11 Replies)
Discussion started by: Validatorian
11 Replies

6. UNIX for Dummies Questions & Answers

Merge multiple files

Hi All, How can I merge 3rd column of multiple files into 1 file, the column header in the merged file being the name of the file from which the 3rd column was taken. The first 2 columns of all the files are exactly same. Thanks for your help ! (3 Replies)
Discussion started by: newbie83
3 Replies

7. Shell Programming and Scripting

Merge 2 text files to one text file side by side

Inquiring minds want to know.... I need to take two files that I have latitude and longitude values and then combine them into one file with the values side by side separated by a space. the first file is temp113-lat.txt and the second is temp113-lon.txt. They each have values listed in the... (15 Replies)
Discussion started by: ahinkebein
15 Replies

8. UNIX for Advanced & Expert Users

Merge multiple .so files

Hi all, I am developing an application in Tcl, inwhich i have to load many modules written in C. I am converting those C modules into shared object(.so) files, and wrap it with my application using SWIG, for which i had the interface file. Now my question is, i have two different... (2 Replies)
Discussion started by: senthilvnr
2 Replies

9. Shell Programming and Scripting

Merge text files while combining the multiple header/trailer records into one each.

Situation: Our system currently executes a job (COBOL Program) that generates an interface file to be sent to one of our vendors. Because this system processes information for over 100,000 employees/retirees (and growing), we'd like to multi-thread the job into processing-groups in order to... (4 Replies)
Discussion started by: oordonez
4 Replies

10. UNIX for Dummies Questions & Answers

grep multiple text files in folder into 1 text file?

How do I use the grep command to take mutiple text files in a folder and make one huge text file out of them. I'm using Mac OS X and can not find a text tool that does it so I figured I'd resort to the BSD Unix CLI for a solution... there are 5,300 files that I want to write to one huge file so... (7 Replies)
Discussion started by: coppertone
7 Replies
Login or Register to Ask a Question