1845992 Members
3841 Online
110252 Solutions
New Discussion

Re: paste two files

 
SOLVED
Go to solution
Nik
Advisor

paste two files

Hi,
Thanks to these forums I have found that I can merge two 1 column files into one 2 column file but using the 'paste' command..
My question is, is there anyway of matching the lines up?

so for instance, if I have two columns:

line1 line1
line2 line2
line3 line4
line4 line5
line5

Is there anyway I can get it to paste them together as:

line1 line1
line2 line2
line3
line4 line4
line5 line5

Thanks!
20 REPLIES 20
RikTytgat
Honored Contributor

Re: paste two files

Hi,

What criterion would you use to decide whether or not a line in file1 matche a line in file2??

Bye,
Rik.
Dan Hetzel
Honored Contributor

Re: paste two files

Hi Nik,

If your two files have a common field, then you could ude the 'join' command.

Dan
Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com
Nik
Advisor

Re: paste two files

Hi,
It must be identical...
say it's a list of hostname on systems... I want to ensure no hosts are missing from one and not in the other.. unfortunatly no file is the master.. some hosts are in one, and not in another and vice versa..

thanks.
RikTytgat
Honored Contributor

Re: paste two files

Hi,

In that case, I woulkd use the comm command to identify the differences between two (sorted) files.

Bye,
Rik
Dan Hetzel
Honored Contributor

Re: paste two files

Hi Nik,

If you want to keep just one instance of every line, you could use:
cat file1 file2 | sort -u > file3
This would create a file, superset of file1 and file2 without duplicate entries.

Dan


Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com
Nik
Advisor

Re: paste two files

Rik,
thanks, 'comm' is good, but I would really like to just match the lines and output to a new file with 2 columns.
So for instance...
If there's a host called "machine1" in file 1, but not in file 2, just print it on one line, however if there is a host called "machine2" in file1 AND file2 then print them next to each other..
basically I've sorted both lists in telephone directory order using 'sort -d' and would now just like to align matching entries against each other...

hmm... maybe it's not possible without a big script!

thanks anyway.. points on way!
Glen Olsen
Occasional Advisor

Re: paste two files

I think the best way to do this would be with a small script of 10 - 20 lines.

If you would like some help let me know and I will help or even write it for you. (I don't have a lot to do right now).
Nik
Advisor

Re: paste two files

that'd be great...
If you could put together a little script..
I'd be interested to see how it's done.

Nik
[why argue with someone who offering to write a script for you?] :)

RikTytgat
Honored Contributor

Re: paste two files

Nik,

Here (attached to ths post - click on the papar-clip) is a little script that does what you asked. Mind though, it does not do anything when a line in in file2 and not in file1 (but you didn't ask it, did you?)

Bye,
Rik
RikTytgat
Honored Contributor

Re: paste two files

Nik,

This time the attachment is included!!!

Here (attached to ths post - click on the papar-clip) is a little script that does what you asked. Mind though, it does not do anything when a line in in file2 and not in file1 (but you didn't ask it, did you?)

Bye,
Rik
Glen Olsen
Occasional Advisor

Re: paste two files

OK a couple of questions first

Do you need a file to be produced as you have indercated above

or

would yout like to scripts to say that "hosname is in file1 but not 2" etc

or

if the hostname is not in file2 but is in file 1 the add it to file 2

the choice is yours :-)
Nik
Advisor

Re: paste two files

Hi Glen,
If I could just get a file created with the two columns...
if it's only in file1, create the entry on one line on the left, if it's only in file2 then create the entry on one line on the right..
if it's in both, create both entrys left and right..

machine1
machine2
machine3 machine3
machine4
machine5

hope this makes sense!

thanks,
Nik

Rik: I'm trying your script now.. I might be able to adjust it to get results mentioned above? thanks.
RikTytgat
Honored Contributor
Solution

Re: paste two files

Nik,

Here is another one that does it all. It uses arrays, but this is no problem in the sh-posix shell (/usr/bin/sh).

Bye,
Rik
Nik
Advisor

Re: paste two files

Rik,
That's brilliant! But it won't stop!
Tried (( iContinue == FALSE )) instead of single = but no luck...

Nik
RikTytgat
Honored Contributor

Re: paste two files

Nik,

You're right.

It doesn't stop (except when to files have equal number of lines -- incidentally the way I tested it). Let me have a look, and I'll post a correction if OK.

Bye,
Rik.
RikTytgat
Honored Contributor

Re: paste two files

Nik,

BTW, thanks for making me a Wizard!!

Bye.
Glen Olsen
Occasional Advisor

Re: paste two files

HI

Try this one as well

It a bit bigger than it 10 - 20 lines but it does it in a way that the files stay sorted
RikTytgat
Honored Contributor

Re: paste two files

Nik,

It does finish now!! Even when different number of lines in the files. And it still works with equal number of lines in the input files.

Bye,
Rik
Dan Hetzel
Honored Contributor

Re: paste two files

Hi Nik,

Combining 'comm' and 'sed' will do the job.

comm file1 file2 | sed 's/\(\)\(.*$\)/\2\2/'

Replace the '' by a real tab character in your script.

Best regards,

Dan
Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com
Nik
Advisor

Re: paste two files

Thanks for everyones help.