LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 09-01-2010, 02:22 PM   #1
W_Bosman
LQ Newbie
 
Registered: Sep 2010
Posts: 3

Rep: Reputation: 0
Combine 3 text files into 1


I have a little issue with combining 3 files into 1 file.

file 1:
1
2
3
4

file 2:
a
b
c
d


file 3:
9
8
7
6

Output in file 4 should be like:
1
a
9
2
b
8
3
c
7
4
d
6

I have tried paste, awk but all are not giving me the correct layout.

Can someone help me out here?

Thanks
 
Old 09-01-2010, 02:45 PM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,614

Rep: Reputation: 450Reputation: 450Reputation: 450Reputation: 450Reputation: 450
Hi,

Try this:
Code:
 sed -e 'R file_2
R file_3
' file_1 > outfile
It is important that there is a newline after file_2 and file_3; otherwise it won't work.

Hope this helps.
 
Old 09-01-2010, 02:49 PM   #3
suprstar
Member
 
Registered: Aug 2010
Location: Atlanta
Distribution: ubuntu, debian
Posts: 142
Blog Entries: 2

Rep: Reputation: 22
I like php, maybe you dont, but it gets the job done:

Code:
#!/usr/bin/php
<?
$f1 = fopen("f1", "r");
$f2 = fopen("f2", "r");
$f3 = fopen("f3", "r");
$f4 = fopen("f4", "w");

while ( true ) {
  $s1=trim(fgets($f1));
  $s2=trim(fgets($f2));
  $s3=trim(fgets($f3));
  if ( !$s1 || !$s2 || !$s3 )
    exit(0);
  fputs($f4,$s1 . "\n" . $s2 . "\n" . $s3 . "\n");
}
?>
 
Old 09-01-2010, 03:11 PM   #4
W_Bosman
LQ Newbie
 
Registered: Sep 2010
Posts: 3

Original Poster
Rep: Reputation: 0
@crts,

your solution worked perfectly
Thank you for it.

Is it possible to use this when i want to add some extra lines in it?

like this:

file 1:
a
b

file 2:
1
2

file 3:
z
x

output:
name a
number 1
clock z
name b
number 2
clock x

this would help me even more.
 
Old 09-01-2010, 03:54 PM   #5
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,614

Rep: Reputation: 450Reputation: 450Reputation: 450Reputation: 450Reputation: 450
Hi,

you will have to use a pipe:
Code:
sed -r 'R file2
R file3
' file1 | sed -r 's/.*/name &/;n;s/.*/number &/;n;s/.*/clock &/'
 
Old 09-01-2010, 04:42 PM   #6
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 729

Rep: Reputation: 75
Hi.

Some shells allow Process Substitution:
Code:
# Like paste file1 file2 file3:
paste -d '\n' <( sed 's/^/name /' data1 ) \
<( sed 's/^/number /' data2 ) \
<( sed 's/^/clock /' data3 )
producing:
Code:
name a
number 1
clock z
name b
number 2
clock x
See man pages for detail, e.g man bash ... cheers, makyo

Environment used:
Code:
Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0 
GNU bash 3.2.39
GNU sed version 4.1.5
paste (GNU coreutils) 6.10
 
Old 09-01-2010, 05:06 PM   #7
aamirnehal
LQ Newbie
 
Registered: Apr 2010
Posts: 3

Rep: Reputation: 0
if you want to copy file1 data, file2 data, and file3 text data into file4 then you have to do just this :

Quote:
[Linux #]cat file1 >> file4;cat file2 >> file4;cat file3 >> file4
and then press enter...

all the data of file1 and file2 and file3 will be paste in file4 respectively...

Thanks
 
Old 09-01-2010, 05:28 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by aamirnehal View Post
if you want to copy file1 data, file2 data, and file3 text data into file4 then you have to do just this :



and then press enter...

all the data of file1 and file2 and file3 will be paste in file4 respectively...

Thanks
He wants them "interleaved", though, not tacked on end to end.
If he wanted to just have them in the manner you suggest a
Code:
cat file1 file2 file3 > file4
would do the job just as well with far less typing.



Cheers,
Tink
 
  


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
[SOLVED] Best way to combine and partially overwrite text files byron262 Linux - Software 2 05-18-2010 07:42 PM
combine text files with a regex itzfritz Linux - General 10 06-18-2009 01:26 PM
Need to combine text on to one line clstanton Linux - Newbie 10 05-12-2009 06:55 AM
Combine number and text Ephracis Programming 3 11-17-2004 11:13 AM
I need a program to combine text with gif files stephenoregan4 Linux - Software 1 02-16-2004 09:29 AM


All times are GMT -5. The time now is 05:53 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