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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Use AWK to check for numeric values? (BASH script)
# 1  
Old 06-15-2011
Data 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.
# 2  
Old 06-15-2011
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:

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

# 3  
Old 06-15-2011
Replace your awk statement like this.

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

# 4  
Old 06-15-2011
If you still want to use AWK:
Code:
hostname=`echo $file | awk 'BEGIN{FS=OFS="."}$NF~"[0-9]+"{$NF=""; NF--;}{print}'`

# 5  
Old 07-17-2011
MySQL Thanks

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. Smilie

TauntaunHerder
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Trying to use awk to check values and multiple

I am trying to use AWK to read a file, comma delimited, and check each field to see if it has a suffix of - (dash , minus sign) if so then I want to either move the minus sign the the beginning of the field or take the numeric portion of the field and multiply it by negative 1 to get the field... (9 Replies)
Discussion started by: ziggy6
9 Replies

2. Shell Programming and Scripting

Merge values from multiple directories into one file in awk or bash

I am trying to merge or combine all $1 values in validation.txt from multiple directories into one new file and output it here tab-delimited:/home/cmccabe/Desktop/20x/total/total.txt. Each $2 value and the header would then be a new field in total.txt. I am not sure how to go about this as cat is... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

How to find the X highest values in a list depending on the values of another list with bash/awk?

Hi everyone, This is an exemple of inpout.txt file (a "," delimited text file which can be open as csv file): ID, Code, Value, Store SP|01, AABBCDE, 15, 3 SP|01, AABBCDE, 14, 2 SP|01, AABBCDF, 13, 2 SP|01, AABBCDE, 16, 3 SP|02, AABBCED, 15, 2 SP|01, AABBCDF, 12, 3 SP|01, AABBCDD,... (1 Reply)
Discussion started by: jeremy589
1 Replies

4. UNIX for Dummies Questions & Answers

Script to Count the Numeric Values and get the Total

Can anyone help me in this? Here is the Secenario. I need to count the Numerical vaues from the below output Item processed: 1401 Item processed: 2839 Item processed: 1261 Item processed: 2584 Item processed: 2 Item processed: 988 Item processed: 1 Item processed: 2119 ... (3 Replies)
Discussion started by: Padmanabhan
3 Replies

5. Shell Programming and Scripting

Extracting information and creating numeric values using awk

I have some variables containing for example m02-npt02-sr40-syn-dv0p01-16x12drw m02-npt02-sr40-syn-dv0p02-16x12drw m02-npt02-sr40-dv0p03-syn-16x12drw I want to extract the dv entry for example dv0p01 dv0p02 dv0p03 Then I want to convert to a numeric, the p specifies the... (5 Replies)
Discussion started by: kristinu
5 Replies

6. Shell Programming and Scripting

Need help in shell script to trim numeric values

Hello I am currently working on a shell script which actually needs to pull some file from a server , The filenames will have the extension with date/time stamp , i need to trim that and name it to proper format Example : xyz_20091108_22142365.gzip i need to remove the... (5 Replies)
Discussion started by: ranga27
5 Replies

7. Shell Programming and Scripting

How to check if the file contains only numeric values

How to check if the file contains only numeric values. I don't want to read entire file it eats lot of cpu Or any way which consumes less memory n cpu.. Please suggest -S (2 Replies)
Discussion started by: sunilmenhdiratt
2 Replies

8. Shell Programming and Scripting

help newb at linux and bash need numeric script sort

I am trying to setup to automatically import a series of mysql database files. I am doing manually now and its a royal pain. All the sql files are sequentially numbered in a format of 4 numbers underscore text with spaces replaced by underscores. example: There are 3 databases each setup... (1 Reply)
Discussion started by: dlm1065
1 Replies
Login or Register to Ask a Question