Use AWK to check for numeric values? (BASH script)

Hi,

I'm quite new to scripting, but know a few AWK statements.

I have the following line in my script:

hostname=`echo $file | awk 'BEGIN{FS=OFS="."}{$NF=""; NF--; print}'`

I use this in my script to rename files, which are similar to this:

name.mvkf.mkvfm.mkfvm.1

To the following (minus the random number at the end):

name.mvkf.mkvfm.mkfvm

My question is... Is there a way to be more thorough about this, and only take the last field off, if it is a integer, anything else to be left.

I'm guessing there's a function which I can use to check if the last field is a number?

Any help greatly appreciated.

Many thanks in advance.

Using awk to handle a single line is like lighting a furnace to burn one hair. Things like awk, sed, grep and so forth function most efficiently when used on large volumes of data. If you're just evaluating one statement it's always better to use shell built-ins.

Depending on your shell, you may be able to do this:

FNAME="${FNAME%%.[0-9]*}"

Replace your awk statement like this.

 
awk 'BEGIN{FS=OFS="."}{if($NF~/[0-9]+/){$NF=""; NF--;} print}'

If you still want to use AWK:

hostname=`echo $file | awk 'BEGIN{FS=OFS="."}$NF~"[0-9]+"{$NF=""; NF--;}{print}'`

Thanks for the response guys... I've finally got around to trying out these, and they are great use, now too choose one to go with. :stuck_out_tongue:

TauntaunHerder