![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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
|
|
||||
|
For char
Code:
awk -F"|" ' $2~ "^[a-zA-Z][a-z[A-Z]*$" { print $2 }' file
Code:
awk -F"|" ' $2~ "^[^0-9][^0-9]*$" { print $2 }' file
Code:
awk -F"|" ' $1 ~ "^[0-9][0-9]*$" { print $1 }' file
|
|
|||||
|
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 |
|
||||
|
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)
|
|
||||
|
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
|
|
|||||
|
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 | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|