Computing.Net > Forums > Unix > Awk script

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Awk script

Reply to Message Icon

Name: yacob
Date: March 15, 2008 at 06:39:10 Pacific
OS: Unix
CPU/Ram: 2GB
Comment:

Dear all

I have an awk script as below. I would like the script to do the if condition based on the number that pics from the subsrt command and then compare with the number that I set for the comparision.

#!/bin/sh
for i in DX* ;
do
echo $i | awk '{ if ( substr($1, 28, 3) < 10 && substr($1, 36, 3) > 20 ) print "jun_"substr($1, 24, 4)}' >> out
done

Now, I have a file of the form:
-----2_2003001-2003031-----

Here the substr pics 001 for the first condition and 031 for the second condition. But my script does not able to pic this file , which apparently does not recognise that 001 is less than 10 and 031 is greater than 20.

How one can fix this problem ? You may propose that to pic the last character of "001" but the files runs from 001 until 335.

I would appreciate if you could help !

Regards
Yacob



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: March 15, 2008 at 10:28:10 Pacific
Reply:

The problem is that you are comparing a character string to an integer. This awk stub fails:


echo ""|awk ' {
if ("065" < 10)
print "its less than 10"
else
print "its greater than 10"

} '


With awk, adding zero performs a numerc conversion. Here is a fix:

echo ""|awk ' {

if ("065"+0 < 10)
print "its less than 10"
else
print "its greater than 10"

} '

In your case you can probably do something like this:

if ( (substr($1, 28, 3)+0) < 10 && (substr($1, 36, 3)+0) > 20 )


0
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Awk script

tweaking William's awk script www.computing.net/answers/unix/tweaking-williams-awk-script/5174.html

awk script help www.computing.net/answers/unix/awk-script-help/5596.html

awk call other awk script? www.computing.net/answers/unix/awk-call-other-awk-script/5574.html