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
346 views
in Technique[技术] by (71.8m points)

awk - Substitute non-matching-pattern strings in every line of a file

From following file:

 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='redo log sync' ela= 1 tim=4217919225677
 6  WAIT #1404991: nam='redo log sync' 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='PGA memory operation' ela= 603 tim=4217919229470
13  WAIT #1404991: nam='PGA memory operation' ela= 1 tim=4217919229647
14  WAIT #1404991: nam='PGA memory operation' ela= 521 tim=4217919230185 

I want to display everything, but for strings inside a line non-matching SQL*Net message between nam=' and ' ela=, I want these strings to be replaced with Other DB operation (and keep nam=' and ' ela= before and after these strings):

 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='Other DB operation' ela= 1 tim=4217919225677
 6  WAIT #1404991: nam='Other DB operation' 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='Other DB operation' ela= 603 tim=4217919229470
13  WAIT #1404991: nam='Other DB operation' ela= 1 tim=4217919229647
14  WAIT #1404991: nam='Other DB operation' ela= 521 tim=4217919230185 

Is there a sed or awk command that'll allow me to do that easily?


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

1 Reply

0 votes
by (71.8m points)

Please try this awk solution, tested with GNU Awk:

awk '/redo/{sub(/redo log sync/, "Other DB operation")}1' Input_file

Explanation

awk '                                      # Start program
/redo/{                                    # On lines matching the regex `redo`
sub(/redo log sync/, "Other DB operation") # replace `redo log sync` with `Other DB Operation`
}1                                         # print
' Input_file                               # Input file goes here

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

...