且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从一个文件中shellscript里另一个文件值:更换钥匙?

更新时间:2023-02-22 11:59:37

 的awk'
    NR == FNR {网址[$ 1] = $ 2;下一个}
    {为(i = 1; I< = NF;我+ +)如果($ i的网址)$ I =网址[$ i];打印}
keys.txt的text.txt

  notrelated somewhatrelated http://thisis.example.com/moooooar.asp
http://thisis.example.com/moooooar.asp不重要
航空自卫队航空自卫队航空自卫队https://maps.google.com dadadada

I have a "database file" with key-value entries and I have another file where the keys appear among other content. Now I want to replace that keywords with the values of my key-value file. Example:

There is one file "keys.txt" with "keyword space url":

name1 https://maps.google.com
something http://www.domain.com/bla.php?value=500
blabla http://thisis.example.com/moooooar.asp

I have another file "text.txt" which has one or several words per line, like this:

notrelated somewhatrelated blabla
blabla unimportant
asdf asdf asdf name1 dadadada

And as a result I want to have something like this:

notrelated somewhatrelated http://thisis.example.com/moooooar.asp
http://thisis.example.com/moooooar.asp unimportant
asdf asdf asdf https://maps.google.com dadadada

Actually my question is very similar to https://***.com/questions/5283653/ but non of the solutions mentioned there did work out since I am working with URLs (slashes, colons...) I guess?

awk '
    NR==FNR {url[$1]=$2; next} 
    {for (i=1; i<=NF; i++) if ($i in url) $i=url[$i]; print}
' keys.txt text.txt 

notrelated somewhatrelated http://thisis.example.com/moooooar.asp
http://thisis.example.com/moooooar.asp unimportant
asdf asdf asdf https://maps.google.com dadadada