UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old January 27th, 2005, 01:43 AM
akash_gmg akash_gmg is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 5 akash_gmg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 38 m 52 sec
Reputation Power: 0
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

Reply With Quote
  #2  
Old January 27th, 2005, 06:04 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,083 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 19 h 49 m 57 sec
Reputation Power: 9
read unix man pages of
diff, cmp, comm

Reply With Quote
  #3  
Old January 27th, 2005, 09:35 AM
zlutovsky zlutovsky is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Prague, Czech Rep.
Posts: 116 zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 h 34 sec
Reputation Power: 6
[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

Reply With Quote
  #4  
Old January 28th, 2005, 04:48 AM
akash_gmg akash_gmg is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 5 akash_gmg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 38 m 52 sec
Reputation Power: 0
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

Reply With Quote
  #5  
Old January 28th, 2005, 05:32 AM
zlutovsky zlutovsky is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Prague, Czech Rep.
Posts: 116 zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 h 34 sec
Reputation Power: 6
Quote:
Originally Posted by akash_gmg
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



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!

Reply With Quote
  #6  
Old January 28th, 2005, 07:35 AM
akash_gmg akash_gmg is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 5 akash_gmg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 38 m 52 sec
Reputation Power: 0
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...

Reply With Quote
  #7  
Old January 29th, 2005, 08:14 AM
zlutovsky zlutovsky is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Prague, Czech Rep.
Posts: 116 zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 h 34 sec
Reputation Power: 6
Quote:
Originally Posted by akash_gmg
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...



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.

Reply With Quote
  #8  
Old January 30th, 2005, 10:23 PM
akash_gmg akash_gmg is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 5 akash_gmg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 38 m 52 sec
Reputation Power: 0
Thumbs up

Quote:
Originally Posted by zlutovsky
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.


hi, sorry couldnt reply earlier, prague wow , your script would be put to use in bangalore, india, small world huh ?? thanks again

Reply With Quote
  #9  
Old February 1st, 2005, 08:08 AM
zlutovsky zlutovsky is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Prague, Czech Rep.
Posts: 116 zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 h 34 sec
Reputation Power: 6
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

Reply With Quote
  #10  
Old February 6th, 2005, 11:17 PM
akash_gmg akash_gmg is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 5 akash_gmg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 38 m 52 sec
Reputation Power: 0
Quote:
Originally Posted by zlutovsky
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



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

Reply With Quote
  #11  
Old February 7th, 2005, 02:55 AM
zlutovsky zlutovsky is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Prague, Czech Rep.
Posts: 116 zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 h 34 sec
Reputation Power: 6
Your Czech is perfect! Do you know more czech words?

Rádo se stalo.

zlutovsky

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > string compare from two files


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |