LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How can i read two files word by word at a time using any loop by shell script? (https://www.linuxquestions.org/questions/programming-9/how-can-i-read-two-files-word-by-word-at-a-time-using-any-loop-by-shell-script-793131/)

vaibhavs17 03-04-2010 06:48 AM

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

catkin 03-04-2010 06:55 AM

What have you thought of or tried so far?

David the H. 03-04-2010 07:13 AM

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?

EricTRA 03-04-2010 07:16 AM

Hello,

Have a look at the comm command. Works great if the files are sorted.
Code:

man comm
Kind regards,

Eric

pixellany 03-04-2010 07:20 AM

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.

vaibhavs17 03-05-2010 12:23 AM

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

primerib 03-05-2010 02:17 AM

Quote:

Originally Posted by vaibhavs17 (Post 3886544)
Hi,

Suppose i have two files delimited by TAB.


but i want word by word comparision which requires word by word navigation then only i can get the output mentioned above.

Why not read each line into an array and compare thru each element? Here's an example:

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

exec 3<a
exec 4<b
while IFS= read -r line1 <&3
IFS= read -r line2 <&4
do
        array1=( `echo $line1` )
        array2=( `echo $line2` )
        for ((X=0; X<="${#array1[@]}"; X++)); do
                if [ "${array1[$X]}" != "${array2[$X]}" ]; then echo "mismatch! file 1: ${array1[$X]}    file 2: ${array2[$X]}"; fi
        done
done


vaibhavs17 03-05-2010 03:40 AM

Hi primerib ,

Thanks for the response.
I used the same code, but got few errors:
Quote:

array1=( `echo $line1` )
Error:
ksh: syntax error: `(' unexpected


I replaced it with
Quote:

array1="echo $line1"
Now I am getting error in for loop
ksh: syntax error: `((' unexpected


How to remove this??

Thanks,
Vaibhav

primerib 03-05-2010 02:51 PM

Quote:

Originally Posted by vaibhavs17 (Post 3886685)
Hi primerib ,

Thanks for the response.
I used the same code, but got few errors:

Error:
ksh: syntax error: `(' unexpected


I replaced it with


Now I am getting error in for loop
ksh: syntax error: `((' unexpected


How to remove this??

Thanks,
Vaibhav

The problem is caused because you're using ksh instead of bash. The script is intended to use bash (see the #!/bin/bash at the top of the script). I'm not familiar with ksh however so I don't know how they handle arrays.

biplabbijay 03-05-2010 08:12 PM

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
this will you give you the line by line comparison.

Biplab

vaibhavs17 03-08-2010 01:14 AM

Quote:

Originally Posted by primerib (Post 3887402)
The problem is caused because you're using ksh instead of bash. The script is intended to use bash (see the #!/bin/bash at the top of the script). I'm not familiar with ksh however so I don't know how they handle arrays.

HI PrimeriB,

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!

primerib 03-08-2010 01:20 AM

Quote:

Originally Posted by vaibhavs17 (Post 3889853)
./vaibhav.sh: line 10: syntax error near unexpected token `(('
./vaibhav.sh: line 10: `for ((X=0; X<="${#array1[@]}";X++));

What about doing it like this then:
Code:

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++
done

or also replace "let X++" with "let X=$X+1" if it still complains.

Hope this works on that system...?

vaibhavs17 03-08-2010 01:35 AM

Quote:

Originally Posted by primerib (Post 3889855)
What about doing it like this then:
Code:

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++
done

or also replace "let X++" with "let X=$X+1" if it still complains.

Hope this works on that system...?


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

primerib 03-08-2010 12:01 PM

Quote:

Originally Posted by vaibhavs17 (Post 3889864)
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

Glad that change works for you. I don't know what shell SunOS uses but it must not support c-like loops which is what the original one was. However, there's always a way to fix it. :)

vaibhavs17 03-17-2010 01:59 PM

Quote:

Originally Posted by primerib (Post 3890442)
Glad that change works for you. I don't know what shell SunOS uses but it must not support c-like loops which is what the original one was. However, there's always a way to fix it. :)

Hi All,

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

regard001 03-18-2010 07:15 AM

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 :)

regard001 03-19-2010 03:48 AM

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.