LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-24-2010, 05:34 AM   #1
jeesun
Member
 
Registered: Aug 2007
Location: Australia
Distribution: RedHat Enterprise
Posts: 82

Rep: Reputation: 15
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.
 
Old 01-24-2010, 05:45 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

Try this:

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

Hope this helps.
 
1 members found this post helpful.
Old 01-24-2010, 06:23 AM   #3
jeesun
Member
 
Registered: Aug 2007
Location: Australia
Distribution: RedHat Enterprise
Posts: 82

Original Poster
Rep: Reputation: 15
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?
 
Old 01-24-2010, 06:42 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem exporting result of awk to an array... mitramcc Linux - Newbie 2 02-10-2009 12:47 PM
using awk on a awk result jpgauvin Programming 1 12-15-2008 04:57 AM
Get all lines containing 23 specific words with AWK cgcamal Programming 3 11-05-2008 11:51 AM
cannot export result from awk into a variable in a bash script Emmanuel_uk Linux - Newbie 4 03-07-2005 02:54 AM
search for specific text in fields using awk Helene Programming 2 04-23-2004 01:13 AM


All times are GMT -5. The time now is 11:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration