0

I'm trying to the use the following awk command to return a user from a file that lists all users on the system but only if the 8th field is empty and they match the user variable (it's being using in a loop):

awk -F':' -v user="$user" 'index($0, user) {if ($8=="") print $1}' file

My issue currently is that it returns users that contain the string and aren't exact matches. Any way around this?

  • Even if you answered the question yourself it would be good to show some sample input and a corresponding username that can be used to reproduce your problem. – Bodo Mar 22 at 12:22
0

above answers are correct...However we can extract the details using the below command.

awk -F ',' '{if ( $9 == "" && $1 =="$user") print $1}' file

0

Solved it.

I ended up using:

awk -F':' -v user="$user" '$1 == user {if ($8 == "") print $1}' file

I'm a silly boy lol

  • 4
    Alternatively, awk ... '$1 == user && $8 == "" { print $1 }' – Kusalananda Mar 22 at 11:56
  • 1
    It's best to avoid -v and use environment variables instead (and ENVIRON["VARNAME"] in awk) as -v mangles values that contains backslashes. For instance, with user='r\157ot', that would return the entry for root as -v would expand that \157 to o – Stéphane Chazelas Mar 22 at 18:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.