How to filter out data file...?

Hi...

I would like to filter out my data file....in two different way

1st way is like this, I will take one example..here...

The script should ask like this.

Enter min value in first column 

Enter max value in first column

Enter min value in second column

Enter max value in second column

Enter min value in third column

Enter max value in third column

After getting input from user end...data need to be filtered within the range specified..

suppose 1 column range is -5 to 5
                          2nd column range is 10 to 25
                          3rd column range is 15 to 35

output :

column 1   column 2  column 3    data   data   data

-5 10 15 12 225 25
-4 11 16 25 356 663
---------------------------------------------------------------------------------
----------------------------------------------------------------------------------
----------------------------------------------------------------------------------
5 25 35 255 2568 258[/CODE]data should be within specified range..

2nd way

Enter first column value 

Enter second column value

Enter third column value 

Data should match the value given by user

suppose say I am giving 1st column value 10, 2nd column values as 25, third as 30

It should display like this

column1 column2 column 3 data        data
10             25           30          12            35 
10             25           30          25            69 
10             25           30          26            39  
 

Hi, what have you tried so far and where are you stuck?

c1_min=0
c1_max=0
c2_min=0
c2_max=0
c3_min=0
c3_max=0

echo -n "Enter column 1 min value > "
read c1_min
echo -n "Enter column 1 max value > "
read c1_max
echo -n "Enter column 2 min value > "
read c2_min
echo -n "Enter column 2 max value > "
read c2_max
echo -n "Enter column 3 min value > "
read c3_min
echo -n "Enter column 3 max value > "
read c3_max

for file in *.txt; do

I am beginner wants to learn googling..and trying...

---------- Post updated at 08:57 AM ---------- Previous update was at 04:00 AM ----------

how can I apply this algorithm for my data file in shell
1st way

if column1 <=c1_min and >=c1_max and 
if column2 <=c2_min and >=c2_max and
if column3 <=c3_min and >=c3_max 
display column data

2nd way
if column1=c1_x and
if column2=c2_x and
if column3=c3_x 
display column 

I think its impossible..in shell

If you have ksh93 that has floating point arithmetic, so you can do something like this:

{
  read header
  printf "%s\n" "$header"
  while read col1 col2 col3 col4 col5; do
    if [ $c1_min -lt $col1 ] && [ $c1_max -gt $col1 ] && 
       [ $c2_min -lt $col2 ] && [ $c2_max -gt $col2 ] &&
       [ $c3_min -lt $col3 ] && [ $c3_max -gt $col3 ]
    then 
      printf "%s\t%s\t%s\t%s\t%s\n" $col1 $col2 $col3 $col4 $col5
    fi
  done 
} < data_filter.txt

If you are using ksh93 then there are better ways of writing this, but you get the idea...

In other shells you would need to convert the values to integer first or use an external program like for example bc

For example, using parameter expansion:

{
  read header
  printf "%s\n" "$header"
  while read col1 col2 col3 col4 col5; do
    if [ $c1_min -lt ${col1%%.*} ] && [ $c1_max -gt ${col1%%.*} ] && 
       [ $c2_min -lt ${col2%%.*} ] && [ $c2_max -gt ${col2%%.*} ] &&
       [ $c3_min -lt ${col3%%.*} ] && [ $c3_max -gt ${col3%%.*} ]
    then 
      printf "%s\t%s\t%s\t%s\t%s\n" $col1 $col2 $col3 $col4 $col5
    fi
  done 
} < data_filter.txt
echo -n "Enter column 1 min value > "
read c1_min
echo -n "Enter column 1 max value > "
read c1_max
echo -n "Enter column 2 min value > "
read c2_min
echo -n "Enter column 2 max value > "
read c2_max
echo -n "Enter column 3 min value > "
read c3_min
echo -n "Enter column 3 max value > "
read c3_max

{
  read header
  printf "%s\n" "$header"
  while read col1 col2 col3 col4 col5; do
    if [ $c1_min -lt $col1 ] && [ $c1_max -gt $col1 ] && 
       [ $c2_min -lt $col2 ] && [ $c2_max -gt $col2 ] &&
       [ $c3_min -lt $col3 ] && [ $c3_max -gt $col3 ]
    then 
      printf "%s\t%s\t%s\t%s\t%s\n" $col1 $col2 $col3 $col4 $col5
    fi
  done 
} < data_filter.txt

I tried like this...not working..

---------- Post updated at 04:24 AM ---------- Previous update was at 04:19 AM ----------

when I tried with ksh file.sh

column1	column2	column3	data1	data2
-0.018	64.008	4	28.18	36.43
-0.018	64.008	5	28.18	36.44
-0.018	64.008	6	28.19	36.45
-0.018	64.008	7	28.19	36.44
-0.018	64.008	8	28.94	34.97
data_filter.sh[30]: [: -1: unknown operator

I got this error

---------- Post updated at 04:36 AM ---------- Previous update was at 04:24 AM ----------

something wrong in if statement...