Doing Checks on a file


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Doing Checks on a file
# 1  
Old 08-12-2008
Doing Checks on a file

I have a process that I am trying to provide a solution for and have hit a brick wall and would like some pointers in the right direction.
Basically on a daily basis a report is automatically generated in a CSV format (FIRST.CSV) which includes codes and amounts in the following format:

A016ZZ ,, 1400.43
A011ZZ ,, 15000.98
B014ZZ ,, -450.83
B027ZZ ,, 86.90
.....

The amounts can be negative or positive. The codes on the other hand always have to begin with A or B and have to always end with ZZ.
Anyway after this file has been scrutinised it usually needs some amendments, however to amend the file a second CSV file (SECOND.CSV) file is created manually in the same format as above. That is codes and the corresponding values.

These 2 files then need to be merged which is fine, but before it can be merged the second file (SECOND.CSV) needs to have some checks run on to ensure each code begins with an A or B followed by three numrical figures followed by the 'ZZ'post fix.

Could anyone assist as to how this check can be done. I have got as far as this:

if [ `cat ${SECOND.CSV} | egrep -v '^L|^M' | wc -l` -gt 0 ]
then
print "code other than A or B present."
exit 1
fi
# 2  
Old 08-12-2008
Haven't tested it but I think it should be as easy as
Code:
if grep -qE '^[AB][0-9][0-9][0-9]' ${SECOND.CSV}; then
    # do something
fi

I am even convinced that the -E option in the grep is redundant.
For such trivial expression we don't need to fire the extended regex engine.
Btw, a variable name containing a dot isn't good naming I would say.
# 3  
Old 08-12-2008
Oops, sorry got it wrong.
You require that each record contains this pattern.
Simply squeeze in the -v with the grep.
# 4  
Old 08-12-2008
If you want to get rid of them, you can use
Code:
sed -n '/^[A|B]\{1\}[0-9]\{3\}ZZ /p' infile

So there will be no need left to check if, and then do some action Smilie

A little hint: If you use grep, don't cat a file into it. Just use grep <pattern> <filename>, as grep can take the file to be processed as parameter. Era will be proud of me! ;D (sorry, that was a kind of insider joke) Smilie
# 5  
Old 08-12-2008
OK the above works a treat thanks. This leads me to mu next and final issue but will need to explain a little first.

Once the first file (FIRST.CSV) has been generated with codes and ammount column adjustments have to be made to certain accounts. This is done my the use of the second file (SECOND.CSV). If the codes in the first file and second file agree then the amount in the second file is added/substracted to the first file. If the code in the second file does not agree to any code in the first file then the code and amount are appended to bottom:

e.g
FIRST.CSV
A001ZZ ,, 400
A002ZZ ,, 300

SECOND.CSV
A001ZZ ,, -200
A002ZZ ,, 100
A003ZZ ,, 10

THIRD.CSV
A001ZZ ,, 200
A002ZZ ,, 400
A003ZZ ,, 10


The above isd done by using the code:
join -t, -a1 -a2 ${SECOND.CSV} ${FIRST.CSV} > ${THIRD.CSV}
nawk -F, '{ printf $1",," "%011.2f\n",$3+$5 }' ${THIRD.CSV} > ${FINAL.CSV}


The problem with the above is that if for example A001ZZ needs two adjustmentss at the same time meaning the SECOND.CSV file will look like:
SECOND.CSV
A001ZZ ,, -200
A001ZZ ,, 300
A002ZZ ,, 100
A003ZZ ,, 10

my code will only pick up the first instance of A001ZZ and adjust as required and the second occurence of A001ZZ will then be treated as a new code and be appended to the bottom of FINAL.CSV.

Could anyone please assist as to how I can resolve this issue?
# 6  
Old 08-12-2008
zaxxon: It's not like I'm a higher authority on that particular issue, just a more vocal one, perhaps.
# 7  
Old 08-12-2008
anyone with an idea????
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract timestamp from first record in xml file and it checks if not it will replace first record

I have test.xml <emp><id>101</id><name>AAA</name><date>06/06/14 1811</date></emp> <Join><id>101</id><city>london</city><date>06/06/14 2011</date></join> <Join><id>101</id><city>new york</city><date>06/06/14 1811</date></join> <Join><id>101</id><city>sydney</city><date>06/06/14... (2 Replies)
Discussion started by: vsraju
2 Replies

2. Shell Programming and Scripting

Script to do the following checks

Hi , I need a script for processing below scenario. I have to check daily by doing ftp IP to check it is logging or not. So i want this activity to be automated such that if login succesful i will get "FTP LOGIN SUCCESS" in a log file and if fails i want the error message in the same log... (1 Reply)
Discussion started by: sv0081493
1 Replies

3. Shell Programming and Scripting

Daily Checks

Hey Guys, I'm seeking some assistance in getting this script to run as a cron job for the user oracle.. the script is basically to perform 2 ADRCI checks... see the script below... i'm getting the following error: /export/home/oracle/Daily_Checks/ADRCI_Daily_Checks.sh: syntax error at line 16:... (7 Replies)
Discussion started by: Racegod
7 Replies

4. UNIX for Dummies Questions & Answers

Daily File Checks

Hello all, I'm sorry if this is answered elsewhere, I've used the search function and can't find the specifics of what I'm after. I am brand new to playing with linux, and ideally I want to get better to help the company that I now work for. What I want to do: Create a script that I... (4 Replies)
Discussion started by: Aussiemick
4 Replies

5. AIX

Pre-checks

AIX Guys!!! What pre-checks would you do on a 5.3 server before TL/SP/APAR installation? Bala (2 Replies)
Discussion started by: balaji_prk
2 Replies

6. UNIX for Advanced & Expert Users

speed test +20,000 file existance checks too slow

Need to make a very fast file existence checker. Passing in 20-50K num of files In the code below ${file} is a file with a listing of +20,000 files. test_speed is the script. I am commenting out the results of <time test_speed try>. The normal "test -f" is much much too slow when a system... (2 Replies)
Discussion started by: nullwhat
2 Replies

7. Shell Programming and Scripting

to perform checks line by line on a file

Hi, I have a file abc.txt with data like this 1 /test/ 2 /test/file.txt 3 /data/ 4 /data/file1.txt 5 /data/file2.txt I want to take out every path from the file and check if its a directory or file. I am trying it with cut with something like this but it doesnt work ... (7 Replies)
Discussion started by: muaz
7 Replies

8. UNIX for Dummies Questions & Answers

health checks

Hello Anyone please help me with a health check script on HP-UX. (1 Reply)
Discussion started by: mahlathini
1 Replies

9. Shell Programming and Scripting

perform some checks on file using perl

hi i want check for PVCS header in file if its present then check if its in proper format or not i want to do this is in perl on windows. this is what i am doing : 1 . open file 2 . check for "PVCS information" if found then store the line no to $line var. 3 . check for "sccs" header ... (0 Replies)
Discussion started by: zedex
0 Replies
Login or Register to Ask a Question