如何解决获取sed提取两点之间的文本,但在第二次出现字符串时停止 使用sed 使用awk
我有一个文本文件,其中包含许多电源数据。我要提取一个看起来像这样的部分:
scan 170110 fission power at tpd 220635.7 total power 107.127
Triad3PC-ScanPower v1.1.1.1 ld=2007-03-27
--------------------------------------------------------------------------------------------------
burnup type: measbu lattice split: on discontinuity factors: normal
--------------------------------------------------------------------------------------------------
y z a b c d e f g h j k l m n o p q r s t
34 ... 34
33 ... ... ... ... 33
32 ... ... ... ... ... 32
31 ... ... ... ... ... ... 31
30 ... ... ... ... ... ... ... 30
29 ... ... 1.280 1.393 1.232 ... ... ... 29
28 ... 0.597 1.390 ... 1.289 ... ... 28
27 ... 0.534 1.113 ... ... ... ... ... 27
26 ... ... 1.536 ... ... 0.706 ... ... ... 26
25 ... ... 1.207 1.128 ... ... 0.784 ... ... ... 25
24 ... ... ... 0.791 0.831 ... ... 0.733 ... 24
23 ... ... ... ... ... 1.069 ... ... 1.226 ... 23
22 ... ... ... 0.945 1.204 0.841 ... 0.687 ... 22
21 ... ... 1.352 ... 0.826 ... ... 1.134 1.383 ... 21
20 ... ... ... 2.246 ... 1.639 1.316 ... ... ... ... 20
19 ... 1.214 0.953 1.551 1.434 ... 1.229 1.048 ... ... 19
18 ... ... 1.256 0.824 ... ... 1.151 1.132 ... 1.552 ... 18
17 ... 0.701 1.311 ... 1.796 1.492 ... 2.365 1.101 ... 17
16 ... ... ... ... ... ... ... 0.970 1.042 1.467 ... 16
15 ... 1.234 ... 0.863 1.574 ... 1.050 0.923 ... ... 15
14 ... ... ... ... ... 1.642 1.571 ... 1.090 1.473 ... 14
13 ... ... 1.062 1.266 1.679 1.599 0.814 ... 1.506 ... 13
12 ... ... 1.430 ... ... ... ... ... ... ... ... 12
11 ... ... 0.978 ... 1.342 1.375 ... ... 1.318 ... 11
10 ... ... ... 0.963 ... 1.066 ... 0.783 0.480 10
9 ... 1.056 ... 1.291 ... ... 0.595 ... 0.145 ... 9
8 ... 1.230 ... ... 0.760 ... ... ... ... 8
7 ... ... 1.313 ... 0.907 ... 0.762 1.534 ... ... 7
6 ... ... 1.306 ... ... ... 1.292 ... ... 6
5 ... ... 1.012 0.818 ... ... ... ... 5
4 ... ... 1.252 ... 1.299 ... ... 4
3 ... ... ... 1.221 1.145 1.109 ... ... 3
2 ... ... ... ... ... ... ... 2
1 ... ... ... ... ... ... 1
0 ... ... ... ... ... 0
-1 ... ... ... ... -1
-2 ... -2
y z a b c d e f g h j k l m n o p q r s t
我的sed命令是:
sed -n '/fission power at tpd/,/ y z a b c/ { p; }' s.171012.so.power.info
但是sed最终停止在“ y z a b c”的第一个实例处
scan 171012 fission power at tpd 239802.1 total power 109.976
Triad3PC-ScanPower v1.1.1.1 ld=2007-03-27
--------------------------------------------------------------------------------------------------
burnup type: measbu lattice split: on discontinuity factors: normal
--------------------------------------------------------------------------------------------------
y z a b c d e f g h j k l m n o p q r s t
我希望它在地图底部的第二个实例处停止。我如何告诉它做到这一点?
解决方法
使用sed
尝试:
sed -n '/fission power at tpd/,/ y z a b c/{/ y z a b c/!p;}; / y z a b c/,/ y z a b c/ { p; }' s.171012.so.power.info
为便于阅读,请考虑以下测试文件:
$ cat file.info
0
begin
1
head
2
3
head
4
我们的命令产生以下输出:
$ sed -n '/begin/,/head/{/head/!p;}; /head/,/head/p' file.info
begin
1
head
2
3
head
/begin/,/head/{/head/!p;}
从包含begin
的行开始打印,直到但不包括包含head
的第一行。 /head/,/head/p
从包含head
的行打印到包含head
的下一行。
使用awk
由于awk了解算术,我们可以计算包含head
的行的出现次数:
$ awk '/begin/{f=1}; f && g<2; f && /head/{g++}' file.info
begin
1
head
2
3
head
与sed一样,awk一次处理输入文件一行。在这里,当我们到达包含f
的行时,将awk变量begin
设置为1(true)。如果f
为true并且g
小于2(表示少于两次head
),我们将打印该行。如果f
为true并且行包含head
,我们将递增g
。
这可能对您有用(GNU sed):
sed -n '/fission power at tpd/{:a;N;s/^.*y z a b c.*/&/M2p;Ta}' file
使用-n
选项关闭显式打印,即更像grep。
将模式空间中的行从包含fission power at tpd
的行收集到另一行,这是包含y z a b c
的行的第二次出现。
该解决方案在多行模式下使用替代命令,并在出现两次时替换自己匹配的行。成功替换命令将设置一个内部标志,如果未设置内部标志,则T
命令将程序流传输到循环标签。因此,循环将在模式空间中收集线,在进行匹配时将其打印并退出循环。
sed是最好的工具。这不是您要执行的操作,因此会选择一个糟糕的选择来尝试将其用于您的应用程序。在每个UNIX框的任何外壳中使用任何awk:
awk '/fission power at tpd/{f=1} f{print; if ((/y z a b c/) && (++c == 2)) exit}' file
,
解决方案是缓冲读取的行,直到遇到停止模式(在这种情况下应将其输出)或直到遇到文件末尾(在这种情况下,自上一个停止模式以来的已缓冲的行都将被丢弃) 。以下解决方案使用~
作为保留空间内“已缓冲要输出”行和“已缓冲但不打算输出”行之间的分隔符:
start="fission power at tpd"
stop=" y z a b c"
sed -n '
# Filter the starting pattern
/'"$start"'/,${
# Add the separator to and hold the line
s/$/~/; h;
# foreach line
:again; n;
# We hold the line unconditionally,cause its after ~ separator
H;
# If its stopping pattern
/'"$stop"'/{
# Move the ~ separator to the end of hold buffer
x;s/~//;s/$/~/;x;
};
# If its end of file
${
# output from the hold buffe only the part before ~ separator
x;s/~.*//;p;q;
};
b again;
}' input
还有一个班轮:
sed -n '/'"$start"'/,${ s/$/~/; h; :again; n; H; /'"$stop"'/{x;s/~//;s/$/~/;x;}; ${ x;s/~.*//;p;q; }; b again; }' input
通过在模式缓冲区上充当“不输出行”缓冲区,您可以在容纳空间内没有分隔符的情况下执行此操作。这会导致stop
模式与整行不匹配,因此在编写模式时必须记住不要使用^
锚点,而不能使用\n
或更多\(^\|\n\)[^\n]*some_pattern[^\n]*$
sed -n '
# Filter from the starting pattern to the of file
/'"$start"'/,${
# Hold the starting line and read next line
h; n;
# Foreach line
:again; {
# If stopping pattern is found
/'"$stop"'/{
# Add everything up until now to hold space
H;
# Now we could be potentially at end of file here - check it before reading next line
$!{
# Clear pattern space,read next line and restart
n;
b again;
}
};
# If end of file
${
# print the content of hold space and quit
x;p;q;
};
# Add next line to pattern space
N;
}; b again;
}' input
还有一个班轮:
sed -n '/'"$start"'/,${h; n; :again; /'"$stop"'/{ H; $!{ n; b again; }; }; ${ x;p;q; }; N; b again; }' input
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。