AWK string comparisons

AWK string comparisons

Post by Matt » Fri, 23 Jan 2004 09:40:33



I have just finished reading through the awk manuals, but
am still unable to find a solution to my problem. I am
trying to find users who have been idle for 24-hours or
longer, and came up with the following:

who -u | awk '{ if ($6 > "24:00" ) print $1 $6 $7 }'

Under certain situations (idle user of 2:01) this will not
work. After reading through the string comparison section,
it looks like awk compares items per character position. Is
there a way to modify awk's string comparison mechanisms to
add a leading 0 if the value is < 9:59, or compare the entire
value as a whole? I guess I could convert to minutes and the
compare. Any thoughts?

- Matty

 
 
 

AWK string comparisons

Post by Ed Morto » Fri, 23 Jan 2004 21:41:27



> I have just finished reading through the awk manuals, but
> am still unable to find a solution to my problem. I am
> trying to find users who have been idle for 24-hours or
> longer, and came up with the following:

> who -u | awk '{ if ($6 > "24:00" ) print $1 $6 $7 }'

<snip>

You could just force a numeric comparison by getting rid of the ":" in
$6 then multiplying it by 1, e.g.:

who -u | awk '{t=$6; sub(":","",t)} 1*t > 2400 {print $1 $6 $7}'

I'm not 100% convinced you really NEED to add the "1*" but it doesn't
hurt. I'm assuming you've checked that who -u always outputs a
hours:mins format in the 6th field and doesn't add days or allow spaces
in preceeding field names or anything.

Regards,

        Ed.

 
 
 

AWK string comparisons

Post by Carlos J. G. Duart » Sat, 24 Jan 2004 09:58:31



> I have just finished reading through the awk manuals, but
> am still unable to find a solution to my problem. I am
> trying to find users who have been idle for 24-hours or
> longer, and came up with the following:

> who -u | awk '{ if ($6 > "24:00" ) print $1 $6 $7 }'

> Under certain situations (idle user of 2:01) this will not

Handle those cases! Ex:
awk '{ s=$6; if (length(s)==4) s="0" s; if (s > "24:00") print $1 $6 $7}'

--
carlos ** http://cgd.sdf-eu.org

 
 
 

1. Bash string comparison not working

I want to grab a copy of the website and store the md5 signature of the
index page in a file I am calling "journal.txt". Every few hours, I want to
fire the script and grab a fresh copy of the page. If the signature of the
page does not match the last entry in the journal, I will tell it to send me
an email. The problem is that I can't get it to recognize that the signature
is different. Echoing both variables to the screen shows that the correct
values are being stored in each variable, so I know it's playing with the
right strings. Does anybody know why $OLDHASH and $NEWHASH are being treated
as the same even when they are different? Is it possible that text strings
need some kind of special treatment?

Here's the part that's giving me problems:

----------------begin--------------------
!# /bin/bash

wget http://www.website.com

OLDHASH=`tail -1 journal.txt`
echo "The old signature is $OLDHASH"

NEWHASH=`md5sum index.html`
echo "The new signature is $NEWHASH"

md5sum index.html >>journal.txt
mv index.html index.html-$(date +%y%m%d%H%M)

  if [ "$OLDHASH"=="$NEWHASH" ] ; then
       echo "No changes detected"
  fi

------------------end------------------------

Thank You for your time
--
Julie Ventimiglia

2. ide harddrive spindown

3. string comparison problem

4. Dual Boot with Windows 95

5. String Comparison

6. Which video card should I buy?

7. Multiple String Comparison in Ksh.

8. Mutliple ISP's?

9. string comparison not working

10. string comparisons

11. Sting Comparisons How do i find if one string contains another

12. string comparison

13. question on string comparison in ksh93