Computing.Net > Forums > Unix > regarding about string compare

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

regarding about string compare

Reply to Message Icon

Name: tigerman
Date: October 27, 2008 at 10:17:57 Pacific
OS: cygwin
CPU/Ram: pentium
Product: intel
Comment:

I have two files,

FILE1:
AAAAA
FFFFFFF
WWWWW
SSSSSSS

FILE2:
SSSSSSSSSS
XXXXXXXXXXXXXX
EE
CCCC
MMMMMM
WWWWW
ooooooooo

WHAT I WANT TO DO IS,
if (line 6 of file2 = line 3 of file1)
then
echo "yes"
else
echo "no"
fi

Another one is,
if (line 6 of file2 = anyline of file1)
then
echo "yes"
else
echo "no"
fi

I have tried with
if $(head -n 6 file2.txt| tail -n 1)= $(head -n 3 file2.txt| tail -n 1)

but not success, if anyone can advice that will be great.

Many thanks



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: October 27, 2008 at 11:47:51 Pacific
Reply:

I think this handles your first example:


!/bin/ksh

if [[ $(head -6 file2|tail +6) = $(head -3 file1|tail +3) ]]
then
echo "Yes"
else
echo "No"
fi

Although I might be wrong, I think your second example requires you to cycle thru file1:


#displays 2-No, 1-Yes, 1-No
myline=$(head -6 file2|tail +6)
while read fileline
do
if [[ "$myline" = "$fileline" ]]
then
echo "Yes"
else
echo "No"
fi
done < file1


0

Response Number 2
Name: tigerman
Date: October 27, 2008 at 14:43:19 Pacific
Reply:

Many thanks for your reply, nails
The first script definitely sort out the problem.
But the second script , whatever i have the same line in the file1 , the output is "no" and coming like that.

TIGERMAN@your-8vzdnak0rt /home/naing/bash
$ ./test.sh
No
No
No
No
No
No

what i am trying to do is
if line6 of file2 , i mean "wwwww" here , is same as anyline of file1, output is "yes" one word only and otherwise "no" one word only.
Thanks for your time.

Many thanks,



0

Response Number 3
Name: nails
Date: October 27, 2008 at 20:56:51 Pacific
Reply:

Ok, then use a flag that is only checked once the while loop completes:


cnt=0
myline=$(head -6 file2|tail +6)
while read fileline
do
if [[ "$myline" = "$fileline" ]]
then
((cnt+=1))
fi
done < file1

if [[ $cnt > 0 ]]
then
echo "Yes"
else
echo "No"
fi


0

Response Number 4
Name: tigerman
Date: October 28, 2008 at 15:04:58 Pacific
Reply:

Thanks for your reply, i have tested the code,
but the output always say "no".

Thanks


0

Response Number 5
Name: nails
Date: October 28, 2008 at 20:33:54 Pacific
Reply:

I think I know what the problem is. With the earlier shells such as the Bourne shell, when the while loop executed, a new shell was spawned. Any variables updated within the shell spawned by the while loop, revert to whatever value was when the while loop terminates.

Here is an example Bourne shell script, test.ss:


#!/bin/sh
# test.ss

#
cnt=0
myline=`head -6 file2|tail +6`
while read fileline
do
if [ "$myline" = "$fileline" ]
then
cnt=`expr $cnt + 1`
echo $cnt
fi
done < file1
echo $cnt # should be 1


#
Execute the above script, and the variable cnt should equal 1. (It does with the newer bash and ksh shells)

Try executing the above script. If $cnt equals 0, then your problem is as I've described.

Is there a fix? You can execute the script by "sourcing" it:

. test.ss

(if you aren't familiar with sourcing, that's a period, a space, and the script name).

Sourcing a file forces all changes made in child shells to be reflected in the parent.

I've never used cygwin, so I can't tell you for sure. you'll just have to try it out.


0

Related Posts

See More



Response Number 6
Name: tigerman
Date: October 29, 2008 at 14:55:24 Pacific
Reply:

Many thanks for your reply, i have tested and the output is "0" as you said.
I start using cywin and linux just about a couple of week ago, so it is hard to understand about sourcing.
But i found many of your replies in this forum and i was trying to modify these scripts and now i can make it work not direct way but indirect way.


Many thanks for your time


0

Sponsored Link
Ads by Google
Reply to Message Icon

Update files with awk Unix awk



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: regarding about string compare

How to compare form feed? www.computing.net/answers/unix/how-to-compare-form-feed/6817.html

Awk for fields checking www.computing.net/answers/unix/awk-for-fields-checking/7678.html

Trim and Timestamp www.computing.net/answers/unix/trim-and-timestamp/7071.html