|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here! |
|
#1
|
|||
|
|||
|
string compare from two files
hi am a newbie to shell scripting, i have been assigned a task to Read two files, contents of each being some string values each seperated by a comma, then compare the values and create an error report of all entries that did not match
Eg a) Contents of File 1 value one,value 2,,value four b) Contents of File 2 value 1, value 2, value 3, value 4 am required to do a compare of each values, and generate an error report which describes the error as - "The actual value is "value one" but generated value is "value 1"" please help |
|
#2
|
|||
|
|||
|
read unix man pages of
diff, cmp, comm |
|
#3
|
|||
|
|||
|
[QUOTE=akash_gmg]hi am a newbie to shell scripting, i have been assigned a task to Read two files, contents of each being some string values each seperated by a comma, then compare the values and create an error report of all entries that did not match
Eg a) Contents of File 1 value one,value 2,,value four b) Contents of File 2 value 1, value 2, value 3, value 4 am required to do a compare of each values, and generate an error report which describes the error as - "The actual value is "value one" but generated value is "value 1"" If I were you, I would not read any man pages except man awk. Your task is not solved in Unix, you must do it yourself. A first approach can be: : [ $# -eq 2 ] || { echo "$0: call: $0 actual generated" >&2 exit 1 } awk ' # Do all in the begin clause BEGIN { REC=0 while (1) { read1() read2() # split both lines into arrays elements N1 = split(LINE1, FIELD1, "," ) N2 = split(LINE2, FIELD2, "," ) REC++ # next record read from both files m = max(N1, N2) # Compare all the fields and report differences for (i=1; i<=m; i++) { if (FIELD1[i] != FIELD2[i]) { printf "record %d, actual \"%s\" generated \"%s\"\n", REC, FIELD1[i], FIELD2[i] } } # End of job? if(LINE1=="" && LINE2=="") exit(0) } } # Read a line from the first file function read1() { if ((getline<"'$1'") > 0) { LINE1 = $0 } else LINE1 = "" } # Read a line from the second file function read2() { if ((getline<"'$2'") > 0) { LINE2 = $0 } else LINE2 = "" } # Return the bigger argument function max(a, b) { if (a>b) return(a) return(b) } ' This script is tested, copy and paste it. All sequences of quotes and apostrophes must be exactly copied! If you copy it as a file scr.sh, do the following: chmod +x scr.sh scr.sh file1 file2 Regards ![]() |
|
#4
|
|||
|
|||
|
firstly i must thank you for all your time and effort, much appreciated
, i did what you asked of me, i copied and pasted the script that you'd written including the commas, apostrophies etc, when i executed the script i got the following error, awk: syntax error near line 6 awk: illegal statement near line 6 awk: syntax error near line 7 awk: illegal statement near line 7 awk: syntax error near line 12 awk: illegal statement near line 12 awk: syntax error near line 17 awk: illegal statement near line 17 awk: syntax error near line 28 awk: bailing out near line 28 incidently these lines happen to have function calls or function definitions, for eg line 6 has read1(), line 7 read2(), 12 has m = max(N1, N2) so on...what am i doing wrong ? please help |
|
#5
|
|||
|
|||
|
Quote:
It is quite simple, dear Watson. Next time specify the Unix system you are using. I have tested my script on Linux and everthing was OK. When I have seen your error messages, I tried a computer with Sun. And I have got the same reults as you. On Sun there is an older version of awk. The better version "new awk" is named nawk. The remedy is: put nawk instead of awk. Don't forget to report your results, pls. You should indent your scripts properly. When I was copying my answer, my indenting got lost, but it makes no harm to the function of the script, but it is badly readable by men. Have a fun! ![]() |
|
#6
|
|||
|
|||
|
purrrfectooooooooooo
thank you very muchooooo ....ooooo just can't thank you enough kind sir, take a bowww to zlutovsky, the script works like a dream, thanks again, your a super star, u just don't know how critical this script is to me... |
|
#7
|
|||
|
|||
|
Quote:
Hi, I am glad to have helped you. I was a beginner too, many years ago...Can you tell me, where my script will work? It was written in Prague, Czech Rep. Take care. ![]() |
|
#8
|
|||
|
|||
|
Quote:
hi, sorry couldnt reply earlier, prague wow , your script would be put to use in bangalore, india, small world huh ?? thanks again |
|
#9
|
|||
|
|||
|
Record
Hallo akash_gmg,
it is my personal record. I have never been to India, neither my programs... And what about you and Europe? Now you have the first step - the tested out alogorithm. In your spare time you can rewrite it in C language. It is a fine class room task. You can then compare the execution times. I have once found the ratio 6:1. If you realy rewrite the script in C you will probably use the functions like fopen(), fgets(), feof(), strtok(), strcmp(), malloc() and others. Try it, it should be ready in three hours' work. Another question is the economy. The price of your time compared to the saved computer time. But nevertheless, you can have a nice C programming practise. Regards zlutovsky |
|
#10
|
|||
|
|||
|
Quote:
hello zlutovsky, i have been to europe, was working in london for RBS bank, was there for a year, never to prague tho, i've heard its beautiful, you should take time off and visit india sometime, u'll be impressed, bangalore is very cosmopolition, i will most definately try rewriting this script in C, i will keep you posted as to how i get along, Děkuji |
|
#11
|
|||
|
|||
|
Your Czech is perfect! Do you know more czech words?
Rádo se stalo. zlutovsky |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > string compare from two files |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|