LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-27-2009, 09:51 AM   #1
Mike_V
Member
 
Registered: Apr 2009
Location: Boston MA
Distribution: CentOS 6.2 x86_64 GNU/Linux
Posts: 59

Rep: Reputation: 19
Paste each single row from n separate text files into a new file containing n rows


Hi there,

Say I have 3 text files each with one row of numbers:

file1.txt contains:
Code:
9830987716238
file2.txt contains:
Code:
908095823098
file3.txt contains:
Code:
43098104982
and I want to paste them in one file called

file_all.txt containing:
Code:
9830987716238
908095823098
43098104982
how could I do that?

I use "paste" a lot to add files containing columns of text, but these are rows...

Thanks a lot!
Mike
 
Old 04-27-2009, 09:53 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

cat file1 file2 file3 fileN > newfile

Hope this helps.
 
Old 04-27-2009, 10:08 AM   #3
Mike_V
Member
 
Registered: Apr 2009
Location: Boston MA
Distribution: CentOS 6.2 x86_64 GNU/Linux
Posts: 59

Original Poster
Rep: Reputation: 19
so simple... and so helpful, thanks!
Mike
 
Old 04-27-2009, 10:11 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Simple if you know how

BTW: if you have a lot of files that need to be processed, you can use wildcards: cat file* > newfile
 
Old 04-27-2009, 10:46 AM   #5
Mike_V
Member
 
Registered: Apr 2009
Location: Boston MA
Distribution: CentOS 6.2 x86_64 GNU/Linux
Posts: 59

Original Poster
Rep: Reputation: 19
Hi Druuna,

Indeed I was using cat file* > newfile.

One problem now. It worked on a test I did myself but not on my actual files. My actual files each contain one row of almost 400 numbers. For some reason they are pasted into a single row in newfile. The strange thing to me is that I see no difference between my test files and the actual files in terms of how the line ends or so...

Would you happen to know how I can be sure that each file will start on a new row in newfile?

~Mike

Last edited by Mike_V; 04-27-2009 at 11:00 AM.
 
Old 04-27-2009, 10:57 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

What is the output of the following commands:

file name.actual.file
od -c name.actual.file
 
Old 04-27-2009, 11:09 AM   #7
Mike_V
Member
 
Registered: Apr 2009
Location: Boston MA
Distribution: CentOS 6.2 x86_64 GNU/Linux
Posts: 59

Original Poster
Rep: Reputation: 19
$ file list_-22_-10_-12_per_track_number_m.txt
list_-22_-10_-12_per_track_number_m.txt: character Computer Graphics Metafile

$ od -c list_-22_-10_-12_per_track_number_m.txt
0000000 0 7 0 0 0 0 0 0
0000020 0 0 0 0 0 0 0 0
0000040 0 1 4 0 0 0 0 0 0
0000060 0 0 0 0 0 1 0 0
0000100 0 0 0 0 0 2 0 0
0000120 0 0 1 0 0 0 0 0
0000140 0 0 0 0 0 0 0 0
*
0000260 0 0 2 0 0 0 0 0
0000300 0 0 0 0 0 0 0 0
*
0000420 0 0 0 0 0 0 1 6 0
0000440 0 0 0 0 0 0 0 0
*
0001220 0 0 0 0 1 4 0 0
0001240 0 0 0 0 0 0 0 0
*
0001440 0
0001442
$



and also here for the "test" file:

$ od -c test1.txt
0000000 0 7 0 0 0 0 0 0
0000020 0 0 0 0 0 0 0 0
0000040 0 1 4 \n
0000045

$ od -c test1.txt
0000000 0 7 0 0 0 0 0 0
0000020 0 0 0 0 0 0 0 0
0000040 0 1 4 \n
0000045


So I can already see a difference myself... the "\n" at the end that my actual files don't have...

I also noticed that "\n" is introduced after I close gedit (it wasn't there when I made the test file)

(One more piece of information: my actual files are 399 in total and their number of columns ranges between 1 and 399)

Last edited by Mike_V; 04-27-2009 at 11:15 AM.
 
Old 04-27-2009, 11:31 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Computer Graphics Metafile files are a binary format, the tools discussed are meant for text files.

I wouldn't know how to manipulate these.........
 
Old 04-27-2009, 01:11 PM   #9
Mike_V
Member
 
Registered: Apr 2009
Location: Boston MA
Distribution: CentOS 6.2 x86_64 GNU/Linux
Posts: 59

Original Poster
Rep: Reputation: 19
In the meantime I did find a solution:

I typed in the command line:

Code:
foreach file (list*)
    echo `cat $file` >> newfile.txt
end
NOTE 1: My file names start with "list..."
NOTE 2: The quotation marks are not ' but ` (same key as ~)

That did work.

Thanks

Last edited by Mike_V; 04-27-2009 at 01:20 PM.
 
Old 04-27-2009, 01:20 PM   #10
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

That, indeed, works. Very nice workaround!

For those that wonder why this does work:

Because cat $file is placed between backticks, it is executed in a new shell. There's still no newline after the cat command is executed, but when the 'new shell' itself is closed, a newline is added before it is written to newfile.txt.
 
Old 04-27-2009, 11:27 PM   #11
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
i do not have your sample files to try, but you can try this. if it doesn't work, so be it.
Code:
awk '{print $0 > 'newfile' }' list*
 
Old 04-27-2009, 11:51 PM   #12
Mike_V
Member
 
Registered: Apr 2009
Location: Boston MA
Distribution: CentOS 6.2 x86_64 GNU/Linux
Posts: 59

Original Poster
Rep: Reputation: 19
ghostdog74:

At first I got an error message but after changing some quotation marks this worked:

Code:
awk '{print $0 > "newfile.txt" }' file*
Very short, very nice.

Cheers,
 
  


Reply



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
How to duplicate rows of text from one file to another? guest Programming 1 04-25-2009 08:14 AM
Print only specific rows in a text file Mike_V Programming 3 04-24-2009 07:18 PM
[C++] append to each row of a text files sylvaticus Programming 2 10-18-2007 07:22 AM
awk command to merge columns from two separate files into single file? johnpaulodonnell Linux - Newbie 4 01-23-2007 10:10 AM
Delete the first row from a text file loopoo Linux - Newbie 2 08-15-2006 02:57 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:44 AM.

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
Open Source Consulting | Domain Registration