Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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
doneNow, 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

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 )

![]() |
![]() |
![]() |

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