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!

Merge two files, line after line 2

Status
Not open for further replies.

mewa

Technical User
Jul 27, 2001
72
DE
Hi,
I have 2 files: filea abd fileb.
filea:
aaa
bbb
ccc
ddd

fileb:
111
222
333
444

I want to get output file: filec.

filec:
aaa 111
bbb 222
ccc 333
ddd 444

Please, help.

Thanks,
mewa
 
Sorry,
Forgot to add that I want to get an output file only if the lines in filea and fileb differ.

Thanks a lot.

mewa
 
Probably a few ways to do this. One option is

paste filea fileb > filec

This will stick the files together. Then you could run the file through awk to remove unwanted lines, something like

awk -F" " '$1 != $2' filec > filed

The above awk solution is simplistic and makes no assumptions about the actual structure of data in your file. I'm sure it can be done fully in awk as well though :)

Greg.
 
Thanks. It works fine.
Is it possible to use only awk to open those 2 files, compare the lines and produce the output?

I tried to use BEGIN and getline within it, but failed.

mewa
 
There is a FAQ on this which may be of some help.

"How do I access more than one file at a time in awk? "

faq271-2571

Greg.
 
awk '{f=$0; getline < &quot;fileb&quot;; if (f != $0) print f, $0}' filea > filec
 
Thanks a lot!
That is exactly what I wanted to do.

mewa
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top