匹配后搜索字符串

时间:2020-04-14 16:38:04

标签: perl awk sed grep

我有一个文件,其中包含多条netstat输出记录,我的示例文件看起来类似于以下内容。我想搜索ex的PID:3453,这是输出的一部分,我还想查看快照时间,以便可以找到PID是否存在于特定快照中。有什么想法吗?

zzz ***Sat Apr 11 03:00:26 UTC 2020
USER        PID   PPID PRI %CPU %MEM    VSZ   RSS WCHAN             S  STARTED     TIME COMMAND
test    1234  3445  19  2.4  1.9 4070932 3539756 futex_wait_queue_ S   Apr 04 04:00:17 test -quiet
test1    3453  6741  19  2.4  1.9 4070932 3539756 futex_wait_queue_ S   Apr 04 04:00:17 test -quiet

zzz ***Sat Apr 11 03:01:26 UTC 2020
USER        PID   PPID PRI %CPU %MEM    VSZ   RSS WCHAN             S  STARTED     TIME COMMAND
test    3453  3453  19  2.4  1.9 4070932 3539756 futex_wait_queue_ S   Apr 04 04:00:17 test -quiet
test1    7842  8712  19  2.4  1.9 4070932 3539756 futex_wait_queue_ S   Apr 04 04:00:17 test -quiet

搜索3453的预期示例输出:

zzz ***Sat Apr 11 03:00:26 UTC 2020
test1    3453  6741  19  2.4  1.9 4070932 3539756 futex_wait_queue_ S   Apr 04 04:00:17 test -quiet
zz ***Sat Apr 11 03:01:26 UTC 2020
test    3453  3453  19  2.4  1.9 4070932 3539756 futex_wait_queue_ S   Apr 04 04:00:17 test -quiet

1 个答案:

答案 0 :(得分:1)

使用GNU sed:

const PRIORITIES = {
    high: 'HIGH',
    low: 'LOW',
};
const NUM_TO_PRIORITY = {
    0: 'high',
    1: 'low',
};
const priorityNum = 0;
console.log(PRIORITIES[NUM_TO_PRIORITY[priorityNum]]); // "HIGH"
console.log(Object.values(PRIORITIES).includes(priorityNum)); // false
console.log("HIGH" || false) // "HIGH" <- Expected output

// Based on the above code, the following code should output "HIGH" yet outputs 0, why does this happen?
console.log(PRIORITIES[NUM_TO_PRIORITY[priorityNum]] || Object.values(PRIORITIES).includes(priorityNum) ? priorityNum : PRIORITIES.low);

输出:

zzz ***Sat Apr 11 03:00:26 UTC 2020
test1    3453  6741  19  2.4  1.9 4070932 3539756 futex_wait_queue_ S   Apr 04 04:00:17 test -quiet
zzz ***Sat Apr 11 03:01:26 UTC 2020
test    3453  3453  19  2.4  1.9 4070932 3539756 futex_wait_queue_ S   Apr 04 04:00:17 test -quiet

请参阅:sed -n '/\*\*\*/,/^$/{ /\*\*\*/p; /3453/p }' file The Stack Overflow Regular Expressions FAQ