LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-04-2010, 06:48 AM   #1
vaibhavs17
Member
 
Registered: Mar 2009
Posts: 51

Rep: Reputation: 1
Question 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
 
Old 03-04-2010, 06:55 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
What have you thought of or tried so far?
 
Old 03-04-2010, 07:13 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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?
 
Old 03-04-2010, 07:16 AM   #4
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Hello,

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

Eric
 
Old 03-04-2010, 07:20 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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.
 
Old 03-05-2010, 12:23 AM   #6
vaibhavs17
Member
 
Registered: Mar 2009
Posts: 51

Original Poster
Rep: Reputation: 1
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
 
Old 03-05-2010, 02:17 AM   #7
primerib
Member
 
Registered: Mar 2010
Posts: 48

Rep: Reputation: 20
Quote:
Originally Posted by vaibhavs17 View Post
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
 
Old 03-05-2010, 03:40 AM   #8
vaibhavs17
Member
 
Registered: Mar 2009
Posts: 51

Original Poster
Rep: Reputation: 1
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
 
Old 03-05-2010, 02:51 PM   #9
primerib
Member
 
Registered: Mar 2010
Posts: 48

Rep: Reputation: 20
Quote:
Originally Posted by vaibhavs17 View Post
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.
 
Old 03-05-2010, 08:12 PM   #10
biplabbijay
Member
 
Registered: Jul 2009
Posts: 89

Rep: Reputation: 15
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
 
Old 03-08-2010, 01:14 AM   #11
vaibhavs17
Member
 
Registered: Mar 2009
Posts: 51

Original Poster
Rep: Reputation: 1
Exclamation

Quote:
Originally Posted by primerib View Post
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!
 
Old 03-08-2010, 01:20 AM   #12
primerib
Member
 
Registered: Mar 2010
Posts: 48

Rep: Reputation: 20
Quote:
Originally Posted by vaibhavs17 View Post
./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...?
 
1 members found this post helpful.
Old 03-08-2010, 01:35 AM   #13
vaibhavs17
Member
 
Registered: Mar 2009
Posts: 51

Original Poster
Rep: Reputation: 1
Thumbs up

Quote:
Originally Posted by primerib View Post
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

Last edited by vaibhavs17; 03-08-2010 at 01:37 AM.
 
Old 03-08-2010, 12:01 PM   #14
primerib
Member
 
Registered: Mar 2010
Posts: 48

Rep: Reputation: 20
Quote:
Originally Posted by vaibhavs17 View Post
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.
 
Old 03-17-2010, 01:59 PM   #15
vaibhavs17
Member
 
Registered: Mar 2009
Posts: 51

Original Poster
Rep: Reputation: 1
Question

Quote:
Originally Posted by primerib View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
URGENT----shell script to change word in file raghupal Programming 14 10-21-2008 12:49 AM
shell script to find an word or words from a line rakesh.tandur Linux - General 5 05-13-2008 01:57 PM
I need a word processor that can read .SDW files newbiesforever Linux - General 3 02-17-2008 04:27 AM
shell script: find subwords with 'compilation' word suresheva31 Programming 11 10-05-2005 04:17 AM
How to read ans parse MS word file using a Linux Shell script. Alek Linux - General 2 11-10-2003 02:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration