Unix Shell - truncating last character

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > April 2004 > truncating last character





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 truncating last character
joe@invalid.address

2004-04-27, 4:34 pm

"Chris Vidal" <cvidal@att.com> writes:

> snmpget <ip> system.sysDescr | grep -i version | awk '{print $7}'
>
> returns
>
> 12.2(19a),
>
> What is the best way to truncate the comma ?


If your shell supports the syntax:

$ x='12.2(19a),'
$ echo ${x%?}
12.2(19a)

Joe
--
If people don't want to come out to the ballpark, nobody's going
to stop them.
- Yogi Berra
Ed Morton

2004-04-27, 4:34 pm



Chris Vidal wrote:

> snmpget <ip> system.sysDescr | grep -i version | awk '{print $7}'


snmpget <ip> system.sysDescr | grep -i version |
gawk '{print gensub(",$","",$7)}'

Regards,

Ed.

Charles Demas

2004-04-27, 5:34 pm

In article <c6mdq8$s3t5@kcweb01.netnews.att.com>,
Chris Vidal <cvidal@att.com> wrote:
>snmpget <ip> system.sysDescr | grep -i version | awk '{print $7}'
>
>returns
>
>12.2(19a),
>
>What is the best way to truncate the comma ?
>
>seems like there would be something better then cut -d"," -f1


snmpget <ip> system.sysDescr | grep -i version |
awk '{sub(/.$/, "", $7); print $7}'

or use tr -d

snmpget <ip> system.sysDescr | grep -i version |
awk '{print $7}' | tr -d ","


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
Charles Demas

2004-04-27, 5:34 pm

In article <c6micp$e6p$1@pcls3.std.com>,
Charles Demas <demas@TheWorld.com> wrote:
>In article <c6mdq8$s3t5@kcweb01.netnews.att.com>,
>Chris Vidal <cvidal@att.com> wrote:
>
>snmpget <ip> system.sysDescr | grep -i version |
>awk '{sub(/.$/, "", $7); print $7}'


Should have been:

snmpget <ip> system.sysDescr | grep -i version |
awk '{sub(/,$/, "", $7); print $7}'

>or use tr -d
>
>snmpget <ip> system.sysDescr | grep -i version |
>awk '{print $7}' | tr -d ","

--
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
Kevin Collins

2004-04-27, 8:34 pm

In article <c6mih2$5dh$1@pcls3.std.com>, Charles Demas wrote:[vbcol=seagreen]
> In article <c6micp$e6p$1@pcls3.std.com>,
> Charles Demas <demas@TheWorld.com> wrote:
>
> Should have been:
>
> snmpget <ip> system.sysDescr | grep -i version |
> awk '{sub(/,$/, "", $7); print $7}'
>

Why is it that people continue to use 'grep | awk' when 'awk' alone can do the
job. There are multiple replies with inefficient and useless use of grep. Try
this, instead:

snmpget <ip> system.sysDescr | \
awk 'tolower($0) ~ /version/ {sub(/,$/, "", $7); print $7}'

GNU awk actually supports case-insensitive matching (since someone mentioned
it).

Or better, use tolower() only on the field which contains what you want to
match instead of the whole line.

And better than that (in this case) would be to use 'awk -F,' and eliminate the
sub(), if the line is actually comma-delimited...

Kevin
rakesh sharma

2004-04-28, 5:34 am

"Chris Vidal" <cvidal@att.com> wrote in message news:

>
> snmpget <ip> system.sysDescr | grep -i version | awk '{print $7}'
>
> returns
>
> 12.2(19a),
>
> What is the best way to truncate the comma ?
>
> seems like there would be something better then cut -d"," -f1
>


There are many ways to do this:

i)
... | sed 's/,$//'

ii)
var=`snmpget <ip> system.sysDescr | awk '/version/{print$7}'`
newvar=`expr "x$var" : 'x\(.*\),'`
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com