awk - printing the passwd file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - printing the passwd file
# 1  
Old 02-13-2013
awk - printing the passwd file

I've got a number of RHEL systems and I'm trying to use awk to read and format the output of /etc/passwd. But I'd like to display the host name of the system at the beginning of each line of output.

I've got it working without the adding the host name in this script:

Code:
#!/bin/bash

#HOST=`hostname -s`

 awk -F: '
        BEGIN {printf ("%-20s%-8s%-8s%-45s%-30s%-20s\n", "Username", "UID", "GID", "Description", "Home Directory", "Shell")}

        $3 > 100 && $3 < 50000 {printf ("%-20s%-8s%-8s%-45s%-30s%-20s\n", $1,$3,$4,$5,$6,$7) }

        ' /etc/passwd

Here's the output and it's the way I want it except for the host name. I don't think it copied and pasted well below:

Quote:
Username UID GID Description Home Directory Shell
rrdcached 101 102 rrdcached /var/rrdtool/rrdcached /sbin/nologin
nagios 501 100 /home/nagios /bin/bash
powersdp 502 504 /home/powersdp /bin/bash
westmorc 503 505 /home/westmorc /bin/bash
williamsj 2010 2010 Jason Williams /home/williamsj /bin/bash
Here's the script I'm having problems with. I've add the variable for the host name and add the formatting to the printf statements:

Code:
#!/bin/bash

HOST=`hostname -s`

 awk -F: '
        BEGIN {printf ("%-35s%-20s%-8s%-8s%-45s%-30s%-20s\n", "Hostname", "Username", "UID", "GID", "Description", "Home Directory", "Shell")}

        $3 > 100 && $3 < 50000 {printf ("%-35s%-20s%-8s%-8s%-45s%-30s%-20s\n", $HOST,$1,$3,$4,$5,$6,$7) }

        ' /etc/passwd

I'm sure it's something simple, But it's driving me nuts. Maybe someone can see what I'm doing wrong.
# 2  
Old 02-13-2013
awk doesn't automagically pickup shell variables unless its told to do so...
Code:
HOST=`hostname -s`
awk -v h=$HOST '{print h}' file

# 3  
Old 02-13-2013
Code:
awk -F: -v h="${HOST}"
.....
$3 > 100 && $3 < 50000 {printf ("%-35s%-20s%-8s%-8s%-45s%-30s%-20s\n", h,$1,$3,$4,$5,$6,$7) }
......

# 4  
Old 02-13-2013
Perfect. I've been wrestling with that one for weeks. I guess I should look at the book again
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk to clean up input file, printing both fields

In the f1 file below I am trying to clean it up removing lines the have _tn_ in them. Next, removing the characters in $2 before the ninth /. Then I remove the ID_(digit- always 4). Finally, the charcters after and including the first _. It is curently doing most of it but the cut is removing $1... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. Shell Programming and Scripting

[awk] printing value of a variable assignment from a file

Heyas Me try to print only the value of a (specific) variable assignment from a file. What i get (1): :) tui $ bin/tui-conf-get ~/.tui_rc TUI_THEME dot-blue "" "$TUI_DIR_INSTALL_ROOT/usr" "$TUI_DIR_INSTALL_ROOT/etc/tui" "$TUI_PREFIX/share/doc/tui" "$TUI_PREFIX/share/tui"... (2 Replies)
Discussion started by: sea
2 Replies

3. Shell Programming and Scripting

Awk: Comparing arguments with in line values of file and printing the result

I need to develop a script where I will take two date arguments as parameter date1 and date2 which will in format YYYYMM. Below is the input file say sample.txt. sample.txt will have certain blocks starting with P1. Each block will have a value 118,1:TIMESTAMP. I need to compare the... (7 Replies)
Discussion started by: garvit184
7 Replies

4. Shell Programming and Scripting

Printing from file with awk

Hi, I'm trying to find an efficient way with awk to print the lines from a file which match the string 'OFFLINE' and DO NOT match the string ONLINE within 2 minutes from the date stamp in column 1. The file is similar to the following: 09:40:26 HostC OFFLINE 09:40:43 HostA ... (7 Replies)
Discussion started by: keenboy100
7 Replies

5. AIX

When did AIX start using /etc/security/passwd instead of /etc/passwd to store encrypted passwords?

Does anyone know when AIX started using /etc/security/passwd instead of /etc/passwd to store encrypted passwords? (1 Reply)
Discussion started by: Anne Neville
1 Replies

6. Shell Programming and Scripting

AWK? parsing /etc/passwd file.

Hello guys, please help me to make simple script for parsing passwd file. I have many passwd files from our servers, named server1.pass, server2.pass etc.. so for server in `ls *.pass` i need to print these rows: server1;root:!:0:0::/root:/usr/bin/ksh... (7 Replies)
Discussion started by: rubico
7 Replies

7. Shell Programming and Scripting

awk printing only parts of file

I am afraid I don't understand awk well enough to do the following. I have a file with a bunch of select statements where the a line starts off with this pattern: "Last parsed SQL statement :", then continues with the select statement. At the first blank space I'd like it to stop, print that... (5 Replies)
Discussion started by: fwellers
5 Replies

8. UNIX for Dummies Questions & Answers

Printing File Contents Using AWK

I want to print the contents of a file from 2nd line to last but one line using the AWK command. I tried using the Shell Script,MyScript as fallows: MyScript: { if(NR>1) { if(NR<9) { print $0 } } } and used the commnd : awk -f MyScript Filename Note: Filename contains 9 lines text.... (0 Replies)
Discussion started by: ashok.g
0 Replies

9. Shell Programming and Scripting

need help with awk in printing the fields in a file

hi all i get a file from a server and i do not know how many fields that file will contain. i need to cut from the second column of the file to the last, irrespective of how many fields are there. is there any way to make the awk command dynamic for fetching from the second to the last... (4 Replies)
Discussion started by: sais
4 Replies

10. Shell Programming and Scripting

AWK - printing certain fields when field order changes in data file

I'm hoping someone can help me on this. I have a data file that greatly simplified might look like this: sec;src;dst;proto 421;10.10.10.1;10.10.10.2;tcp 426;10.10.10.3;10.10.10.4;udp 442;10.10.10.5;10.10.10.6;tcp sec;src;fac;dst;proto 521;10.10.10.1;ab;10.10.10.2;tcp... (3 Replies)
Discussion started by: eric4
3 Replies
Login or Register to Ask a Question