Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
239 views
in Technique[技术] by (71.8m points)

shell - Remove block of lines from file depending on presence of string in preceding line, then later another

I have a 25-line file that looks like:
cat -n oz

1 PARSING IN CURSOR #140499 dep=0 tim=4217919222030 sqlid='ftf4q8xj38z7k'
2 WAIT #1404991: nam='SQL*Net message' ela= 1 tim=4217919224736
3 WAIT #1404991: nam='SQL*Net message' ela= 655 tim=4217919225409
4 PARSING IN CURSOR #140499 dep=0 tim=4217919225606 sqlid='9fufagwmu041b'
5 WAIT #1404991: nam='SQL*Net message' ela= 1 tim=4217919225677
6 WAIT #1404991: nam='SQL*Net message' ela= 736 tim=4217919226432
7 PARSING IN CURSOR #140499 dep=0 tim=4217919226577 sqlid='bzdm0nbr7c036'
8 WAIT #1404990: nam='SQL*Net message' ela= 1 tim=4217919226665
9 WAIT #1404990: nam='SQL*Net message' ela= 2027 tim=4217919228710
10 WAIT #1404991: nam='SQL*Net message' ela= 1 tim=4217919228849
11 PARSING IN CURSOR #140499 dep=1 tim=4217919225606 sqlid='9fufagwmu041b'
12 WAIT #1404991: nam='SQL*Net message' ela= 603 tim=4217919229470
13 WAIT #1404991: nam='SQL*Net message' ela= 1 tim=4217919229647
14 WAIT #1404991: nam='SQL*Net message' ela= 521 tim=4217919230185
15 WAIT #1404991: nam='SQL*Net message' ela= 0 tim=4217919230330
16 WAIT #1404991: nam='SQL*Net message' ela= 758 tim=4217919231107
17 WAIT #1404990: nam='SQL*Net message' ela= 0 tim=4217919231222
18 WAIT #1404990: nam='SQL*Net message' ela= 1635 tim=4217919232874
19 PARSING IN CURSOR #140499 dep=0 tim=4217919226577 sqlid='bzdm0nbr7c036'
20 WAIT #1404990: nam='SQL*Net message' ela= 1 tim=4217919226665
21 WAIT #1404990: nam='SQL*Net message' ela= 2027 tim=4217919228710
22 PARSING IN CURSOR #140499 dep=1 tim=4217919225606 sqlid='9fufagwmu041b'
23 WAIT #1404991: nam='SQL*Net message' ela= 521 tim=4217919230185
24 WAIT #1404991: nam='SQL*Net message' ela= 0 tim=4217919230330
25 WAIT #1404991: nam='SQL*Net message' ela= 758 tim=4217919231107

that I want to transform into ("cat -n" of same file once processed):
1 PARSING IN CURSOR #140499 dep=0 tim=4217919222030 sqlid='ftf4q8xj38z7k'
2 WAIT #1404991: nam='SQL*Net message' ela= 1 tim=4217919224736
3 WAIT #1404991: nam='SQL*Net message' ela= 655 tim=4217919225409
4 PARSING IN CURSOR #140499 dep=0 tim=4217919225606 sqlid='9fufagwmu041b'
5 WAIT #1404991: nam='SQL*Net message' ela= 1 tim=4217919225677
6 WAIT #1404991: nam='SQL*Net message' ela= 736 tim=4217919226432
7 PARSING IN CURSOR #140499 dep=0 tim=4217919226577 sqlid='bzdm0nbr7c036'
8 WAIT #1404990: nam='SQL*Net message' ela= 1 tim=4217919226665
9 WAIT #1404990: nam='SQL*Net message' ela= 2027 tim=4217919228710
10 WAIT #1404991: nam='SQL*Net message' ela= 1 tim=4217919228849
11 PARSING IN CURSOR #140499 dep=0 tim=4217919226577 sqlid='bzdm0nbr7c036'
12 WAIT #1404990: nam='SQL*Net message' ela= 1 tim=4217919226665
13 WAIT #1404990: nam='SQL*Net message' ela= 2027 tim=4217919228710

Algorithm is:
o does first line contain (will always start with "PARS...") "dep=1" ?,
o if yes, I keep reading without outputting anything till next "PARS...",
o if contains "dep=0", I display it, with those that follow, till next "PARS...",
o then same check again, ... etc.

