I've got a longish log file with content such as
Uplink traffic:
Downlink traffic:
I want to parse the log file and remove any line that contains the string "Uplink traffic:" at the beginning of the line, but only if the line following it beginnings with the string "Downlink traffic:" (in other words a grep -v won't work)
put sample of the file and the desired o/p
In cases where successive lines are of the form
Uplink traffic:
Downlink traffic:
I want the "Uplink traffic:" line removed
In cases where successive lines are of the form
Uplink traffic:
Dropped packets: 40
Downlink traffic:
I don't want the "Uplink traffic:" line removed
panyam
November 5, 2009, 8:28am
4
Something like this :
awk '/Uplink traffic:/ {p=NR;st=$0;q=1;next} /Downlink traffic:/ {print (NR==p+1)?$0:$0;q=0;next} { if(q==1){q=0;print st} print}' file_name.txt
Cheers Panyam.. that worked. No idea how though.. Must have a read of an Awk tutorial and figure it out.
code
nawk '
/Uplink/{getline a;getline b; if (a !~ /^Down/){printf"%s\n%s\n%s\n\n",$0,a,b} else {printf"%s \n\n",a} }
' up
panyam
November 6, 2009, 12:46am
7
ahmad.diab:
code
nawk '
/Uplink/{getline a;getline b; if (a !~ /^Down/){printf"%s\n%s\n%s\n\n",$0,a,b} else {printf"%s \n\n",a} }
' up
Check the script , I guess it wont give the desired result as "yorkie99" expected.
Or something like this:
$
$ cat -n tst.log
1 blah
2 blah
3 Uplink traffic:
4 Downlink traffic:
5 blah
6 Uplink traffic:
7 Downlink traffic:
8 Uplink traffic:
9 Dropped packets: 40
10 Downlink traffic:
11 blah
12 blah
$
$ perl -ne 'BEGIN{undef $/} $x=$_; END{$x=~s/Uplink.*?\nDownlink/Downlink/g; print $x}' tst.log
blah
blah
Downlink traffic:
blah
Downlink traffic:
Uplink traffic:
Dropped packets: 40
Downlink traffic:
blah
blah
$
$
tyler_durden