Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combine File Contents

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi!

All the filenames are listed vertically in 1 file (ex. file.txt). How can I combine all the file contents into a single file (ex. fileout.txt). Assume that I am already in the current directory of interest. Need your advice...

Thanks!
-shen544
 
cat `cat file.txt` > fileout.txt

now this assume they are all text files because CAT doesn't behave too well with binary files. Also there won't be any file separator. all the text will run together. It is very hard to researate this 'bundle' back into the separate files that created it.

you could use

tar -cf fileout.tar `cat file.txt`

if you want to keep them as separate files but inside a single 'bundle'. then you use

tar -xf fileout.tar

to extract them back out

 
shen544:

tdatgod's solution should work, but perhaps it's more elegant to use xargs:

xargs cat < file.txt > fileout.txt

Regards,

Ed
Schaefer
 
Hi tdatgod, olded,

What if instead of filenames, the content of file.txt are directory names.

file.txt:
DIR1
DIR2

How can I combine the contents of a particular filename in these directories. (Example: Combine contents of DIR1/file and DIR2/file). Please Advice.

Regards,
shen544
 
Hi,
That requires a Loop. Again this depends on the number of lines in your file as olded pointed out my solution will BREAK if you have more than 300 files or so in the file.txt.

I am csh programmer so this would need to be converted to sh if you need sh or ksh.

I don't quite understand what you mean that you will have DIR1 and DIR2 in the file. are these intertwinded with other files or are there just 2 lines?


I guess I could interpret your request a couple ways

combine identical file names from dir1 and dir2 into a combined file of the same name in like DIR3. ( now where DIR3 comes from I don't know. )

dir1/file1.txt dir2/file1.txt > dir3/file1.txt

foreach file ( `ls -1 $DIR1` )
set a = $file:t
if ( -e $DIR2/$a )
cat $file $DIR2/$a > $DIR3/$a.txt
endif
end

if you want them all into a single file you could replace

cat $file $DIR2/$a > $DIR3/$a.txt

with
cat $file $DIR2/$a >>! onebigfile.txt

Another way to interpret your request would be.

CAT all the files from dir1 and dir2 into a single file.

foreach file ( `cat file.txt` )
if ( -d $file ) then
# this is a directory
xargs cat < `ls -1 $file` >>! fileout.txt
else
# this is a file
xargs cat < file.txt >>! fileout.txt
endif
end

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top