1836100 Members
2180 Online
110089 Solutions
New Discussion

Re: UNIX script help

 
Robert_73
Occasional Advisor

UNIX script help

Hi! List,
I am trying to write a script which will check the first field in art1 file, if it is less than 1100 it should print second field.

Here is the script:
#!/bin/sh
#
# Usage : <script_name> input_file output_file
####

# Removes previous output file
rm $2 2>/dev/null

while read LINE
do
FST_FIELD=`echo $LINE | cut -d " " -f 1`
SND_FIELD=`echo $LINE | cut -d " " -f 2`
if (( ${FST_FIELD} < 1100 ))
then
echo 'First field = '$FST_FIELD' Second Field = '$SND_FIELD
fi
done < /tmp/art1

Errors:
SND_FIELD=200205281021_3cf3925c.137f.69_BW.html
+ 9416
rmb1.sh: 9416: not found
+ read LINE
+ echo 9496 200205281021_3cf3925c.137f.94_BW.html
+ cut -d -f 1
FST_FIELD=9496
+ echo 9496 200205281021_3cf3925c.137f.94_BW.html
+ cut -d -f 2
SND_FIELD=200205281021_3cf3925c.137f.94_BW.html
+ 9496
rmb1.sh: 9496: not found
+ read LINE
+ echo 5659 200205281021_3cf3925d.137f.148_BW.html
+ cut -d -f 1
FST_FIELD=5659




contents of /tmp/art1
4301 200206030418_3cfb5e62.3e6b.169.1.html
1710 200206030418_3cfb5e63.3e6b.170.1.html
5155 200206030443_3cfb6381.582d.51.2.html
6254 200206030443_3cfb6386.582d.53.2.html
4301 200206030443_3cfb638f.582d.58.2.html
4202 200206030443_3cfb63a1.582d.70.1.html
3817 200206030443_3cfb6457.582d.184.1_UPI.html
3119 200206030443_3cfb645a.582d.186.1.html
3833 200206030518_3cfb6b67.343.34.2.html
3599 200206030518_3cfb6b87.343.56.2.html

Script is failing to compare, any ideas?

Thanks in advance
UNIXDUDE
5 REPLIES 5
A. Clay Stephenson
Acclaimed Contributor

Re: UNIX script help

Change your if
if (( ${FST_FIELD} < 1100 ))
to
if [ ${FST_FIELD} -lt 1100 ]
then
....
....
fi


If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: UNIX script help

Also:

echo 'First field = '$FST_FIELD' Second Field = '$SND_FIELD

should be
echo "First field = \"${FST_FIELD}\" Second Field = \"${SND_FIELD}\""

You want the shell to interpret the values, hence " rather than '.

If it ain't broke, I can fix that.
Jordan Bean
Honored Contributor

Re: UNIX script help

Try this:

#!/usr/bin/sh
typeset -i i=0
if [ $# -eq 2 ]
then
if [ -s $1 ]
then
while read i line
do
if [ $i -lt 1100 ]
then
echo $line
fi
done < $1 > $2
fi
fi
Leif Halvarsson_2
Honored Contributor

Re: UNIX script help

Hi

awk is also a good tool for this type of problems. Try:
awk '$1 < 1100 { print $2 }'
Peter Kloetgen
Esteemed Contributor

Re: UNIX script help

Hi Robert,

two mistakes in your script:

if [ $FST_FIELD -lt 1100 ]
then
echo "First field = $FST_FIELD Second Field = $SND_FIELD"
......


will do it for you.

Allways stay on the bright side of life!

Peter
I'm learning here as well as helping