For Programmers: Free Programming Magazines  


Home > Archive > AWK > May 2004 > dropping last 4 characters









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author dropping last 4 characters
Chris G.

2004-05-20, 1:30 pm

I have a file list of MAC addresses from a router and would like to do a
compare with a master file. The problem is the router adds a (04) to the end
of the address and the master list doesn't have it. I'm using awk '/4000./
{print $2}' MACFile. Can I do something with awk to drop the last 4
characters or should I pipe to sed and do something there.



A. W. Dunstan

2004-05-20, 3:30 pm

Chris G. wrote:

> I have a file list of MAC addresses from a router and would like to do a
> compare with a master file. The problem is the router adds a (04) to the
> end of the address and the master list doesn't have it. I'm using awk
> '/4000./
> {print $2}' MACFile. Can I do something with awk to drop the last 4
> characters or should I pipe to sed and do something there.


substr($0, 0, length($0)-4)

Not elegant, but it gets the job done. Might want to check for
strings of length <= 4, tho'.

--
Al Dunstan, Software Engineer
OptiMetrics, Inc.
3115 Professional Drive
Ann Arbor, MI 48104-5131

"To educate a man in mind and not in morals is to
educate a menace to society."
- Theodore Roosevelt

Charles Demas

2004-05-20, 3:30 pm

In article <Ki5rc.7$Ni1.5@fe37.usenetserver.com>, Chris G. <xxx@xxx.xxx> wrote:
>I have a file list of MAC addresses from a router and would like to do a
>compare with a master file. The problem is the router adds a (04) to the end
>of the address and the master list doesn't have it. I'm using awk '/4000./
>{print $2}' MACFile. Can I do something with awk to drop the last 4
>characters or should I pipe to sed and do something there.



one (untested) way is:

awk '/4000./ {sub(/....$/, "", $2); print $2}' MACFile


Chuck Demas

--
Eat Healthy | _ _ | Nothing would be done at all,
Stay Fit | @ @ | If a man waited to do it so well,
Die Anyway | v | That no one could find fault with it.
demas@theworld.com | \___/ | http://world.std.com/~cpd
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com