简体   繁体   English

如何使用sed / awk命令剪切字符串并追加到后续行?

[英]How to use sed/awk commands to cut a string and append to the succeeding lines?

Need your help in fixing one issue which I am facing while writing a shell script. 在编写Shell脚本时需要您的帮助来解决我面临的一个问题。

In a text file, I have the content as below : 在一个文本文件中,我的内容如下:

Deployment ' wonderful ' 部署“ 精彩

mon_z1/0 (c99-2230-333) running mon_z1 / 0(c99-2230-333)正在运行

mon_z1/1 (24a90-00d) running mon_z1 / 1(24a90-00d)正在运行

mon_z1/2 (4b2-86a5fb2) running mon_z1 / 2(4b2-86a5fb2)正在运行

Deployment ' rainbow ' 部署“ 彩虹

post_m (333-33-22sd) running post_m(333-33-22sd)正在运行

post_s (8-333-33d) running post_s(8-333-33d)正在运行

=================================== ==================================

I am looking for an output something like this - where deployment name should append to the running states : Example : 我正在寻找类似这样的输出-部署名称应附加到运行状态:示例:

wonderful : mon_z1/0 (c99-2230-333) running :正在运行mon_z1 / 0(c99-2230-333)

wonderful : mon_z1/1 (24a90-00d) running :mon_z1 / 1(24a90-00d)运行

wonderful : mon_z1/2 (4b2-86a5fb2) running :正在运行mon_z1 / 2(4b2-86a5fb2)

rainbow : post_m (333-33-22sd) running 彩虹 :post_m(333-33-22sd)跑步

rainbow : post_s (8-333-33d) running 彩虹 :post_s(8-333-33d)跑步

Please help how I can achieve with sed or awk commands 请帮助我如何使用sed或awk命令实现

awk solution: awk解决方案:

awk '/Deployment/{ gsub("\047","",$2); d=$2; next }$3~/running/{ print d,":",$0 }' file

The output: 输出:

wonderful : mon_z1/0 (c99-2230-333) running
wonderful : mon_z1/1 (24a90-00d) running
wonderful : mon_z1/2 (4b2-86a5fb2) running
rainbow : post_m (333-33-22sd) running
rainbow : post_s (8-333-33d) running

  • /Deployment/{...} - perform an action on encountering Deployment line /Deployment/{...} -在遇到Deployment行时执行操作

  • gsub("\\047","",$2) - remove single quotes from Deployment name gsub("\\047","",$2) -从部署名称中删除单引号

  • d=$2 - capture Deployment name d=$2捕获部署名称

  • $3~/running/ - on encountering line with running state - print the line with related Deployment name $3~/running/ -在遇到具有运行状态的行时-打印具有相关部署名称的行

Here is another awk solution: 这是另一个awk解决方案:

awk 'BEGIN{FS="\047"}/^Deployment/{ x = $2; next }$0 != ""{ print x, ":" ,$0 }' file 

Output: 输出:

wonderful : mon_z1/0 (c99-2230-333) running
wonderful : mon_z1/1 (24a90-00d) running
wonderful : mon_z1/2 (4b2-86a5fb2) running
rainbow : post_m (333-33-22sd) running
rainbow : post_s (8-333-33d) running

It is similar to the one from RomanPerekhrest. 它与RomanPerekhrest的类似。 The difference is that I'am filtering out empty strings with: 区别在于我使用以下方法过滤出空字符串:

$0 != "" 

That takes care that the action is only performed on lines that are not empty. 这要注意,该操作仅在不为空的行上执行。

You asked for sed or awk, but I'm going to provide an example in perl which is easily translated to awk. 您要求使用sed或awk,但我将在perl中提供一个易于转换为awk的示例。 The trick is how you access the match (the name of the deployment). 诀窍是您如何访问匹配项(部署的名称)。 You can use a technique like RomanPerekhrest and remove the ' marks from the 2nd field, or you can use regular expression matching and take the deployment name from the match group. 您可以使用RomanPerekhrest之类的技术并从第二个字段中删除'标记,也可以使用正则表达式匹配并从匹配组中获取部署名称。 This is what I did, because it's very simple in perl. 这就是我所做的,因为在Perl中它非常简单。 As a command line: 作为命令行:

cat data | perl -e "use warnings; use strict; my \$deployment; while (<>) { if (\$_ =~ /^Deployment '(.*)'/) { \$deployment = \$1; } else { print(\"\$deployment: \$_\n\"); } }"

Or, rewritten for clarity: 或者,为了清楚起见,将其重写:

use warnings;
use strict;

my $deployment;

while (<>) {
    if ($_ =~ /^Deployment '(.*)'/) {
        $deployment = $1;
    } else {
        print("$deployment: $_\n");
    }
}

But do note that when you rewrite this in awk, you don't need the while loop. 但是请注意,当您用awk重写此代码时,不需要while循环。 Awk's main strength is that it implicitly loops over each line and splits it into fields. Awk的主要优势在于,它隐式循环遍历每行并将其拆分为多个字段。

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

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