How easily can it be done ? Using simple shell command (cat etc.) maybe ? But my file is fairly big...
Thanks a lot.
Seb


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just do something like:

awk '/^PARSING IN CURSOR/{ t = match($0, "dep=1")} !t' input

Each time the line of input matches PARSING IN CURSOR, check to see if the line matches the string dep=1 and set t accordingly. The !t causes the line to be output if dep=1 was not in the header. If dep=1 was in the header, then !t is false and no output is generated.

eg:

$ cat input
PARSING IN CURSOR #140499 dep=0 tim=4217919222030  sqlid='ftf4q8xj38z7k'
WAIT #1404991: nam='SQL*Net message' ela= 1 tim=4217919224736
WAIT #1404991: nam='SQL*Net message' ela= 655 tim=4217919225409
PARSING IN CURSOR #140499 dep=0 tim=4217919225606  sqlid='9fufagwmu041b'
WAIT #1404991: nam='SQL*Net message' ela= 1 tim=4217919225677
WAIT #1404991: nam='SQL*Net message' ela= 736 tim=4217919226432
PARSING IN CURSOR #140499 dep=0 tim=4217919226577  sqlid='bzdm0nbr7c036'
WAIT #1404990: nam='SQL*Net message' ela= 1 tim=4217919226665
WAIT #1404990: nam='SQL*Net message' ela= 2027 tim=4217919228710
WAIT #1404991: nam='SQL*Net message' ela= 1 tim=4217919228849
PARSING IN CURSOR #140499 dep=1 tim=4217919225606  sqlid='9fufagwmu041b'
WAIT #1404991: nam='SQL*Net message' ela= 603 tim=4217919229470
WAIT #1404991: nam='SQL*Net message' ela= 1 tim=4217919229647
WAIT #1404991: nam='SQL*Net message' ela= 521 tim=4217919230185
WAIT #1404991: nam='SQL*Net message' ela= 0 tim=4217919230330
WAIT #1404991: nam='SQL*Net message' ela= 758 tim=4217919231107
WAIT #1404990: nam='SQL*Net message' ela= 0 tim=4217919231222
WAIT #1404990: nam='SQL*Net message' ela= 1635 tim=4217919232874
PARSING IN CURSOR #140499 dep=0 tim=4217919226577  sqlid='bzdm0nbr7c036'
WAIT #1404990: nam='SQL*Net message' ela= 1 tim=4217919226665
WAIT #1404990: nam='SQL*Net message' ela= 2027 tim=4217919228710
PARSING IN CURSOR #140499 dep=1 tim=4217919225606  sqlid='9fufagwmu041b'
WAIT #1404991: nam='SQL*Net message' ela= 521 tim=4217919230185
WAIT #1404991: nam='SQL*Net message' ela= 0 tim=4217919230330
WAIT #1404991: nam='SQL*Net message' ela= 758 tim=4217919231107
$ awk '/^PARSING IN CURSOR/{ t = match($0, "dep=1")} !t' input
PARSING IN CURSOR #140499 dep=0 tim=4217919222030  sqlid='ftf4q8xj38z7k'
WAIT #1404991: nam='SQL*Net message' ela= 1 tim=4217919224736
WAIT #1404991: nam='SQL*Net message' ela= 655 tim=4217919225409
PARSING IN CURSOR #140499 dep=0 tim=4217919225606  sqlid='9fufagwmu041b'
WAIT #1404991: nam='SQL*Net message' ela= 1 tim=4217919225677
WAIT #1404991: nam='SQL*Net message' ela= 736 tim=4217919226432
PARSING IN CURSOR #140499 dep=0 tim=4217919226577  sqlid='bzdm0nbr7c036'
WAIT #1404990: nam='SQL*Net message' ela= 1 tim=4217919226665
WAIT #1404990: nam='SQL*Net message' ela= 2027 tim=4217919228710
WAIT #1404991: nam='SQL*Net message' ela= 1 tim=4217919228849
PARSING IN CURSOR #140499 dep=0 tim=4217919226577  sqlid='bzdm0nbr7c036'
WAIT #1404990: nam='SQL*Net message' ela= 1 tim=4217919226665
WAIT #1404990: nam='SQL*Net message' ela= 2027 tim=4217919228710

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...