Operating System - Linux
1748163 Members
3784 Online
108758 Solutions
New Discussion юеВ

Re: how to remove the specific feilds from a string

 
SOLVED
Go to solution
Jared Middleton
Frequent Advisor

Re: how to remove the specific feilds from a string

James, on my system (RHEL 4.8), neither example (perl or awk) is printing the first 3 fields.
Maaz
Valued Contributor

Re: how to remove the specific feilds from a string

Hi Thanks James R. Ferguson for help.

One problem

# perl -wnae '$skip="4 6 9 10 11 12 16";for ($n=0;$n<@F;$n++) {printf "%s ",$F[$n] unless $skip=~$n};END{print "\n"}' log1
192.168.6.3 203.81.203.5: login expired username operator1 at (2009-06-08 10:30:23) 192.168.6.3 199.1.76.59: login expired username mrkt_emea at 192.168.6.3 199.1.76.55: login expired username mrkt_emea at 172.20.1.29/255.255.255.255.

both the perl and awk examples are not formating the newline("\n") properly.
I mean the perl and awk examples print the lines but without newlines, i.e from start till end no new lines.

Please help

Regards
Maaz
Stephen P. Schaefer
Frequent Advisor

Re: how to remove the specific feilds from a string

With light testing, this awk script does the job; the initialization of the array holding the fields to skip is more awkward than the perl; a better awk programmer might know something more idiomatic, but I'm pretty sure this works.

awk 'BEGIN{split("5,7,10,11,12,13,17",a,/,/);for (j in a) skip[a[j]]=1};{for (n=1;n<=NF;n++) {if (!skip[n]) {printf "%s ",$n}}};END{print "\n"}' < log1
Stephen P. Schaefer
Frequent Advisor

Re: how to remove the specific feilds from a string

Sorry; need to fix the newline issue:

awk 'BEGIN{split("5,7,10,11,12,13,17",a,/,/);for (j in a) skip[a[j]]=1};{for (n=1;n<=NF;n++) {if (!skip[n]) {printf "%s ",$n}}i;print};END{print "\n"}' < log1
Stephen P. Schaefer
Frequent Advisor

Re: how to remove the specific feilds from a string

Sigh. Another newline fix:

awk 'BEGIN{split("5,7,10,11,12,13,17",a,/,/);for (j in a) skip[a[j]]=1} {for (n=1;n<=NF;n++) {if (!skip[n]) {printf "%s ",$n}};printf "\n"}'
Jared Middleton
Frequent Advisor

Re: how to remove the specific feilds from a string

How about:
awk '{$5=$7=$10=$11=$12=$13=$17="";gsub(/[ ]+/," ")}1' < log1
Maaz
Valued Contributor

Re: how to remove the specific feilds from a string

Dear Stephen P. Schaefer thanks for such a nice help

Dear Jared Middleton its CCC(cool, crunchy, and crispy) ;), thanks a lot.

>awk '{$5=$7=$10=$11=$12=$13=$17=""
I understand the above part of the code

if you can please explain this ' gsub(/[ ]+/," ") ' part of the code/syntax, I mean whats its doing.. highly appreciated

Regards

James R. Ferguson
Acclaimed Contributor

Re: how to remove the specific feilds from a string

Hi (again) Maaz:

My apologies for not writing and testing my own code better. I failed to rigorously match the exclusion list and hence my output eliminated the first three fields.

In the modified Perl script, the '\b' represents word "boundry" character. This serves to delineate each value in the '$skip' list and thus allows exact matching whereas before values of 0, 1, or 2 were matched to segments of 10, 11, and 12.

Thus, use:

# perl -nae '$skip="4 6 9 10 11 12 16";for ($n=0;$n<@F;$n++) {printf "%s ",$F[$n] unless $skip=~/\b$n\b/};END{print "
\n"}' file

...

@ Stephen: very good!

@ Jared: very nicely done!

Regards!

...JRF...

James R. Ferguson
Acclaimed Contributor

Re: how to remove the specific feilds from a string

Hi:

Sorry, that last Perl should have been (without the 'END' block):

# perl -nae '$skip="4 6 9 10 11 12 16";for ($n=0;$n<@F;$n++) {printf "%s ",$F[$n] unless $skip=~/\b$n\b/};print "\n"' file

That aside, Jared's 'awk' solution could be emulated in Perl (thought not nearly as concisely) with:

# perl -ane '@F[4,6,9..12,16]=undef;for (0..$#F) {printf "%s ",$F[$_] if defined $F[$_]};print "\n"' file

Lastly, Jared's 'gsub' globally substitutes muliple spaces to one single space (left from nulling the fields you wanted to skip). In my second Perl script above, I skip undefined array elements.

Regards!

...JRF...
ghostdog74
Occasional Advisor

Re: how to remove the specific feilds from a string

don't have to use cat/grep/sed/perl....you can just use awk

awk '/Allowed.IP.Address|login expired/{ $5=$7=$10=$13=$17=""}1' file