Help me with awk and grep script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help me with awk and grep script
# 1  
Old 04-07-2010
Help me with awk and grep script

Code:
#!/bin/sh
PRINTF=/usr/bin/printf

$PRINTF "Enter a UserID\n"
read USERID

for USERID in `cat passwd | awk -F : '{print $1}'`
do
    USERHOME=`grep ^$/qianxin/ passwd | awk -F : '{print $6}'`
    USERSHELL=`grep ^$/qianxin/ passwd | awk -F : '{print $7}'`

$PRINTF "$USERID\n"
$PRINTF "$USERHOME\n"
$PRINTF "$USERSHELL\n"

done

Hi all, I got execution problem with this script, can someone please help me fix it?

In this script, first promote the user enter a valid ID, then if the ID is in the first field of a passwd file, system will print out the sixth and seventh filed. The type of a passwd file is in this way:
root:89898:93434:root...:dfdfd:ererer:ferer:

thx a lot!
# 2  
Old 04-07-2010
What is the error you are getting?

Try using the full path for the password file if you are not executing from that directory.


Also not sure why you are using this

Code:
grep ^$/....


If your requirement is to grep the user_id just entered remove the forloop

Code:
#!/bin/sh
PRINTF=/usr/bin/printf

$PRINTF "Enter a UserID\n"
read USERID

USERHOME=`grep "$USERID" /etc/passwd | awk -F : '{print $6}'`
USERSHELL=`grep "$USERID" /etc/passwd | awk -F : '{print $7}'`

$PRINTF "$USERID\n"
$PRINTF "$USERHOME\n"
$PRINTF "$USERSHELL\n"

HTH,
PL

Last edited by daptal; 04-07-2010 at 01:05 AM..
# 3  
Old 04-07-2010
Thx for u help.

BTW, is there any files that map userid to home directory and other information?
# 4  
Old 04-09-2010
One (of many) unix programs which give this information is "finger".

Code:
finger -m username

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Piping grep into awk, read the next line using grep

Hi, I have a number of files containing the information below. """"" Fundallinfo 6.3950 14.9715 14.0482 """"" I would like to grep for Fundallinfo and use it to read the next line? I ideally would like to read the three numbers that follow in the next line and... (2 Replies)
Discussion started by: Paul Moghadam
2 Replies

2. Homework & Coursework Questions

Shell Script: Sorting by column using grep/awk

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: You will write a script that will read a tab-separated file that contains the names of all 50 states &... (7 Replies)
Discussion started by: tburns517
7 Replies

3. Shell Programming and Scripting

Using awk or grep with Variables in a script

Good day Geeks, Am having an issue with using variables in a rather simple script, the script is as follows: #!/bin/bash ### Script written by Adigun Gbenga ### Date: April 28, 2012 array=( 1 2 3 4 5 29 7 8 9... (6 Replies)
Discussion started by: infinitydon
6 Replies

4. Shell Programming and Scripting

AWK/GREP: grep only lines starting with integer

I have an input file 12.4 1.72849432773174e+01 -7.74784188610632e+01 12.5 9.59432114416327e-01 -7.87018212757537e+01 15.6 5.20139995965960e-01 -5.61612429666624e+01 29.3 3.76696387248366e+00 -7.42896194101892e+01 32.1 1.86899877018077e+01 -7.56508762501408e+01 35 6.98857157014640e+00... (2 Replies)
Discussion started by: chrisjorg
2 Replies

5. Shell Programming and Scripting

Script with awk and grep

Hi .. I am working on a shell script to do following task. I have Input file as follows. I have to find failed or Offline disk and run command # hic -ip 172.124.24.59 getlogicalgrp |grep -B10 diskid against the disk ID which is 1 line above the disk state. I am using grep with -b10 is because... (7 Replies)
Discussion started by: dynamax
7 Replies

6. Shell Programming and Scripting

Shell script / Grep / Awk to variable and Loop

Hi, I have a text file with data in that I wish to extract, assign to a variable and process through a loop. Kind of the process that I am after: 1: Grep the text file for the values. Currently using: cat /root/test.txt | grep TESTING= | awk -F"=" '{ a = $2 } {print a}' | sort -u ... (0 Replies)
Discussion started by: Spoonless
0 Replies

7. Shell Programming and Scripting

AWK script for directory listing using GREP

Hi, All - script1.awk contains the following script: BEGIN { printf "The following are directory files:" "ls -l | grep '^d' | {print $0}" printf "There were "NR" directories in this list." } When I run the script, Here is what I get: $ awk -f... (4 Replies)
Discussion started by: ora_umair
4 Replies

8. Shell Programming and Scripting

MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else

Hi Guys, I need to set the value of $7 to zero in case $7 is NULL. I've tried the below command but doesn't work. Any ideas. thanks guys. MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else { print $7}}' ` Harby. (4 Replies)
Discussion started by: hariza
4 Replies

9. Shell Programming and Scripting

grep, awk, typeset in a shell script..

For e.g I have a file named "relation" which has three coloums i.e JOHN MARY JACK PETE ALISIA JONNY TONY JACKIE VICTOR If I do grep -w 'JOHN' relation | awk '{print""$1" is husband of "$2" & father of "$3""}' It gives out JOHN i husband of MARY & father of JACK (which is desired... (7 Replies)
Discussion started by: nick_25
7 Replies

10. Shell Programming and Scripting

script ksh/awk/grep

How can I extract the 9th line of a text file. thanks vm.:confused: (2 Replies)
Discussion started by: hoang
2 Replies
Login or Register to Ask a Question