script to read and compare two consecutive lines in a file
Hi All
I have a file like below ADP_Comment- 4758 ADP_Comment-is missing cbdkbckd- 46983 cbdkbckd- 46983 ljhjg- 547 ljhjg- 980 ..... ..... ..... Now I ve to read the consecutive lines and then compare them to see if the count MATCHES DIFFERNT OR TABLE MISSING please help as it was a long and a real complicated script and this is the last step to my final OP and goshhh I am stuck!!!!Thankyou. |
Is Perl OK?
How about this: Code:
#!/usr/bin/perl *edit* btw, to run it, use the name of the file with your data as first command line argument, or pipe data into stdin. Output will look something like this: Code:
Table First Second Diagnosis |
Or you could use a simple awk script:
Code:
awk '{current = $NF;getline; if($NF == current}print "match";else print "mismatch"}' file |
thank you matthewg42.ur perl script works wonders.just a little problem.i need to hardcodeb the input file.so how do i do it and i would be in a much better position if u could xplain ur script as dis is my 1st script in perl.Thankyou so much.
hey grail...the awk script isnt wkng...anyways I am trying to fix the errors and once done i ll paste it here for beginners like me :-)Thankyuou so much!!! |
Perhaps I'm getting old, but I find your posts to be very difficult to understand. Please use proper English on these forums - you will get more, better and more accurate responses if your posts are intelligible.
I think you want to put the name of the input file in the program? Replace this line: Code:
while(<>) { Code:
open(IN, "<yourfilename") || die "cannot open yourfilename for reading: $!"; |
Hey Matthewg42
when I tried running this script with the above changes the following happens: Even when there is a match in the count the output says value mismatch.otherwise allz working right!!! thankyou very much |
Do you have extraneous whitespace after the numbers?
If this is the case, you could add this: Code:
open(IN, "<yourfilename") || die "cannot open yourfilename for reading: $!"; |
If that doesn't work, please post the output contents of this command:
Code:
head -4 yourdatafile |od -tc |
The Output of the above command is :
Quote:
Quote:
Thank you so much for helping me out.I really appriciate !!! |
I want the od -tc output for the data, not the script (I'm trying to see if there is trailing whitespace).
Solving a mystery like this is something of an addiction :D *edit*: aha, we I see from the data you posted that it's different from the original data, which explains the difference in what the program is doing. *edit2*: no, wait I'm confused. urr, well anyhow, if you post the od command output for the input file, then that would be helpful. |
wait....do u want m e to include this,i.e,head -4 yourdatafile |od -tc command in the script or just type it on the command prompt.
|
Quote:
Quote:
Shows unexpected results? If you let me know which and what the error, if any, I can probably fix it quickly? My guess is we probably need to see some more accurate data than was initially provided. |
Hi...when I type the above command on command prompt I get the following o/p:
Quote:
|
when i run the awk one liner the error looks like the below
Quote:
|
Hi guys ..since u all are reeally trying to help me sort out the errors...i think i should better explain you the requirements and what I have done.i think this would make all of us think on similar grounds.
so,there are 4 parts of this script. 1.i have to read a logfile which looks like: Quote:
Quote:
NOW THE SECOND PART: I TAKE THE LOG OUTPUT FILE AND CUT THE TABLENAME INTO A VARIABLE.I QUERY THE DATABASE FOR THE ROW COUNT OF EACH TABLE.THE SCRIPT FOR THIS LOOKS LIKE: Quote:
I AM READY WITH 2 OUTPUT FILES-LOG OUTPUT FILE AND DATABASE OUTPUT FILE.NOW I HAVE TO COMPARE THESE TWO OUTPUT FILES TO FIND IF: 1.THE COUNT MATCHES OR 2.THE COUNT DOES NOT MATCH OR 3.THE TABLE IN THE LOGFILE DOES NOT EXIST IN THE DATABASE THE SCRIPT FOR ABOVE COMPARISON IS: Quote:
Quote:
SO TO EXPEDITE THE SCRIPT I USED COMM COMMAND FOR A COMPARE. Quote:
Quote:
ACCOUNT_MISSING_FRM_RCIS_LINK- is ACCOUNT_MISSING_FRM_RCIS_LINK-4 #is means the table is missing in database ADP_COMMENT- 2384 ADP_COMMENT-2311 ADP_CONFIG- 11 ADP_CONFIG-11 ADP_FIELD- 36333 ADP_FIELD-36323 ADP_HEADER- 1 ADP_HEADER-1 here the problem is that some tables get repeated n number of times in the output file. now the last script is: Quote:
|
Well in answer to your question why the awk script failed, this is because of a simple typo:
Code:
awk '{current = $NF;getline; if($NF == current)print "match";else print "mismatch"}' file So based on that, the following would be the small change required: Code:
awk -F"-" '{current = $NF;getline; if($NF == current)print "match";else print "mismatch"}' file it within a function: Code:
DBcounttry_finalfunction() Is there a reason for the mash up? On a quick scan there does not appear to be anything particularly ksh specific that required this? Anyhoo, let me know how you get on. |
Suppose you had a large file including thousands of lines and you may have a few lines that are different with consecutive lines, you may want to simply print its line number with the code:
Code:
Code:
|
All times are GMT -5. The time now is 12:47 PM. |