LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Specific result of awk script (http://www.linuxquestions.org/questions/programming-9/specific-result-of-awk-script-784349/)

jeesun 01-24-2010 04:34 AM

Specific result of awk script
 
Hi all

I'm using the following awk command to find out the users home directory from /etc/passwd file.

#grep jeesun /etc/passwd |awk 'BEGIN {FS=":"};{print $6}'

But problem is when there are two name like "jeesun" and "jeesunadm" exist the script shows both result not specific one.

#grep jeesun /etc/passwd |awk 'BEGIN {FS=":"};{print $6}'
/home/jeesun
/home/jeesunadm

But I need the exact name and its home directory.

druuna 01-24-2010 04:45 AM

Hi,

Try this:

awk -F: '$1 == "jeesun" { print $6}' /etc/passwd

Hope this helps.

jeesun 01-24-2010 05:23 AM

Great help. But when i put this in a single command it works.

When i put this in in a script it won't work because in script the " " is not executed. How will i use this in a script when i want to put any name?

druuna 01-24-2010 05:42 AM

Hi again,

Code:

#!/bin/bash

USERNAME=$1
INFILE="/etc/passwd"

awk -v username=$USERNAME -F: '$1 == username { print $6}' $INFILE

You need to "give" the bash variable to awk (the -v username=$USERNAME part).

Hope this helps.

PS: gawk users manual is a good reference for using gawk.


All times are GMT -5. The time now is 05:19 PM.