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!

join lines from two files - Unix ksh

Status
Not open for further replies.

nsivas

Programmer
Nov 29, 2000
9
US
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.

Please help me in this.

Thanks
Siva
 
If these lines have something in common (a field) you can use join <file1> <file2>, look at the manpages.
If the files have nothing in common, you must write a script (perl,awk,bash).

regards
chenn
 
awk won't do it easily, nawk (on solaris ) or gawk will do it if you use getline ...

however these will be harder if there are spaces on each of the lines ...

perl is your easiest bet if you have it and want to use it ...

all of these solutions are a lot easier if the files are of equal length ... but i recomend perl.
 


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.
Can I use looping logic?

Thanks

Siva

 
The script is completed:

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 + 3 ))
done

Thanks Tik-tips

Siva
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top