使用awk命令替换字符串 [英] string replacement using awk command

查看:1224
本文介绍了使用awk命令替换字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我上一篇文章使用awk命令进行字符串替换的延续.不能100%完全解决..使其更加清晰..

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)

要求1

如果我没有查找数据(例如,对于"u_no"不在参考文件中,则当前解决方案将打印一个空字段.

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

要求2

如果源文件或参考文件中的数据混合在一起,则仍应正确进行替换.

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

也就是说,当ref文件这样更改时:

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

为此,我得到了

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

我希望搜索和替换基于块aaaa,bbbb,zzzz,etc..而不是顺序进行. (例如,此处bbbbpwd值应为2b2b而不是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).

源文件(src.txt)

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

注意bbbbuidpwd值与src文件中的zzzz相同.

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

参考文件(ref.txt)

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

注意:参考文件2中的记录顺序也可能会发生变化,正如我在要求2中提到的那样.uidpwdbbbb的查找值与zzzz的查找值不同.但我需要如下结果:-

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:-

必需的输出文件(src_out.txt)(包括要求1和2)

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

代码

$ 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

这对我的最初期望来说很好用,但是现在添加了我在此处指定的requirement 1requirement 2,因此我需要针对这些的解决方案.

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.

推荐答案

这是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

这篇关于使用awk命令替换字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