How can i read two files word by word at a time using any loop by shell script?
hi Guys,
This forum helpmend a lot! thank you so much. Well, I am facing one issue: How can i read two files word by word at a time using any loop as i need word by word comparision in shell script? Please let me know pseudo code. Your help will be highly appreciated! Thanks in Advance, vaibhav |
What have you thought of or tried so far?
|
That's a little vague, isn't it?
What do you mean exactly by "two files, word by word"? Do you mean one file after another, or combining the input from both files in some way, or what? How exactly are the files formatted; one word per line, or something else? What do you intend to do with the input? How about some examples? And of course, as catkin asked already, what have you tried? |
Hello,
Have a look at the comm command. Works great if the files are sorted. Code:
man comm Eric |
vaibhav;
In your earlier threads, you are not posting any follow-up when people try to help you. If you want good help here, you need to respond. I'm not sure what you mean by "word by word comparison". Please post some sample files, plus the output you are looking for. |
Hi,
Suppose i have two files delimited by TAB. File 1 ====== 1 B AB 2 2 C AB 3 File 2 ======= 3 B AB 5 2 C AD 3 Output Should be:: Mismatch in file 1 and 2 first Line 1,3 and 2,5 second Line AB,AD. Actually the problem is "How can i navigate in both the files at a same time" using any loop. Using while loop i tried to navigate through both the files at a time. [code] exec 3<File1.txt exec 4<File2.txt while IFS= read -r line1 <&3 IFS= read -r line2 <&4 do echo "$line1" echo "$line2" done [code] but i want word by word comparision which requires word by word navigation then only i can get the output mentioned above. Thanks, Vaibhav |
Quote:
First make a couple test files: echo -e "one\t2\tthree\t4" >a echo -e "1\t2\t3\t4" >b Then the script: Code:
#!/bin/bash |
Hi primerib ,
Thanks for the response. I used the same code, but got few errors: Quote:
ksh: syntax error: `(' unexpected I replaced it with Quote:
ksh: syntax error: `((' unexpected How to remove this?? Thanks, Vaibhav |
Quote:
|
Vaibhav,
if u want to compare two files word by word then in a shell just type the command diff with their full location before their name as : Code:
diff /home/vaibhav/file1 /home/usr/file2 Biplab |
Quote:
well, Thanks for your quick response. The sniipet of codde you have sent is running on almost all unix flavours but When I tried the same in office work station, It has SunOS nygespappd29 5.8 Generic_117350-46 sun4u sparc SUNW,Sun-Fire-V240 I tried writting the same for bash shell nad I chosen filename as temp.sh . Even after it would not work. And it is exactly complaining at for loop statement. Inarrowed down the problem by commenting out one statement by one. ./vaibhav.sh: line 10: syntax error near unexpected token `((' ./vaibhav.sh: line 10: `for ((X=0; X<="${#array1[@]}";X++)); Please HELP HELP HELP! |
Quote:
Code:
X=0 Hope this works on that system...? |
Quote:
Hi PrimeriB, You are an Amazing! Hats Off! One thing I wanted to know is this unix flavour is not updated one since it is complaining for previously mentioned syntax? since previous syntax was working on all unix falvor except sun-os. Anyways, Your post was really very helpful for me. Thanks vaibhav |
Quote:
|
Quote:
Thanks for your help. I would need your help again,script is working fine except with below scenario. Below scenario depict how array fails. I have a two files delimited by TAB. I need to compare these two files word by word.But if some of the fields are not populated then array comparison is not giving proper outputs FILE1.txt =========== A B C E FILE2.txt ========== A B D E OUTPUT SHOULD be =================== Mismatch File1.txt C File2.txt NULL(not display anything) Mismatch File1.txt NULL(not display anything) File2.txt D using below snippet of shell , getting output as below (which is not expected. SHELL ====================== exec 3<file1.txt exec 4<file2.txt while IFS= read -r line1 <&3 IFS= read -r line2 <&4 do array1=( `echo $line1` ) array2=( `echo $line2` ) X=0 for Y in "${array1[@]}"; do if [ "${array1[$X]}" != "${array2[$X]}" ]; then echo "mismatch! file 1: ${array1[$X]} file 2: ${array2[$X]}"; fi let X=$X+1 done done OUTPUT ======== Mismatch File1.txt C File2.txt D Please let me know how can I correct above mentioned snippet of shell. Your help would be highly appreciated. Thanks, Vaibhav |
Hi guys!
Sry, I can't help you out, but I have a similar problem, except that I have to compare lines not words, without the cmp or diff command. I've tried that script and it is helpful for me too, but I dont know how to difference lines. My original problem is that I have to make a script, what compares two files by lines without command cmp or diff,and if a line isn't match, the script write it out,and if there are more lines in the first file than in the second, or reverse, it has to write them out too. I hope that I don't disturbed this thread... just don't wanted to create another. With regards regard001 :) |
Thx guys, I've already solved it :P, just wanted to do it complicated :D, but I finally realized that I can do it on easy way. But I learned a lot from this search.
With regards regard001 :) |
All times are GMT -5. The time now is 05:33 AM. |