The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
check the given string is numeric or not. knowledge_gain High Level Programming 11 02-03-2009 11:25 AM
How to check Null values in a file column by column if columns are Not NULLs Mandab Shell Programming and Scripting 7 03-15-2008 09:57 AM
to check variable if its non numeric sachin.gangadha Shell Programming and Scripting 3 12-06-2007 05:33 PM
Check for numeric inputs Raynon Shell Programming and Scripting 6 08-22-2007 03:17 AM
How to check for a valid numeric input Vijayakumarpc Shell Programming and Scripting 1 08-04-2007 08:34 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-04-2006
jambesh's Avatar
jambesh jambesh is offline
Registered User
  
 

Join Date: Aug 2006
Location: Pune,India
Posts: 136
How to check a column contain numeric or char data type ??

I have a file called clientname_filename.csv
whose contents are like
col1|col2|col3|col4|
510|abc|xxx|450|
510|abc11|yyy|350
510|pqr99|zzz| 670
512|222|439|110


Here i have check the contents of column for data type.
i have a constraints that col1 always contain Numeric value column 2 can contain char type or alphanumeric ,column 3 should always char type,column 4 always numeric value


here i have a constarinfile called clientname_filename_constraint.csv

contents of this file is nothing but the datatype each column contain
col1|col2|col3|col4
Nu|char|char|Nu

Based on this file i want to check the main file and verify the data type of the column if any column violate the constraints it should show error.

here col 1 is right as it contain only numeric,col2 is invalid as it contain a numeric at the end, column 3 is also invalid for same reason. Column is right as it contain numeric only .

How will i check whether a column contain only numeric value or char value ??
please assist !!
  #2 (permalink)  
Old 10-04-2006
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,715
try a shell script like this - you need to order the isnumeric and isalpha tests
per your requirements, I have them as col1 + col4 numeric col2 + col4 alpha:
Code:
#!/bin/ksh

isnumeric()
{
	result=$(echo "$1" | tr -d '[[:digit:]]')
	echo ${#result}
}

isalpha()
{
	result=$(echo "$1" | tr -d '[[:alpha:]]')
	echo ${#result}
}
col1=""
col2=""
col3=""
col4=""
let retval=1

while read record
do
	echo "$record" | awk -F"|" '{print $1, $2, $3, $4 }' | read col1 col2 col3 col4
	
	if [[ $(isnumeric "$col1") -eq 1 && $(isnumeric "$col4") -eq 1 ]]; then
		 retval=1
	else
		 retval=0
		 break
	fi

	if [[ $(isalpha "$col2") -eq 1 && $(isalpha "$col3") -eq 1 ]]; then
		 retval=1
	else
		 retval=0
		 break
	fi
done  < filename

if [[ $retval -eq 1 ]]; then
	echo "test is okay"
else
	echo "test failed for this row:"
	echo "$col1 $col2 $col3 $col4"
fi
  #3 (permalink)  
Old 10-04-2006
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
For char
Code:
awk -F"|" ' $2~ "^[a-zA-Z][a-z[A-Z]*$" { print $2 }' file
if it contains special char then

Code:
awk -F"|" ' $2~ "^[^0-9][^0-9]*$" { print $2 }' file
For number

Code:
awk -F"|" ' $1 ~ "^[0-9][0-9]*$" { print $1 }' file
  #4 (permalink)  
Old 10-04-2006
jambesh's Avatar
jambesh jambesh is offline
Registered User
  
 

Join Date: Aug 2006
Location: Pune,India
Posts: 136
Anbu & Jim thanks a lot for the clue
Can i make this $1 or $2 as variable as for some file $1 can be numeric but for other file the first column constrint could be character
so depend upon the constrint file i want to check the main file.

Thanks for the clue
  #5 (permalink)  
Old 10-04-2006
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,507
Python alternative:
Code:
>>> for lines in open("clientname_filename.csv"):
... 	lines = lines.strip().split("|")
... 	if not lines[0].isdigit() or not lines[3].isdigit():
...  		print "First or  fourth col not numeric: %s" %(lines)
... 	elif not lines[1].isalnum():
... 		print "Second col not alphanumeric: %s" %(lines)
... 	elif not lines[2].isalpha():
... 		print "Third col not char type: %s" %(lines)
  #6 (permalink)  
Old 10-04-2006
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
try this

Code:
awk -F"|" ' 
BEGIN {
getline var < "constraintfile"
getline var < "constraintfile" 
n = split( var , arr , "|" )
for ( i = 1 ; i <= n ; ++i )
{
        if( arr[i] == "Nu" )
                regexp[i] = "^[0-9][0-9]*$"
        if( arr[i] == "char" )
                regexp[i] = "^[a-zA-Z][a-z[A-Z]*$"
print regexp[i]
}
}
$1 ~ regexp[1] && $2 ~ regexp[2] && $3 ~ regexp[3] && $4 ~ regexp[4] {
print $0
}' file
  #7 (permalink)  
Old 10-05-2006
jambesh's Avatar
jambesh jambesh is offline
Registered User
  
 

Join Date: Aug 2006
Location: Pune,India
Posts: 136
Anbu,

Thanks a lot for being with me and helping me.
can you please tell me why we ned twice

getline var < "constraintfile"
getline var < "constraintfile"

??
Also for sample purpose i have given 4 column now but in actual we can have max 150 column.
how to deal if the number column is more then 30 .
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:50 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0