Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Click here to return to the 'Actually that grep is redundant, all you need is sed..' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Actually that grep is redundant, all you need is sed..
Authored by: cynikal on Dec 18, '02 04:05:24PM

the grep parameter should be '\\.548' instead of just '.548' because the dot is a wildcard for 'match any character' (similar to ? in the shell).

If you don't escape the meaning of (with the backslash) then you will match anything with 548 including 1548, etc.. Granted these other ports are probably not very common this will probably work. But i just wanted to make this command sniplet more effective.

If you want to, you can combine the awk/grep line (because awk can do everything grep and sed do) like this:

netstat -f inet -n | awk '$5 ~ /\\.548$/ {sub(/\\.[^.]+$/, ""); print $5}'

basically, i had awk check that the 5th field (white space seperated) ends in a dot (not any character) followed by 548 followed by the end of the field (the dollar sign is an 'anchor' for the end of the string, the field being the entire string). If this holds true for the current line, i used the sub() awk function (takes place of the seperate sed). i then also only print that 5th field.

[ Reply to This | # ]
Authored by: cynikal on Dec 18, '02 04:08:26PM

there should NOT be double slashes, only single slashes..

When i previewed my post, it ignored the first slash so i put two (sometimes you need to escape the slash itself :P)

[ Reply to This | # ]
Actually that grep is redundant, all you need is sed..
Authored by: loren_ryter on Dec 20, '02 09:30:45AM

I don't know how quoting works in tch, so i wonder if anyone can give the sytax of you wanted to make that an alias.


[ Reply to This | # ]
The correct string is...
Authored by: geppo1982 on Apr 12, '06 11:20:48PM
netstat -f inet -n | awk '$5 ~ /\.548$/ {sub(/\.[^.]+$/, ""); print $5}'
Thanks cynikal for your hint.

[ Reply to This | # ]