Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Awk > Re: Inserting f...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 4 of 7 Topic 2198 of 2236
Post > Topic >>

Re: Inserting fields by reading and comparing two files? -->

by Ed Morton <morton@[EMAIL PROTECTED] > Mar 20, 2008 at 11:27 PM

On 3/20/2008 5:55 PM, Grant wrote:
> On Thu, 20 Mar 2008 15:23:36 -0700 (PDT), google@[EMAIL PROTECTED]
 wrote:
> 
> 
>>Hey AWK Folks!
>>I'm stumped... Here's what I'm trying to do. I have two files,
>>1) names.out with three fields space delimited. User Login ID,
>>Firstname, & Lastname IE:
>>	jjd John Doe
>>	jdd Jane Doe
>>	sae Someone Else
>>	... etc ...
>>2) masterfile.out with 3 fields colon delimited. User Login
>>ID:null:department number IE:
>>	jjd:null:43
>>	jdd:null:25
>>	sae:null:9
>>	... etc ...
>>
>>What I need to do is fill all the null's in masterfile.out with first
>>and last name found in my names.out file. I would assume we would have
>>to open both files for read, compare the first field between the two
>>files and if the User Login ID matches, take fields $2 & $3 ( First
>>and Last name ) from names.out and copy to $2 of masterfile.out, if
>>the User Login ID does not match simply skip.
>>
>>I have over 1,400 lines to go through for which only about 1,200
>>should match and fill the names in.
>>
>>Is this something easy to do with awk? I've attempted many scripts but
>>I just can't wrap my head around this:(
> 
> 
> Like this?
> 
> grant@[EMAIL PROTECTED]
 cat namefile
> jjd John Doe
> jdd Jane Doe
> sae Someone Else
> grant@[EMAIL PROTECTED]
 cat masterfile
> jjd:null:43
> jdd:null:25
> sae:null:9
> grant@[EMAIL PROTECTED]
 cat mixemup
> #!/usr/bin/gawk -f
> #
> # read names
> NR == FNR { names[$1] = $2 " " $3 }
> 
> # read masterfile and print
> NR != FNR {
>         split($0, mf, ":")
>         if (names[mf[1]]) {

ITYM:

	if (mf[1] in names) {

but that'd delete lines fom masterfile if the name didn't exist in
namefile
which may not be desirable.

>                 print mf[1] ":" names[mf[1]] ":" mf[3]
>         }
> }
> grant@[EMAIL PROTECTED]
 ./mixemup namefile masterfile
> jjd:John Doe:43
> jdd:Jane Doe:25
> sae:Someone Else:9
> 
> Grant.




 7 Posts in Topic:
Inserting fields by reading and comparing two files? -->
google@[EMAIL PROTECTED]   2008-03-20 15:23:36 
Re: Inserting fields by reading and comparing two files? -->
Ed Morton <morton@[EMA  2008-03-20 17:30:40 
Re: Inserting fields by reading and comparing two files? -->
Grant <g_r_a_n_t_@[EMA  2008-03-21 09:55:45 
Re: Inserting fields by reading and comparing two files? -->
Ed Morton <morton@[EMA  2008-03-20 23:27:59 
Re: Inserting fields by reading and comparing two files? -->
Gecko12332 <google@[EM  2008-03-20 18:17:53 
Re: Inserting fields by reading and comparing two files? -->
Ed Morton <morton@[EMA  2008-03-20 21:39:45 
Re: Inserting fields by reading and comparing two files? -->
Ed Morton <morton@[EMA  2008-03-20 21:51:48 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat May 17 1:12:54 CDT 2008.