This is a continuation to my previous post [string replacement using awk command](https://stackoverflow.com/questions/29012648/string-replacement-using-awk-command) which is not 100% fully resolved.. To make it clearer.. 

(The source file and code I am using are at the bottom of the post)

**requirement 1**

If I dont have a lookup data(Eg, for "u\_no" is not in the reference file, the current solution prints an empty field. 

```
aaaa
uid=1a1a
pwd=1b1b
u_no=

```

I want that to pick the value from the source file instead..

```
aaaa
uid=1a1a
pwd=1b1b
u_no=12345

```

**requirement 2**

If the data in the source file or reference file is mixed up, it should still do the replacement properly.

That is, when the ref file is changed like this:

```
block,parameter,value
aaaa,uid,1a1a
aaaa,pwd,1b1b
bbbb,uid,2a2a
zzzz,pwd,9b9b
zzzz,uid,9a9a
bbbb,pwd,2b2b

```

**For this, I am getting**

```
aaaa
uid=1a1a
pwd=1b1b
u_no=
bbbb
uid=2a2a
pwd=9b9b
zzzz
uid=9a9a
pwd=2b2b

```

\*\* I must get\*\* 

```
aaaa
uid=1a1a
pwd=1b1b
u_no=12345    #points to my req 1 
bbbb
uid=2a2a
pwd=2b2b
zzzz
uid=9a9a
pwd=9b9b

```

I want the search and replace to happen based on the blocks `aaaa,bbbb,zzzz,etc..` Not sequentially. (For eg, here the `pwd` value for `bbbb` should be `2b2b` and not `9b9b`).

**source file**(src.txt)

```
aaaa
uid=xxxx
pwd=nnnn
u_no=12345
bbbb
uid=yyyy
pwd=eeee
zzzz
uid=yyyy
pwd=eeee

```

Note `uid` and `pwd` value for `bbbb` is same as that of `zzzz` in `src` file.

**reference file**(ref.txt)

```
block,parameter,value
aaaa,uid,1a1a
aaaa,pwd,1b1b
bbbb,uid,2a2a
zzzz,pwd,9b9b
zzzz,uid,9a9a
bbbb,pwd,2b2b

```

Note: The order of records in the ref file might also change as I mentioned in requirement 2. The lookup value for `uid` and `pwd` for `bbbb` is different than that of `zzzz`. But I need the result as follows:-

**Required Output file**(src\_out.txt)(including the requirements 1 and 2)

```
aaaa
uid=1a1a
pwd=1b1b
u_no=12345
bbbb
uid=2a2a
pwd=2b2b
zzzz
uid=9a9a
pwd=9b9b

```

**Code**

```
$ cat tst.awk
BEGIN { FS = "[,=]+" }
NR==FNR {
    if (NR>1) {
        map[$1,$2] = $3
    }
    next
}
{
    if (NF==1) {
        key = $0
    }
    else {
        $0 = $1 "=" map[key,tolower($1)]
    }
    print
}

$ awk -f tst.awk ref.txt src.txt

```

This works fine for my initial expectation, but the `requirement 1` and `requirement 2` which I specified here are now added and so I need the solution for these.