I have two files and I need to combine (not append - but combine a row to a row)
eg:
File1:
apples
grapes
oranges
lemons
File2:
red
green
orange
yellow
After combining, the file should look like: (the second column should start at a specific byte)
apples red
grapes green
oranges orange
lemons yellow
Thanks
Thanks for the reply.
I tried the paste and it worked but the tab is not adjusted and this was my fear.
eg: after paste
apples red
grapes green
oranges orange
lemon yellow
I was wondering something like the first line of file 1 stars at the 1 postion of the line and the first line of file 2 starts at the 60th position of the line.
Thanks again.
you can specify delimiters (TAB by default) with the '-d' switch.
aigles
June 13, 2007, 2:56am
5
Try this awk program:
#!/usr/bin/awk -f
#Awk program : paste.awk
function readFile(i, rec) {
if (getline rec < ARGV)
return rec;
else {
++eof;
return "";
}
}
BEGIN {
while (1) {
rec1 = readFile(1);
rec2 = readFile(2);
if (eof[1] && eof[2]) break;
printf("%-60s%s\n", rec1, rec2);
}
exit;
}
Output:
$ awk -f paste.awk file1.txt file2.txt
apples red
grapes green
oranges orange
lemons yellow
$
Jean-Pierre.
Ygor
June 13, 2007, 11:07am
6
Or...
paste file1 file2 | awk -F '\t' '{printf "%-60s%s\n", $1, $2}'
aigles
June 13, 2007, 2:18pm
7
Simpler, but assumes that none of the files contains tabulation.
Jean-Pierre.
Ygor
June 13, 2007, 10:32pm
8
Sure, and if they do contain tabs, then it's simple to specify a unique delimiter.