Line/string 操纵
Line/string manipulation
当涉及到 regex/perl/string 操纵时,这里的社区多次 helped/taught 给我很大的帮助。正文如下:
===================================
| IO Statistics |
| |
| Interval size: 2.254 secs (dur) |
| Col 1: Frames and bytes |
|---------------------------------|
| |1 |
| Interval | Frames | Bytes |
|---------------------------------|
| 0.000 <> 2.254 | 10 | 1709 |
===================================
我想要的:
Time: 2.254 Seconds
Frames: 10
Bytes: 1709
有什么帮助吗?谢谢!!!
对于特定的文本,您可以使用:
pax> awk '
...> =="(dur)" {print "Time: "" Seconds"}
...> =="<>" {print "Frames: ""\nBytes: "}' inFile
Time: 2.254 Seconds
Frames: 10
Bytes: 1709
这是从 Interval size
行获取持续时间,从包含 <>
的行获取其他两个数字。
如果您的要求比这更复杂,则需要将它们拼写出来。例如,假设您想从以下(稍作修改的)文件的 <>
行获取信息:
pax> cat infile
===================================
| IO Statistics |
| |
| Interval size: 5.000 secs (dur) |
| Col 1: Frames and bytes |
|---------------------------------|
| |1 |
| Interval | Frames | Bytes |
|---------------------------------|
| 0.000 <> 1.234 | 10 | 1234 |
| 1.234 <> 2.718 | 20 | 9876 |
| 2.718 <> 3.142 | 42 | 42 |
| 3.142 <> 5.000 | 99 | 97 |
===================================
然后您可以使用类似的东西:
pax> awk '
...> =="<>" {
...> print "Time: "(-)" Seconds";
...> print "Frames: """;
...> print "Bytes: ""\n"
...> }' inFile
Time: 1.234 Seconds
Frames: 10
Bytes: 1234
Time: 1.484 Seconds
Frames: 20
Bytes: 9876
Time: 0.424 Seconds
Frames: 42
Bytes: 42
Time: 1.858 Seconds
Frames: 99
Bytes: 97
关于如何处理其他输入有点模棱两可,但在 perl 中(在命令行上作为 ~one-liner):
#| 0.000 <> 2.254 | 10 | 1709 |
perl -ie '
if (m:^\|\s+(\d+\.\d+)\s+<>\s+(\d+\.\d+)\s+\|\s+(\d+)\s+\|\s+(\d+)\s+\|:){
print "Time: ".(-)." Seconds\nFrames: \nBytes: \n\n"
}' infile
实用的解决方案:
tail -n 2 file |
awk '{ printf "Time: %s seconds\nFrames: %s\nBytes: %s\n", , , ; exit }'
- 假设感兴趣的行是倒数第二个输入行。
- 从该行中提取第 4、第 6 和第 8 个字段并使用
printf
格式化输出。
当涉及到 regex/perl/string 操纵时,这里的社区多次 helped/taught 给我很大的帮助。正文如下:
===================================
| IO Statistics |
| |
| Interval size: 2.254 secs (dur) |
| Col 1: Frames and bytes |
|---------------------------------|
| |1 |
| Interval | Frames | Bytes |
|---------------------------------|
| 0.000 <> 2.254 | 10 | 1709 |
===================================
我想要的:
Time: 2.254 Seconds
Frames: 10
Bytes: 1709
有什么帮助吗?谢谢!!!
对于特定的文本,您可以使用:
pax> awk '
...> =="(dur)" {print "Time: "" Seconds"}
...> =="<>" {print "Frames: ""\nBytes: "}' inFile
Time: 2.254 Seconds
Frames: 10
Bytes: 1709
这是从 Interval size
行获取持续时间,从包含 <>
的行获取其他两个数字。
如果您的要求比这更复杂,则需要将它们拼写出来。例如,假设您想从以下(稍作修改的)文件的 <>
行获取信息:
pax> cat infile
===================================
| IO Statistics |
| |
| Interval size: 5.000 secs (dur) |
| Col 1: Frames and bytes |
|---------------------------------|
| |1 |
| Interval | Frames | Bytes |
|---------------------------------|
| 0.000 <> 1.234 | 10 | 1234 |
| 1.234 <> 2.718 | 20 | 9876 |
| 2.718 <> 3.142 | 42 | 42 |
| 3.142 <> 5.000 | 99 | 97 |
===================================
然后您可以使用类似的东西:
pax> awk '
...> =="<>" {
...> print "Time: "(-)" Seconds";
...> print "Frames: """;
...> print "Bytes: ""\n"
...> }' inFile
Time: 1.234 Seconds
Frames: 10
Bytes: 1234
Time: 1.484 Seconds
Frames: 20
Bytes: 9876
Time: 0.424 Seconds
Frames: 42
Bytes: 42
Time: 1.858 Seconds
Frames: 99
Bytes: 97
关于如何处理其他输入有点模棱两可,但在 perl 中(在命令行上作为 ~one-liner):
#| 0.000 <> 2.254 | 10 | 1709 |
perl -ie '
if (m:^\|\s+(\d+\.\d+)\s+<>\s+(\d+\.\d+)\s+\|\s+(\d+)\s+\|\s+(\d+)\s+\|:){
print "Time: ".(-)." Seconds\nFrames: \nBytes: \n\n"
}' infile
实用的解决方案:
tail -n 2 file |
awk '{ printf "Time: %s seconds\nFrames: %s\nBytes: %s\n", , , ; exit }'
- 假设感兴趣的行是倒数第二个输入行。
- 从该行中提取第 4、第 6 和第 8 个字段并使用
printf
格式化输出。