Bash脚本打印模式1,搜索并打印从模式2到模式3的所有行,并打印模式4。

时间:2023-01-14 15:30:35

Please Help - I'm very rusty with my sed/awk/grep and I'm attempting to process a file (an export of a PDF that is around 4700 pages long).

请帮忙——我对我的sed/awk/grep非常生疏,我正在处理一个文件(一个大约4700页长的PDF输出)。

Here is what I'm attempting to do: search/print line matching pattern 1, search for line matching pattern 2 and print that line and all lines from it until pattern 3 (if it includes/prints the line with pattern 3, I'm ok with it at this point), and search/print lines matching pattern 4.

我试图做的:搜索/打印线匹配模式1、搜索匹配模式2和打印线和所有台词,直到模式3(如果它包括/打印模式3,与它在这一点上我很好),和搜索/打印线匹配模式4。

All of the above patterns should occur in order (pattern 1,2,3,4) several hundred times in the file and I need to keep them in order.

所有上述模式都应该在文件中按顺序出现(模式1、模式2、模式3、模式4)数百次,我需要保持它们的顺序。

Pattern 1: lines beginning with 1-5 and a whitespace (this is specific enough despite it seeming vague) Pattern 2: lines beginning with (all caps) SOLUTION: Pattern 3: lines beginning with (all caps) COMPLIANCE: Pattern 4: lines beginning with an IP Addresses

模式1:以1-5和空格开头的行(尽管看起来很模糊)模式2:以(all caps)解决方案开头的行:模式3:以(所有大写)遵从性开始的行:模式4:以IP地址开头的行。

Here's what I've cobbled together, but it's clearly not working:

这是我拼凑出来的,但显然行不通:

#!/bin/bash
#
sed '

/^[1-5]\s/p {

       /^SOLUTION/,/^COMPLIANCE/p {

                /^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/p }

}' sample.txt

2 个解决方案

#1


1  

to use p in sed you need to use -n as well and also add -r for extended regex:

要在sed中使用p,您还需要使用-n,并为扩展regex添加-r:

Here is how it should look like:

它应该是这样的:

sed -r -n '{
/^[1-5] /p
/^SOLUTION/,/^COMPLIANCE/p
/^([0-9]{1,3}[\.]){3}[0-9]{1,3}/p
}' sample.txt

#2


1  

You probably want something like this, untested since you didn't provide any sample input or expected output:

你可能想要这样的东西,未经测试,因为你没有提供任何样本输入或期望输出:

awk '
BEGIN         { state = 0 }
/^[1-5] /     { if (state ~ /[01]/) { block = $0; state = 1 } }
/^SOLUTION/   { state = (state ~ /[12]/ ? 2 : 0) }
state == 2    { block = block ORS $0 }
/^COMPLIANCE/ { state = (state == 2 ? 3 : state) }
/^([0-9]{1,3}\.){3}[0-9]{1,3}/ { if (state == 3) { print block ORS $0; state = 0 } }
' file

#1


1  

to use p in sed you need to use -n as well and also add -r for extended regex:

要在sed中使用p,您还需要使用-n,并为扩展regex添加-r:

Here is how it should look like:

它应该是这样的:

sed -r -n '{
/^[1-5] /p
/^SOLUTION/,/^COMPLIANCE/p
/^([0-9]{1,3}[\.]){3}[0-9]{1,3}/p
}' sample.txt

#2


1  

You probably want something like this, untested since you didn't provide any sample input or expected output:

你可能想要这样的东西,未经测试,因为你没有提供任何样本输入或期望输出:

awk '
BEGIN         { state = 0 }
/^[1-5] /     { if (state ~ /[01]/) { block = $0; state = 1 } }
/^SOLUTION/   { state = (state ~ /[12]/ ? 2 : 0) }
state == 2    { block = block ORS $0 }
/^COMPLIANCE/ { state = (state == 2 ? 3 : state) }
/^([0-9]{1,3}\.){3}[0-9]{1,3}/ { if (state == 3) { print block ORS $0; state = 0 } }
' file