0

I'm trying to create awk script that can provide a structured listing of account information for all UIDs greater than 1000. This is what I tried: This is the expected output

BEGIN { FS=":"}

$3>1000 && $3<2000 {print $1   "  uid: " $3 " Full name: " $5 " Home dir "$6 " Shel :"$7}BEGIN { FS=":"} 

the resualt i got

2
  • And what have you tried so far?
    – Matej
    Feb 19, 2019 at 4:59
  • BEGIN { FS=":"} $3>1000 && $3<2000 {print $1 " uid: " $3 " Full name: " $5 " Home dir "$6 " Shel :"$7}BEGIN { FS=":"}
    – Luay Aq
    Feb 19, 2019 at 5:47

2 Answers 2

0

Try this:

BEGIN { FS=":" }
{
  if($3 > 1000 && $3 < 2000){
    print $1
    print " uid: " $3
    print " Full name: " $5
    print " Home dir: " $6
    print " Shel: " $7
  }
}

First line - BEGIN { FS=":" } - sets field separator to : on the beginning of script run. Every line will be splitted to separate columns and saved to variables $1 - $n (n is number of separated fields).

After that you have there { } and this will be executed for every input line. There is the if inside and it checks uid value, prints will print required datas if value is correct.

0
0

The problem here is that awk is interpreting the UIDs as strings; you can force it to interpret as integers for the comparison by doing integer calculations with it.

The easiest way is to add a 0 to the value.

So

$3 + 0 > 1000 && $3 + 0 < 2000

Here is a good awk reference for this: https://www.gnu.org/software/gawk/manual/html_node/Strings-And-Numbers.html#Strings-And-Numbers

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .