Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I have two files,
FILE1:
AAAAA
FFFFFFF
WWWWW
SSSSSSSFILE2:
SSSSSSSSSS
XXXXXXXXXXXXXX
EE
CCCC
MMMMMM
WWWWW
oooooooooWHAT I WANT TO DO IS,
if (line 6 of file2 = line 3 of file1)
then
echo "yes"
else
echo "no"
fiAnother one is,
if (line 6 of file2 = anyline of file1)
then
echo "yes"
else
echo "no"
fiI 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

I think this handles your first example:
!/bin/kshif [[ $(head -6 file2|tail +6) = $(head -3 file1|tail +3) ]]
then
echo "Yes"
else
echo "No"
fiAlthough 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

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
Nowhat 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,

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 < file1if [[ $cnt > 0 ]]
then
echo "Yes"
else
echo "No"
fi

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.

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

![]() |
Update files with awk
|
Unix awk
|

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