To compare the content of two text files

I have two files,

sec.txt(1st File)
3172

disp.txt(2nd file)

the file name is
***********
45676

Now i want to compare the value in sec.txt file with disp.txt file
Excatly i want to compare the value 3172 in first file and 45676 in second file.

i want to compare the first line of first file and fourth line of second file with IF condition.
Please help me with the code.:confused:

Hi,

try this:

#!/bin/bash

if [ "$( cat $1)" -lt "$( sed -n '4 p' $2)" ]
then
        echo "less than"
fi

with parameter

 sec.txt disp.txt

Hi rammm,

One way using perl:

$ head sec.txt disp.txt 
==> sec.txt <==
3172

==> disp.txt <==

the file name is
***********
45676
$ cat script.pl
use warnings;
use strict;

my @nums;

while ( <> ) {
        chomp;
        if ( m/\A\d+\Z/ ) {
                push @nums, $_;
                close $ARGV;
        }
}

if ( @nums >= 2 ) {
        printf qq[%s\n], ($nums[0] == $nums[1]) ? q[Equal] : q[Not equal];
}
else {
        printf qq[Error\n];
}
$ perl script.pl sec.txt disp.txt 
Not equal

Hi pokerino,
Its not working. Can you explain me in detail. I used the code as follows,

#!/bin/bash  if [ "$( cat $sec.txt)" -lt "$( sed -n '4 p' $disp.txt)" ] then         echo "less than" fi

---------- Post updated at 07:41 AM ---------- Previous update was at 07:40 AM ----------

Hi birei,
thanks for your help. I am not using perl. I am using bash. Can you help me out in that.

You write the code in a file called

newfile.sh

Make this file executable with

chmod 750 newfile.sh

run with

./newfile.sh sec.txt disp.txt

the parameter -lt is "less than" you can also use -gt "greater than" -eq "equal"

Hi pokerino,
Thanks. the code is working but i didt run the script separately i.e ./newfile.sh sec.txt disp.out . If i run in that way it is working. I am using this code as part of the script. That is this code comes in one part of big script file. Then in that case how can i run the file with parameter in run time. I want to use those parameter files inside the code or inside the main script file. help me out.

You can write the filenames replacing

$1

and

$2

.
Or insert a variable like

$MYFILE_1

and

$MYFILE_2

, in replace

$1

and

$2

, and put the value in the head fo your big script file.

Thank you it works fine. :slight_smile: One more thing,
I have a text file as 1.txt with following content,

q34566788u
w2343556666t

I have used script file sample.sh with content,

#!/bin/ksh
echo "Enter the Value : "
read value
echo "$value"
sed '1s:\(.\{4\}\)\(.\{3\}\):\1$value:' 1.txt

I used the script file to replace the value 6678 in 1.txt file with the input value on run time which i try to read with variable name value. But its not replacing the value stored on variable instead its replacing $value i.e o/p

q345$value8u

.
I want the output as

q345(input value)8u

with input value on runtime in dash places.
Help me out. :slight_smile:

---------- Post updated at 10:37 AM ---------- Previous update was at 10:34 AM ----------

I got the result,
I $value in sed command inside '$value ' it works.

in sed command use a double quote instead a single quote.

#!/bin/ksh 
mv /int/int1/abc.txt /int/int1/1.txt

echo "Enter the Value : " read value echo "$value" sed '1s:\(.\{4\}\)\(.\{3\}\):\1'$value':' 1.txt

In the above code i try to replace the value in 1.txt file
But its not working. When i use someother file already created in that path, its working.
but when i created the file at run time as in above code its not working.

when i used the file 1.txt its replacing the string and not reading the input value.

Any solutions ????? :wall:

if you want replace value in the file you need to add

-i

option to sed. For a correct replacement of the keyword

$value

you need to change the single quote to double quote:

sed "1s:\(.\{4\}\)\(.\{3\}\):\1$value:" -i 1.txt
sed "1s:\(.\{4\}\)\(.\{3\}\):\1$value:" -i 1.txt

The exact command is not working.

but without -i its working as

sed "1s:\(.\{4\}\)\(.\{3\}\):\1$value:" 1.txt

Thanks

If i want to the output of above command in the same text file
i.e 1.txt means wat is the command?

I tried as

sed "1s:\(.\{4\}\)\(.\{3\}\):\1$value:" 1.txt > 1.txt 

but its not working

i want the result in the same file which is reading file. any help?

---------- Post updated 04-25-12 at 01:47 AM ---------- Previous update was 04-24-12 at 02:07 AM ----------

I am using the following code for replacing the value in the file.

sed "1s:\(.\{4\}\)\(.\{3\}\):\1$value:" 1.txt

I want the output of above command in the same file i am reading i.e 1.txt.
I tried out as following, its not working

sed "1s:\(.\{4\}\)\(.\{3\}\):\1$value:" 1.txt > 1.txt

Also tried for

sed "1s:\(.\{4\}\)\(.\{3\}\):\1$value:" 1.txt >temp.txt; mv temp.txt 1.txt

But No value is replaced.
Any ideas?