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
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
04-27-2009, 09:51 AM
#1
Member
Registered: Apr 2009
Location: Boston MA
Distribution: CentOS 4.7 x86_64 GNU/Linux
Posts: 58
Rep:
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:
file2.txt contains:
file3.txt contains:
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
04-27-2009, 09:53 AM
#2
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 7,514
Hi,
cat file1 file2 file3 fileN > newfile
Hope this helps.
04-27-2009, 10:08 AM
#3
Member
Registered: Apr 2009
Location: Boston MA
Distribution: CentOS 4.7 x86_64 GNU/Linux
Posts: 58
Original Poster
Rep:
so simple... and so helpful, thanks!
Mike
04-27-2009, 10:11 AM
#4
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 7,514
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
04-27-2009, 10:46 AM
#5
Member
Registered: Apr 2009
Location: Boston MA
Distribution: CentOS 4.7 x86_64 GNU/Linux
Posts: 58
Original Poster
Rep:
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 .
04-27-2009, 10:57 AM
#6
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 7,514
Hi,
What is the output of the following commands:
file name.actual.file
od -c name.actual.file
04-27-2009, 11:09 AM
#7
Member
Registered: Apr 2009
Location: Boston MA
Distribution: CentOS 4.7 x86_64 GNU/Linux
Posts: 58
Original Poster
Rep:
$ 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 .
04-27-2009, 11:31 AM
#8
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 7,514
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.........
04-27-2009, 01:11 PM
#9
Member
Registered: Apr 2009
Location: Boston MA
Distribution: CentOS 4.7 x86_64 GNU/Linux
Posts: 58
Original Poster
Rep:
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 .
04-27-2009, 01:20 PM
#10
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 7,514
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.
04-27-2009, 11:27 PM
#11
Senior Member
Registered: Aug 2006
Posts: 2,695
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*
04-27-2009, 11:51 PM
#12
Member
Registered: Apr 2009
Location: Boston MA
Distribution: CentOS 4.7 x86_64 GNU/Linux
Posts: 58
Original Poster
Rep:
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,
Thread Tools
Search this Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT -5. The time now is 02:45 PM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News