LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-05-2015, 05:25 PM   #1
dcdw51
LQ Newbie
 
Registered: Feb 2015
Posts: 1

Rep: Reputation: Disabled
Need help cat multiple files to one file


I am currently running a system simulation on multiple files.
I have a computer algorithm written in perl to run "system" simulations for all the files I need

What I am trying to do is put multiple files into one file, only problem is that its not doing exactly what I need it do

Example:

I am "cat txt0.txt txt1.txt txt2.txt txt3.txt > allfiles.txt"

I need it to read as

txt0.txt
txt1.txt
txt2.txt
txt3.txt

Instead its taking all the files and taking the information within each txt file and putting them all together. Info that looks like this


fdfasdfqwdefdfefdkfkkkkkkkkkkkkkkkfsdfasdxfewqfe..........

all clustered together

you get the picture ?

I am really confused how to get this to work, there are over 100 files that need to go into a single file.
That way when I run it through the perl algorithm I created, I can do it in one shot.
 
Old 02-05-2015, 05:43 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,561

Rep: Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127
If I understand your problem correctly, it's that the source files have no trailing new line, so when they get catted it all ends up one one big line instead of each file's contents on their own.

In that case you can just do it in a for loop and manually stick a line break between them
Code:
rm allfiles.txt
for i in txt?.txt; do
  cat "$i" >> allfiles.txt
  echo >> allfiles.txt
done
 
Old 02-05-2015, 06:48 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,997

Rep: Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219Reputation: 2219
Code:
cat txt?.txt > allfiles.txt
 
Old 02-06-2015, 01:30 AM   #4
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 364

Rep: Reputation: 158Reputation: 158
Quote:
Originally Posted by dcdw51 View Post
...

I am "cat txt0.txt txt1.txt txt2.txt txt3.txt > allfiles.txt"

I need it to read as

txt0.txt
txt1.txt
txt2.txt
txt3.txt

Instead its taking all the files and taking the information within each txt file and putting them all together.

...
It sounds like cat is working exactly as it should. It concatenates files.

I am guessing that you want allfiles.txt to contain the filenames, not the contents of those files. There are a number of ways to do that. Here is one:
Code:
ls txt*.txt > allfiles.txt
Replace txt*.txt with the appropriate file specification for the file list you require.

Last edited by Beryllos; 02-06-2015 at 01:39 AM. Reason: A simpler command
 
Old 02-06-2015, 02:53 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,417

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
This really is the QN: do you want the filenames listed into the new file or the contents (or both ??).
In any case, why bother? If you've already got a Perl program to process them, why not add some minimal code to handle multiple files?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Combine (cat) all files in a tar.gz file mdavisuk Linux - Newbie 3 08-01-2014 03:05 AM
[SOLVED] Can `cat` binary file damage any files on HDD? Mr. Alex Linux - General 7 05-25-2012 06:37 AM
how to cat multiple files into a single file with tab delimiter shyamsandeep Linux - Newbie 2 09-06-2011 12:30 PM
a little bash help: cat multiple files together except first line? sanimfj Linux - General 3 02-09-2010 05:27 PM
How to Cat multiple files with numeric differences zxcvcxz Linux - Software 7 11-19-2009 07:49 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration