简体   繁体   English

bash sed如何追加到行首,与行首匹配的变量

[英]bash sed how to append to beginning of line, that matches variable at beginning of line

I'm having some trouble figuring out how to append at the beginning of each line if the beginning of the line matches the servername in a variable. 我很难弄清楚如果行的开头与变量中的服务器名匹配时如何在每行的开头进行追加。 Although nice to have but not required is if no site was matched up, to indicate "unknown-site" at the beginning of line. 虽然很高兴,但不是必须的,如果没有站点匹配,则在行的开头指示“未知站点”。

I do have a known list of machine names for each site. 我确实知道每个站点的计算机名称列表。

site1="server1 server2 ..."
site2="server3 ..."

I was thinking the code should be something like 我当时在想代码应该像

for each servername in $site1 do; sed command in file
for each servername in $site2 do; sed command in file

The raw data that was supplied to me 提供给我的原始数据

server1 data1...
server1 data2...
server3 data3...
server3 data2...
server2 data4...
server3 data3...
server2 data5...

And this should be the out put 这应该是输出

site1 server1 data1...
site1 server1 data2...
site2 server3 data3...
site2 server3 data2...
site1 server2 data4...
site2 server3 data3...
site1 server2 data5...

If you can arrange the server<>site mapping to one file ( servers ) like this: 如果您可以将server <>站点映射安排到一个文件( servers ),如下所示:

server1 site1
server2 site1
server3 site2

Then this becomes easy to do with awk : 然后,使用awk可以轻松实现:

$ awk 'FNR==NR {s[$1] = $2; next} {print s[$1], $0}' servers in
site1 server1 data1...
site1 server1 data2...
site2 server3 data3...
...

NR is the number of records (lines) seen by awk this far, FNR the number of records within this file seen this far. NR是迄今为止awk看到的记录(行)数, FNR是到目前为止该文件中看到的记录数。 Comparing them with FNR==NR is a common idiom for separating the first file from the rest. 将它们与FNR==NR进行比较是将第一个文件与其余文件分开的常见用法。 So, for each line of the first file, we set the values found to the array s , with the first field ( $1 ) as key, and the second ( $2 ) as value, then jump to next line. 因此,对于第一个文件的每一行,我们将找到的值设置为数组s ,以第一个字段( $1 )作为键,第二个字段( $2 )作为值,然后跳至next行。 (All arrays in awk are associative, and work with strings as keys/indexes.) Now we have the site names matching the servers in array s (eg s["server1"] == "site1" ) and for the subsequent files, we just use the first field as an index to the array, print the value found there, along with the full input line ( $0 ). awk中的所有数组都是关联的,并且使用字符串作为键/索引。)现在,我们拥有与数组s的服务器匹配的站点名称(例如s["server1"] == "site1" ),以及后续文件,我们只使用第一个字段作为数组的索引,打印在那里找到的值以及完整的输入行( $0 )。

We could also add a known marker (instead of empty) for the unknown ones by checking if s[$1] exists and is nonempty: 我们还可以通过检查s[$1]存在并且为非空来为未知标记添加一个已知标记(而不是空):

$ awk 'FNR==NR {s[$1] = $2; next} {if (s[$1]) print s[$1], $0; else print "unknown", $0;  next}' servers data.txt

With the server names as you had in the shell, you could produce the servers file with something like this: 使用外壳中的服务器名称,就可以生成如下所示的servers文件:

site1="server1 server2 ..."
site2="server3 ..."
true > servers        # to clear the file
for x in $site1 ; do echo "$x site1" ; done >> servers
for x in $site2 ; do echo "$x site2" ; done >> servers

Try this: 尝试这个:

awk -F '[ ="]+' 'NR==FNR {for (i=2; i<=NF; i++) { sites[$i]=$1; }; next }
                 { print sites[$1] " " $0 }' sites data

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM