1753820 Members
8745 Online
108805 Solutions
New Discussion юеВ

Easy Script

 
SOLVED
Go to solution
Joseph C. Denman
Honored Contributor

Easy Script

I just can't get it. I have 21 files and each has one field. I want to combine all the files into one with all the fields matching on each line separated by a :

example:

file1.
xxxx11111
xxxx11111
bbbb22222

file2.
c
d
a

file3.
abcd1234
asdfjkl;


The file I want:
xxxx1111:c:abcd1234:
xxxx1111:d:asdfjkl;:
bbbb2222:a::


How can I combine the files?????

Thanks,

jcd...
If I had only read the instructions first??
6 REPLIES 6
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Easy Script

Easy

paste -d : file1 file2 ... filen > outfile

Man paste for details.
If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: Easy Script

Hi Joesph:

# paste -d ":" file1 file2 file3 ...

Regards!

...JRF...
Hein van den Heuvel
Honored Contributor

Re: Easy Script


Paste is probably the way to go.

To produce EXACTLY the requested result just add /dev/null as a final file to force the final seperator.

$ paste -d : file* /dev/null
xxxx11111:c:abcd1234:
xxxx11111:d:asdfjkl;:
bbbb22222:a::

If you need more elaborate logic in the paste operation (dealing with empty lines?), then you may want to script it, perhaps in perl. For example:

-------- paste.p --------
$sep = shift @ARGV;
while () {
$i=0;
open F,"<$_";
while (){
chop;
@lines[$i++] .= $_ . $sep;
}
@lines[$i++] .= $sep while ($i < $m);
$m = $i;
}
print "$_\n" foreach @lines;


----------- sample usage ----------

$ perl paste.p : file*
xxxx11111:c:abcd1234:
xxxx11111:d:asdfjkl;:
bbbb22222:a::


fwiw,
Hein.
Muthukumar_5
Honored Contributor

Re: Easy Script

pr -ms: -t file*

or

pr -ms: -t file1 file2 file3 ... filen

will do it.

-Muthu
Easy to suggest when don't know about the problem!
Joseph C. Denman
Honored Contributor

Re: Easy Script

Thanks folks. Ya'll are the best!!! I guess that is why all the head gear!@#$%&*(

Thanks Again,

...jcd...
If I had only read the instructions first??
Joseph C. Denman
Honored Contributor

Re: Easy Script

Thread Closed. Solutions above.
If I had only read the instructions first??