Validating input based on fixed number of fields


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Validating input based on fixed number of fields
# 1  
Old 02-09-2009
Bug Validating input based on fixed number of fields

[post moved]

Yes, i did... let me state my problem in more detail
Inputs:
I have one input CSV file
And, i have stored no. of comma each line should in a variable.
e.g.

$ cat cmt.csv
this, is a ,comma ,count test1
,,this, is a ,comma ,count test2
this, is a ,comma ,count test3
Thisisaline,withoutspace,test1
Thisisaline,withoutspace,test2
this, line is ,lacking comma test1
this, line is ,lacking comma test2

and,
export row_comma=3

Now, requirement is:
if, in a line, no. of commas not equal to $row_comma then write this line into cmt.bad, and remove this line from cmt.csv.
To achieve this, i hv written something as below. which is working fine if there are NO spaces in input cmt.csv... but whenever there is a space its a mess.

#$FEED_DIR/$feed_nm=cmt.csv
for k in `cat $FEED_DIR/$feed_nm`
do
echo $k > $FEED_DIR/tmp.lst
line_comma_cnt=`awk ' BEGIN { count=0; }
{ for (i=1;i<=length($0);i++)
{
if(substr($0,i,1)=="," ) {count++;}
}
}
END {print count} ' $FEED_DIR/tmp.lst`
echo "New count: $line_comma_cnt"

#rm -f $EXCEPTION_DIR/${feed_nm}_insufficient_data.bad
rm -f $FEED_DIR/$feed_nm.tmp

#echo line comma count $line_comma_cnt and row comma count is $row_comma

if [[ $line_comma_cnt != $row_comma ]]
then
# echo "count mismatch, prepare separate files"
# cp $FEED_DIR/$feed_nm $FEED_DIR/$feed_nm.orig
#echo line is $k
# grep `cat $FEED_DIR/tmp.lst ` >> $FEED_DIR/lack.txt
grep $k $FEED_DIR/$feed_nm >> $EXCEPTION_DIR/${feed_nm}_insufficient_data.bad
# grep -v $k $FEED_DIR/$feed_nm > $FEED_DIR/$feed_nm.tmp
# mv $FEED_DIR/$feed_nm.tmp $FEED_DIR/$feed_nm
fi
done

#echo intial insuff count is `wc -l $EXCEPTION_DIR/${feed_nm}_insufficient_data.bad`

for d in `cat $EXCEPTION_DIR/${feed_nm}_insufficient_data.bad `
do
#echo "in test area `wc -l $EXCEPTION_DIR/${feed_nm}_insufficient_data.bad` "
grep -v $d $FEED_DIR/$feed_nm > $FEED_DIR/${feed_nm}.tmp
#echo after temp `wc -l $FEED_DIR/${feed_nm}.tmp `

mv $FEED_DIR/${feed_nm}.tmp $FEED_DIR/$feed_nm
# cp $FEED_DIR/${feed_nm}.tmp $FEED_DIR/$feed_nm
done

i will really appreciate, if someone help me out to get the desired result in optimal way.

Thanks
Dipali

Last edited by radoulov; 02-09-2009 at 08:09 AM.. Reason: new thread opened
# 2  
Old 02-09-2009
You can try something like this (backup your data first, use gawk, nawk or /usr/xpg4/bin/awk on Solaris):

Code:
awk 'BEGIN {
  FS = ","; te = "tmp"; be = "bad"
  ffn = fn = ARGV[2]; sub(/[^.]*$/,"", fn)
  }
{ 
  print > (fn (NF != nf + 1 ? be : te)) 
  }
END {
  if (system("mv " fn te OFS ffn)) {
    print "error moving", fn te, "to", ffn | "cat >&2"
    exit 1
    }
  }' nf="$row_comma" cmt.csv

# 3  
Old 02-09-2009
Thanks for your reply....
But this block is giving errorSmiliepossibly the simple one... but i m not good in awk programming), so pls suggest what wrong i m doing

