Re: How to - Parse a field from one file to be used to parse data from another file.



On 2006-06-13, lnrntx@xxxxxxxxx wrote:
Hi All!

I am very green when it comes to scripting, very green!!!

And I have to create a report that displays like an excel spreadsheet
identifying the userid, username, user groups, shadow, sudoer info, &
user ldap info.

And I have been trying to parse the group ID of a user from the passwd
file and use the group ID value to parse the group name from the group
file.

I need something like this -

awk -F: '{print $1 " " $4}' /etc/passwd | grep $4 /etc/group

but I know I can not grep the value of $4, that is from the passwd
file, to get that value in the group file but can not figure out how to
use the value of $4 to obtain the information of the users groups even
if I parse it to an output file I still need to know how to read that
file values to obtain the groups of the user.

Searching for the user's gid in /etc/group isn't going to help.
You need to search for the userid.

I also thought I could grep the userid from the passwd and then use the
group command to list the groups.

The command is 'groups', and it is deprecated in favour of 'id'.

How would I take a field from one file and use that field value to
grep from another file?

Do exactly what you described; don't try to write a clever script
until you know the basics.

while IFS=: read user p u gid nn
do
groups=$( grep -e "[:,]$user," -e "[:,]$user$" /etc/group | cut -d: -f1)
[ -n "$groups" ] && {
printf "%s: " "$user"
printf "%s," $groups
printf "\n"
}
done < /etc/passwd

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
.



Relevant Pages

  • Re: Running Login Script after establishing a VPN connection
    ... I have to figure out how to parse the file correctly. ... scripting, so everything is slow going for me. ... "I suppose I could call up the text file with the actuall mapped drives ... and parse it for UNC paths. ...
    (microsoft.public.windows.server.scripting)
  • Re: CMD script parser
    ... > I nned a utility that will parse through .txt files and wherever it see a ... > carriage return it will replace with a semi-colon. ... -- torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway Administration scripting examples and an ONLINE version of the 1328 page Scripting Guide: ...
    (microsoft.public.windows.server.general)
  • Re: CMD script parser
    ... > I nned a utility that will parse through .txt files and wherever it see a ... > carriage return it will replace with a semi-colon. ... -- torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway Administration scripting examples and an ONLINE version of the 1328 page Scripting Guide: ...
    (microsoft.public.win2000.cmdprompt.admin)
  • Parsing with Expect Scripts
    ... I'm new to expect scripting and I'm looking for a way to output my data ... to a file with the current date stamp and then parse that file for ... I have a working script already that will run the commands I need so at ... I need to know how to output this to a file and parse. ...
    (comp.lang.tcl)
  • Re: Disable printf
    ... I guess the gcc doesn't know how to handle the std c lib if you ... More likely the problem is that printf is a variadic ... If you don't have a favourite scripting language, ...
    (comp.lang.c)