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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Joining lines from two different file (in order) Unix Ksh

Status
Not open for further replies.

nsivas

Programmer
Nov 29, 2000
9
US
Hi

I want to join all the lines of two different files in order. ie.,1st line of the file1 will be joined to the first line of the file2 and second line of the file1 to second line of the file2 and vice versa, the ouput has to be moved to another file.

For this Tony sent me a very usefull reply:

#!/bin/sh

cat /tmp/a | sed -n '1p' >> d
cat /tmp/b | sed -n '1p' >> d

where a, b and d are files

Please help me to convert this script in to the entire file.

Thanks

Siva

 
if files are same length ... try perl:

open (FILEA, "a");
@linesa = <FILEA>;
close (FILEA);

open (FILEB, &quot;b&quot;);
@linesa = <FILEB>;
close (FILEB);

foreach $num (0..$linesa)
{
print $linesa[$num], $linesb[$num], '\n';
}

-------------
create the above as a file and pipe it ... you could even pass in the first 2 files as arguments if you wanted ...
 
Thanks Jad, ur anwer is good, it works.
In Ksh also I completed in the following way:

total=23
m=1
n=1
while (( $total >= $m ))
do
cat $HOME/file1 | sed -n &quot;$m&quot;p >> file3
cat $HOME/file2 | sed -n &quot;$n&quot;p >> file3
(( m = $m + 1 ))
(( n = $n + 1 ))
done

Siva
 
You want this?
FILE1
[tt]
first line
second line
third line
fourth line
[tt]
FILE2
[tt]
record 1
record 2
record 3
record 4
[tt]
and get
[tt]
first line record 1
second line record 2
third line record 3
fourth line record 4
[tt]
on FILE3?
then use the [tt]paste[/tt] command!
[tt]paste FILE1 FILE2 > FILE3[/tt]

i hope it works...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top