简体   繁体   English

sed以某种方式写一行

[英]sed write a line in a certain way

I have this text file that have lines made in a certain format just like this next line 我有这个文本文件,其中的行以某种格式制作,就像下一行一样

bla bla name1=WORD1 bla    bla name2=WORD2 bla bla name3=WORD2

I want to extract WORD1 WORD2 WORD3 without all the bla bla and printing them with semicolon 我想提取WORD1 WORD2 WORD3而不用全部bla bla并用分号打印

WORD1;WORD2;WORD3

can this be done using only sed ? 可以仅使用sed完成此操作吗?

One way that handles a different key=value string. 一种处理不同的key=value字符串的方法。

Assuming infile with content: 假设infile与内容:

bla bla name1=WORD1 bla   noname=WORD4 bla name2=WORD2 bla bla name3=WORD3

And script.sed with content: script.sed内容。

## Add a newline character just before each word.
s/name[1-3]=\([^ ]*\)/\n\1/g;

## Remove all characters until each newline appended in previous command, so only
## words will be left, and insert a ';' between them.
s/[^\n]*\n\([^ ]*\)/\1;/g;

## Remove last ';'.
s/;[ ]*$//;

Run it like: 像这样运行:

sed -f script.sed infile

That yields: 产生:

WORD1;WORD2;WORD3

If WORD* always occur in this manner, you can use these two patterns [^=]*= and [^ ]* to match before and the word respectively. 如果WORD*总是以这种方式出现,则可以使用这两个模式[^=]*=[^ ]*分别匹配before和word。 ^ at the beginning of a group inverts the matching. 组开头的^反转匹配。 Something like this works in GNU sed: 像这样的东西在GNU sed中起作用:

sed -r 's/[^=]*=([^ ]*)[^=]*=([^ ]*)[^=]*=(.*)/\1;\2;\3/' infile

If you are interseted in awk, below would work. 如果您对awk感到困惑,则可以使用以下方法。

awk '{for(i=1;i<=NF;i++){if($i~/\=/){split($i,a,"=");if(p){p=p";"a[2]}else{p=a[2]}}}}END{print p}'

tested below: 测试如下:

> echo "bla bla name1=WORD1 bla    bla name2=WORD2 bla bla name3=WORD2" | awk '{for(i=1;i<=NF;i++){if($i~/\=/){split($i,a,"=");if(p){p=p";"a[2]}else{p=a[2]}}}}END{print p}'
WORD1;WORD2;WORD2
> 

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

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