Issue
I have a simple question
I have this file :
[NOC_S_Z_1]
callerid=NULL
secret=NOC_S_Z_1
context=CallCenterGWs
type=peer
host=dynamic
[NOC_S_A_2]
callerid=NULL
secret=NOC_S_A_2
context=CallCenterGWs
type=peer
host=dynamic
[5154]
callerid=<5154>
secret=5154
context=SupportGroup
type=friend
host=dynamic
[5155]
callerid=<5155>
secret=5155
context=SupportGroup
type=friend
host=dynamic
[PRIGW]
callerid=NULL
secret=PRI18865
context=CallCenterGWs
type=peer
host=192.168.2.95
I want to use sed or awk command to look for this word callerid=NULL
and remove it , but here is the trick , i want it to remove the line above it and the 4 lines below it !
it's a very large file but it's all the same type and the callerid=NULL
is randomly sorted in the file
I managed to remove only the callerid=NULL
but this is not helping
Any idea would be helpfull.
Solution
Assuming [
always denotes start of new block I would harness GNU AWK
for this task following way, let file.txt
content be
[NOC_S_Z_1]
callerid=NULL
secret=NOC_S_Z_1
context=CallCenterGWs
type=peer
host=dynamic
[NOC_S_A_2]
callerid=NULL
secret=NOC_S_A_2
context=CallCenterGWs
type=peer
host=dynamic
[5154]
callerid=<5154>
secret=5154
context=SupportGroup
type=friend
host=dynamic
[5155]
callerid=<5155>
secret=5155
context=SupportGroup
type=friend
host=dynamic
[PRIGW]
callerid=NULL
secret=PRI18865
context=CallCenterGWs
type=peer
host=192.168.2.95
then
awk 'BEGIN{RS="["}NR>1&&!/callerid=NULL/{printf "[%s",$0}' file.txt
gives output
[5154]
callerid=<5154>
secret=5154
context=SupportGroup
type=friend
host=dynamic
[5155]
callerid=<5155>
secret=5155
context=SupportGroup
type=friend
host=dynamic
Explanation: I inform GNU AWK that row separator is [
then I use printf
to output records excluding first one (NR>1
, empty string before first [
) and those with callerid=NULL
inside (negated regular expression). I do prepend [
to record as it is treated as row separator, i.e. not included in record.
(tested in gawk 4.2.1)
Answered By - Daweo Answer Checked By - Senaida (WPSolving Volunteer)