Read from a file and compare


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read from a file and compare
# 1  
Old 05-25-2009
Error Read from a file and compare

i want a shell script to read the a list of numbers from a file and compare with a unique number with the list.

FOR EXAMPLE :

abc.lst file contains d following
2343
3214
45654
563456
5436

Unique number is 2342
# 2  
Old 05-25-2009
How do you get the unique number? What should be the output? Is this homework? What have you tried so far?
# 3  
Old 05-25-2009
#! /usr/bin/ksh

line_cnt=`cat abc.lst | wc -l`

i=1
while [ $i -le $line_cnt ]
do
num=`cat abc.lst | head -$i | tail -1`
if [ $num -eq 2342 ]
then
echo "Bingo!"
fi
done
# 4  
Old 05-25-2009
Error Read from a file and compare

this is not a homework.......Smilie
unique number is user input(can be changed by command line argument)

#!/bin/sh
echo "number to be compared"
read t
var=`cat abc.lst`
for i in $var; do
if $t == $i then
echo "same"
else
echo "not same"
fi
echo $i
done
# 5  
Old 05-25-2009
Use grep. read man pages for grep

Code:
read inp; 
echo $inp;
if [ `grep -s -c "$inp" abc.lst` -gt 0 ]; then 
echo "matching"; 
else 
echo "notmatching"; 
fi


-Devaraj Takhellambam
# 6  
Old 05-25-2009
if you have Python
Code:
#!/usr/bin/env python
userinput = raw_input("Enter user input: ")
for line in open("file"):
    line=line.strip()
    if line==userinput:
        print "same"

# 7  
Old 05-25-2009
And if you have perl, then:

Code:
$ 
$ cat abc.lst
2343         
3214         
45654        
563456       
5436         
$            
$ cat readcmp.pl
#!/usr/bin/perl -w
print "Enter number to be compared : ";
chomp($num = <STDIN>);
open(LST,"abc.lst") or die "Can't open abc.lst: $!";
while (<LST>) {
  chomp;
  if ($_ < $num)    { printf("%8d is less than    %d\n",$_,$num) }
  elsif ($_ > $num) { printf("%8d is greater than %d\n",$_,$num) }
  else              { printf("%8d is equal to     %d\n",$_,$num) }
}
close(LST) or die "Can't close abc.lst: $!";

$
$ perl readcmp.pl
Enter number to be compared : 1234
    2343 is greater than 1234
    3214 is greater than 1234
   45654 is greater than 1234
  563456 is greater than 1234
    5436 is greater than 1234
$
$ perl readcmp.pl
Enter number to be compared : 50000
    2343 is less than    50000
    3214 is less than    50000
   45654 is less than    50000
  563456 is greater than 50000
    5436 is less than    50000
$
$ perl readcmp.pl
Enter number to be compared : 45654
    2343 is less than    45654
    3214 is less than    45654
   45654 is equal to     45654
  563456 is greater than 45654
    5436 is less than    45654
$

HTH,
tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Read two file and compare the line

Hello Guys I need to read and compare two file, one file contains hostname, and others contain hostname and IP@, and the objective is to read each file and compare if line in file1 equal to first word in the second file2 file1 file2 this is my first code fqdn_hosts=list.txt... (2 Replies)
Discussion started by: Abdellah
2 Replies

2. UNIX for Dummies Questions & Answers

How to read from the window and compare in Ncurses?

How would I go about having ncurses look at a coordinate, read what character is there, and see if it is the same as an other character? I've been trying to use inch(), but I don't think I'm doing it right. Here's the portion of the code I'm struggling with. //Code to scroll left chtype... (0 Replies)
Discussion started by: mitsopy
0 Replies

3. Shell Programming and Scripting

How to read file line by line and compare subset of 1st line with 2nd?

Hi all, I have a log file say Test.log that gets updated continuously and it has data in pipe separated format. A sample log file would look like: <date1>|<data1>|<url1>|<result1> <date2>|<data2>|<url2>|<result2> <date3>|<data3>|<url3>|<result3> <date4>|<data4>|<url4>|<result4> What I... (3 Replies)
Discussion started by: pat_pramod
3 Replies

4. Shell Programming and Scripting

Re: Read lines and compare in a loop

I have a file which has following content: NAME=ora.DG1.dg TYPE=ora.diskgroup.type TARGET=ONLINE STATE=ONLINE NAME=ora.DG2.dg TYPE=ora.diskgroup.type TARGET=ONLINE STATE=ONLINE NAME=ora.DG3.dg TYPE=ora.diskgroup.type TARGET=ONLINE STATE=ONLINE NAME=ora.DG4.dg... (7 Replies)
Discussion started by: rcc50886
7 Replies

5. UNIX for Dummies Questions & Answers

When reading a csv file, counter to read 20 lines and wait for minute then read next 20 till end

Hello All, i am a newbie and need some help when reading a csv file in a bourne shell script. I want to read 10 lines, then wait for a minute and then do a reading of another 10 lines and so on in the same way. I want to do this till the end of file. Any inputs are appreciated ... (3 Replies)
Discussion started by: victor.s
3 Replies

6. Shell Programming and Scripting

How to Read & Compare Two Files

Hi forumers, How is it going. Ok i need some advice on the following problem. I have 2 files to read and compare data.FileA and FileB. FileA will return either status 1 or 0. FileB on the other hand is trickier and has the following details:- Count DeviceID CurrentStatus ... (7 Replies)
Discussion started by: prakash1111
7 Replies

7. Shell Programming and Scripting

need to read lines in file and compare value in if not working

Hello, I have this file that sometime contains 0 lines and sometimes 1 or more. It's supposed to then put the result (could be 0 or 1 or 2 or more) into a variable. Then it's supposed to echo using an if else statement depending on the value of the variable. flagvar='wc -l $tempfile |... (1 Reply)
Discussion started by: script_op2a
1 Replies

8. Shell Programming and Scripting

Read Two lines in a CSV File and Compare

Hi , I have a CSV file ( file.csv) with some data as below: A,1,abc,x,y,z,,xyz,20100101,99991231 A,1,abc,x,y,z,234,xyz,20100101,99991231 I have to delete the duplicate line based on unique identifiers which are values in the fields- 2,3,4,8.These coulmns in both the rows have same... (6 Replies)
Discussion started by: Sheel
6 Replies

9. Shell Programming and Scripting

How to read and compare multiple fields in a column at the same time

Hi, Currently I am coding up a nasty way of reading file input using *cat* rather than *read*. My text input looks like TextA 100 TextB 110 TextC 120 Currently I am using cat |while read line to read the first column and second column fields. cat foo.txt|while read line do ... (1 Reply)
Discussion started by: ahjiefreak
1 Replies

10. Shell Programming and Scripting

read a file and compare

hi how can i read a file using the unix script and check for one or more field value for a predefined status/value for example : the file contains the following text Name IP Address/Mask Type Connection Status mgmt-eth0(1) ... (2 Replies)
Discussion started by: aemunathan
2 Replies
Login or Register to Ask a Question