我有一个名为file的文件,其内容是
noise
noise
X noise STUFF1 noise STUFF2 noise
noise
Y noise STUFF3 noise
noise我断言X和Y是不同的,每个都在file中发生一次,X首先发生。
我能够发出一个sed命令来提取第一批信息,类似于
$ sed -n '/X/s/\(.*\)\(…\)\(.*\)\(…\)/\2 \4/p' < file
STUFF1 STUFF2
$ 还有一个类似的方法来提取STUFF3 (1),但我真正想做的是找到正确的sed咒语,以便
$ sed … < file
STUFF3 STUFF1 STUFF2
$(最后还可能学习!)sed的how缓冲器是如何工作的)。
(1)这不是一个关于正则表达式的问题,我知道如何隔离我所需要的文本片段。我需要保存我收集的信息,并在正确的时间输出它。
发布于 2022-05-21 12:24:44
使用sed
$ sed -n '/^X/{s/.[^[:upper:]]*\([[:alnum:]]*\)/\1 /g;h};/^Y/{s/.[^[:upper:]]*\([[:alnum:]]*\)/\1 /g;G;s/\n//p}' file
STUFF3 STUFF1 STUFF2$ cat script.sed
/^X/{ #Match line beginning with X
s/.[^[:upper:]]*\([[:alnum:]]*\)/\1 /g #As you know how to extract what you need, this is just for your sample data to extract needed strings
h #Retain the output of the substitution in the hold buffer
}
/^Y/{ #Match line beginning with Y
s/.[^[:upper:]]*\([[:alnum:]]*\)/\1 /g #Same as above
G #Append the contents of the hold space
s/\n//p #Remov the new line
}sed -nf script.sed file
STUFF3 STUFF1 STUFF2https://stackoverflow.com/questions/72329408
复制相似问题