How can i read two files word by word at a time using any loop by shell script?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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 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.
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
./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.
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
Last edited by vaibhavs17; 03-08-2010 at 01:37 AM.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.