$ cat ttt
ID .... VALUE
-------------
A001 .... 100
C003 .... 800
B002 .... 200
corrupt
data
A004 .... 300
C003 .... 800
foo .... bar
The script:
#!/bin/ksh
INPUT=ttt
{ while read LINE
do
echo $LINE |egrep "^A|^B" > /dev/null 2>&1
if [ $? -eq 0 ]
then
echo "Processing $LINE"
else
echo "Skipping $LINE"
fi
done } < $INPUT
The output:
$ ./ttt.ksh
Skipping ID .... VALUE
Skipping -------------
Processing A001 .... 100
Skipping C003 .... 800
Processing B002 .... 200
Skipping corrupt
Skipping data
Processing A004 .... 300
Skipping C003 .... 800
Skipping foo .... bar
You could do a single string of commands using awk for the pattern matching, but I'm not sure how you want to process the line once you verify it's good... so this may offer the most flexibility.
Let us know if you need anything in the script explained.
Ok the above only tell me how many times A or B where in there. I require it to send an error message if a C exist or otherwise its fine to continue processing file.. hope that makes more sense
That shell script above - you see where it displays "error" - if you make the script in a file with the execute bit set, you can run it. Use the portion that shows an error, i.e. "Skipping line" - and say, for example, "exit 1". Then at the end, assuming it didn't hit there, say "exit 0". Remove the printing where it's OK on A/B. Then your script exit value is zero/non-zero and you can use it as a test. Have it's input be from standard input, and it can "filter" for you. I realize this might be lots of theory and not enough specifics. If so, I could make it so and give you exact examples. Other folks here could too, I imagine. If you need it and no one has by the time I get back here, I'll do it.
Thanks for all the above help, but let me clarify the need of what is required to see if anyone can assist further.
Currently as stated i have teh following file called TEST1.CSV
This file needs to be processed but only if all the ID's are correct (that means only if the IDs begin with an A - B charcter.
If the ID begins with any character other A or B the process needs to exit out the script with error message.
What i would like is the following:
IF [begining of row is not A or B] THEN
print error message
exit 1
END IF
No else required as if the only A and B the IF statement will end succesfully leading the process scripting afterwards.
So any thoughts, not really as complex as previously thought???
SAMZ, until now you didn't tell us what you really want to achieve. The logic you used is ok, but - depending on what you want to do, the part inside "carry on doing what you want" - there may or may not be better solutions. We are able to help you only if we know exactly what you want to achieve.
Maybe your way (that is: to do it in shell script language) is good, because the shell is best suited to what you want to achieve. The other solutions (awk, sed, ...) may or may not be better suited than your solution because the respective tools may or may not be better suited to what you want to do - to finally assess this question we would need to know what you want to do.