awk 'BEGIN {
> FS = ","; te = "tmp"; be = "bad"
> ffn = fn = ARGV[2]; sub(/[^.]*$/,"", fn)
> }
> {
> print > (fn (NF != nf + 1 ? be : te))
> }
> END {
> if (system("mv " fn te OFS ffn)) {
> print "error moving", fn te, "to", ffn | "cat >&2"
> exit 1
> }
> }' nf="$row_comma" cmt.csv
mv: cannot stat `cmt.tmp': No such file or directory
error moving cmt.tmp to cmt.csv
# 4  
Old 02-09-2009
It seems there are no valid records in cmt.csv ...
Try this one:

Code:
awk 'BEGIN {
  FS = ","; te = "tmp"; be = "bad"
  ffn = fn = ARGV[2]; sub(/[^.]*$/,"", fn)
  }
{ 
  print > (fn (NF != nf + 1 ? be : te)) 
  }
END {
  if (system("[ -f " fn te " ] && mv " fn te OFS ffn)) {
    print "error moving", fn te, "to", ffn, \
    "or no valid records found" | "cat >&2"
    exit 1
    }
  }' nf="$row_comma" cmt.csv

# 5  
Old 02-09-2009
Another approach:

Code:
awk -F"," '
NF != n+1 {print > "cmt.bad"; next}
1' n=$row_comma cmt.csv > cmt.new 

mv cmt.new cmt.csv

Regards.
# 6  
Old 02-10-2009
Thanks a lot Radoulov & Franklin52 for such a compact solutions, And 3 liner approach is wonderful !!!
well, both approches working fine for me. Now i need little more enhancement in it that when i merged this piece of code into my shell script where i need to create bad file name dynamically e.g.

awk -F"," '
NF != n+1 {print > "$feed_nm.bad"; next}
1' n=$row_comma $FEED_DIR/$feed_nm > $FEED_DIR/$feed_nm.new

cp $FEED_DIR/$feed_nm.new $FEED_DIR/$feed_nm

So, can you please guide me here as well.

Thanks
Dipali
# 7  
Old 02-10-2009
Quote:
Originally Posted by Dipali
Thanks a lot Radoulov & Franklin52 for such a compact solutions, And 3 liner approach is wonderful !!!
well, both approches working fine for me. Now i need little more enhancement in it that when i merged this piece of code into my shell script where i need to create bad file name dynamically e.g.

awk -F"," '
NF != n+1 {print > "$feed_nm.bad"; next}
1' n=$row_comma $FEED_DIR/$feed_nm > $FEED_DIR/$feed_nm.new

cp $FEED_DIR/$feed_nm.new $FEED_DIR/$feed_nm

So, can you please guide me here as well.

Thanks
Dipali
Try something like this:

Code:
feed_nm.bad="cmt.bad"

awk -F"," '
NF != n+1 {print > bad; next}
1' n=$row_comma bad="$feed_nm.bad" $FEED_DIR/$feed_nm > $FEED_DIR/$feed_nm.new

Regards
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Splitting the file based on two fields - Fixed length file

Hi , I am having a scenario where I need to split the file based on two field values. The file is a fixed length file. ex: AA0998703000000000000190510095350019500010005101980301 K 0998703000000000000190510095351019500020005101480 ... (4 Replies)
Discussion started by: saj
4 Replies

2. Shell Programming and Scripting

awk command to search based on 5 user input fields

Field1=”” Field2=”” Field3=”” Field4=”” Field5=”” USER INPUT UP TO 5 FIELDS awk -F , '{ if ( $3 == Field1 && $6 == Field2 && $8 == Field3 && $9 == Field4 && $10 == Field5) print $0 }' /tmp/rodney.outD INPUT FILE (Rodney.outD): ... (3 Replies)
Discussion started by: rmerrird
3 Replies

3. Shell Programming and Scripting

Awk: Combine multiple lines based on number of fields

If a file has following kind of data, comma delimited 1,2,3,4 1 1 1,2,3,4 1,2 2 2,3,4 My required output must have only 4 columns with comma delimited 1,2,3,4 111,2,3,4 1,222,3,4 I have tried many awk command using ORS="" but couldnt progress (10 Replies)
Discussion started by: mdkm
10 Replies

4. Shell Programming and Scripting

Shell script for validating fields in a file

Hi, I have not used Unix in a very long time and I am very rusty. I would appreciate any help I can get from the more experienced and experts in Shell script. I am reading one file at a time from a folder. The file is a flat file with no delimeters or carriage return. Col1 through col6 is... (5 Replies)
Discussion started by: asemota
5 Replies

5. UNIX for Dummies Questions & Answers

Validating user input

I'm trying to set up a script that takes user input and validates that the user input was entered correctly. So far I have this: while : do echo "Please enter your name." read NAME if then echo "You have not entered a name." echo... (13 Replies)
Discussion started by: fufaso
13 Replies

6. Shell Programming and Scripting

Validating Input parameters

Hi All, I have tried to use ckdate (sun) command in script. It checks the input parameter which should be in 'YYYYMMDD format. date=$( echo $1 | ckdate -f "%Y%m%d") | true if ] then print " success" else print "no success" fi But in whatever format i pass the parameter,... (3 Replies)
Discussion started by: Amit.Sagpariya
3 Replies

7. Shell Programming and Scripting

Validating the input date format

I have one script.for that the inputs are fromdate(dd/mon/yyyy) and todate(dd/mon/yyyy). How i can validate the input format?for eg.27/08/2008 is not valid.27/aug/2008 or 27/Aug/2008 are valid. and the todate is optional.so if the todate is not present in the input then i need to assign the... (6 Replies)
Discussion started by: Sharmila_P
6 Replies

8. Shell Programming and Scripting

validating input using regular expressions

I am trying to validate input from the user in a script. I thought is was easy to do using regular expressions but I can't figure out how to use REs in a conditional. I have tried using grep from a temp file, sed from a temp file, sed from command line, comparison in an if condition and I cannot... (1 Reply)
Discussion started by: nrodolfich
1 Replies

9. UNIX for Dummies Questions & Answers

Validating fixed length field...

What's wrong with this part of my script? I just want to put each column from a fixed length file into a variable so I can validate each field later in the script. exec< myfile.dat while read afile; do a=`echo $(echo $afile |cut -c1-10)` echo "$a" b=`echo $(echo $afile |cut -c11-20)`... (12 Replies)
Discussion started by: giannicello
12 Replies

10. Programming

validating input

how do i validate y script so that it only accepts values between 1 and 3 and against any character input, cause at the moment i can only validate against numbers outside 1 and 3 but not characters cheers (4 Replies)
Discussion started by: ruffenator
4 Replies
Login or Register to Ask a Question