使用awk/sed查找和替换文件中的键值对到其他 [英] Using awk / sed to find and replace key value pair from file to other
问题描述
我有一个名为key.txt
的键值文件,带有:
分隔符.
值的显示方式如下:
I have a key value file, named key.txt
, with a :
separator.
This is how values are displayed:
server_name:server1
username:someuser
keyname:123key
我的第二个文件是一个脚本文件,该文件在整个文件中都有这些键.它以以下格式显示在文件中的任何位置,与行或列无关:
My second file is a script file, which has these keys all over the file. It shows up in following format anywhere in file doesn't matter which line or column:
what is the servername '%%server_name%%'
Enter your username '%%username%%'
What is the ssh key value '%%keyname%%'
我想要做的是,当我运行命令时,它将在第二个文件中找到键server_name
,并将键%%server_name%%
替换为文件server1
中的值,但要确保它带有引号之前和之后.
What I want to do is when I run the command it will find the key server_name
in my second file and replace the key %%server_name%%
with value from file one which is server1
, but make sure it has quotes before and after.
所以我的新文件将如下所示:
So my new file will look like this:
what is the servername 'server1'
Enter your username 'someuser'
What is the ssh key value '123key'
我在论坛上查看时发现了此代码,但无法使其正常工作.
I looked on forum found this code but unable to make it work.
awk 'NR==FNR {url[$1]=$2; next} {for (i=1; i<=NF; i++) if ($i in url) $i=url[$i]; print}' key.txt file2.txt
+ xxd keys.txt
0000000: 7371 6c5f 7365 7276 6572 5f6e 616d 653a sql_server_name:
0000010: 7465 7374 5f73 6571 7365 7276 6572 5f31 test_seqserver_1
0000020: 3233 340d 0a0d 0a73 716c 5f6c 6f67 696e 234....sql_login
0000030: 5f6e 616d 653a 7465 7374 5f6c 6f67 696e _name:test_login
0000040: 5f6e 616d 650d 0a0d 0a70 6173 7377 6f72 _name....passwor
0000050: 643a 7465 7374 5f70 6173 7377 6f72 6420 d:test_password
0000060: 0d0a 0d0a 5349 443a 3132 3334 3536 200d ....SID:123456 .
0000070: 0a0d 0a64 6566 6175 6c74 5f64 6174 6162 ...default_datab
0000080: 6173 653a 7465 6d70 6462 0d0a 0d0a 6465 ase:tempdb....de
0000090: 6661 756c 745f 6c61 6e67 7561 6765 3a75 fault_language:u
00000a0: 735f 656e 676c 6973 680d 0a0d 0a63 6865 s_english....che
00000b0: 636b 5f65 7870 6972 6174 696f 6e3a 4f46 ck_expiration:OF
00000c0: 460d 0a0d 0a63 6865 636b 5f70 6f6c 6963 F....check_polic
00000d0: 793a 4f46 460d 0a0d 0a64 656c 6976 6572 y:OFF....deliver
00000e0: 7974 7970 653a 7363 6865 6475 6c65 640d ytype:scheduled.
00000f0: 0a0d 0a73 6368 6564 756c 6564 5f64 656c ...scheduled_del
0000100: 6976 6572 7964 6174 653a 3035 2d33 302d iverydate:05-30-
0000110: 3230 3939 0d0a 0d0a 7363 6865 6475 6c65 2099....schedule
0000120: 645f 6465 6c69 7665 7279 5f32 3468 725f d_delivery_24hr_
0000130: 6365 6e74 7261 6c5f 7469 6d65 3a31 3135 central_time:115
0000140: 3920 0d0a 0d0a 0d0a 0d0a 0d0a 0d0a 0d0a 9 ..............
推荐答案
您可以使用sed将key.txt
转换为一系列sed替换,然后在输入文件上运行它们:
You can use sed to turn key.txt
into a series of sed substitutions, then run these on your input file:
$ sed '/^[[:blank:]]*$/d;s|^|s/%%|;s|:|%%/|;s|$|/|' key.txt | sed -f - infile
what is the servername 'server1'
Enter your username 'someuser'
What is the ssh key value '123key'
第一个sed命令具有以下输出:
The first sed command has this output:
s/%%server_name%%/server1/
s/%%username%%/someuser/
s/%%keyname%%/123key/
然后,我们使用sed -f -
运行这些命令,其中-f
表示从文件读取命令",而特殊文件-
是标准输入.
Then we run these commands with sed -f -
, where -f
says "read commands from a file", and the special file -
is standard input.
仔细研究第一个命令的作用:
A closer look at what the first command does:
sed '
/^[[:blank:]]*$/d # Delete blank lines in key file
s|^|s/%%| # Insert "s/%%" at the start of the line
s|:|%%/| # Replace ":" with "%%/"
s|$|/| # Insert "/" at the end of the line
' key.txt
我将|
用作分隔符,而不是更常见的/
,因此我不必转义斜线:s/$/\//
也可以.
I'm using |
as delimiter instead of the more common /
so I don't have to escape my slashes: s/$/\//
would work, too.
这篇关于使用awk/sed查找和替换文件中的键值对到其他的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!