string replacement using awk command

匆匆过客 提交于 2020-01-25 20:38:55

问题


This is a continuation to my previous post 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.


回答1:


Here is one solution with awk

awk -F= '
FNR==NR {
    split($0,b,",")
    a[b[1] FS b[2]]=b[3]
    next} 
!/=/ {
    f=$1
    print
    next} 
    {
    print $1"="(a[f FS $1]?a[f FS $1]:$2)}
' ref.txt src.txt
aaaa
uid=1a1a
pwd=1b1b
u_no=12345
bbbb
uid=2a2a
pwd=2b2b
zzzz
uid=9a9a
pwd=9b9b



回答2:


Tweaked to test for the presence of the src values in the ref file:

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

$ awk -f tst.awk ref.txt src.txt
aaaa
uid=1a1a
pwd=1b1b
u_no=12345
bbbb
uid=2a2a
pwd=2b2b
zzzz
uid=9a9a
pwd=9b9b


来源:https://stackoverflow.com/questions/29012648/string-replacement-using-awk-command

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!