LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Good time to use AWK? (https://www.linuxquestions.org/questions/linux-general-1/good-time-to-use-awk-708874/)

VeeDubbs 03-03-2009 01:52 PM

Good time to use AWK?
 
Hi all -

I need to get minimal information from many many files. This is from our e-mail system, and each person has a file called account.settings which contains the following:

Code:

{
 AccessModes = (23,Mail,Relay,Signal,Mobile,TLS,POP,IMAP,MAPI,AirSync,SIP,XMPP,WebMail,XIMSS,FTP,ACAP,PWD,LDAP,RADIUS,"S/MIME",WebCAL,PBX,HTTP);
 AddMailTrailer = NO;
 AddWebBanner = NO;
 ExternalINBOX = NO;
 MaxAccountSize = 100M;
 PWDAllowed = YES;
 Password = password;
 RPOPAllowed = YES;
 RealName = "Bob Smith";
 RecoverPassword = name@email.com;
}

This file is held in a folder with the format username.macnt. So if my name is Bob Smith, my account.settings file is in the bsmith.macnt folder -- we have 5,642 accounts.

The information I need is: username, first name, last name & password.

What is the easiest possible way to do this? Is awk/sed an option?

allend 03-03-2009 08:39 PM

This little script run in the directory that contains the *.macnt directories should isolate the information you want.
Code:

#!/bin/bash
#
# script to parse mail account files

for ifname in $(find . -type d -name "*.macnt" -print)
        do
                echo $(basename $ifname)
                grep "Password ="\|"RealName =" $ifname/account.settings
        done
exit


Tinkster 03-04-2009 02:41 AM

Or in awk
Code:

$ ls
bsmith.macnt
$ ls bsmith.macnt
account.settings         
$ find -type d -name \*.macnt -exec awk -vdir={} 'BEGIN{FS="[=;]";printf "%s,", gensub( /..([a-z]+).macnt/, "\\1", 1, dir)}/RealName/{name=$2}/ Password/{pwd=$2}END{printf "%s,%s\n",name,pwd}' {}/account.settings \;
bsmith, "Bob Smith", password


Cheers,
Tink

allend 03-04-2009 07:04 AM

Tinkster wins!
A very neat and instructive one liner. My congratulations.

I knew awk could do it, but, being a coward, rather than spend the time that it would take me to produce that, I would clean up the output from my script in a text editor.

sundialsvcs 03-04-2009 06:58 PM

awk is a great tool, well worth knowing.

The perl programming-language "ascended from" awk, and proceeded to the next galaxy.

It is well worth the time to become at-least cursorily familiar with the cornucopia of power-tools that are available in Linux... all for free. Bear in mind that you will never complete the task.

VeeDubbs 03-08-2009 10:01 PM

Quote:

Originally Posted by Tinkster (Post 3464287)
Or in awk
Code:

$ ls
bsmith.macnt
$ ls bsmith.macnt
account.settings         
$ find -type d -name \*.macnt -exec awk -vdir={} 'BEGIN{FS="[=;]";printf "%s,", gensub( /..([a-z]+).macnt/, "\\1", 1, dir)}/RealName/{name=$2}/ Password/{pwd=$2}END{printf "%s,%s\n",name,pwd}' {}/account.settings \;
bsmith, "Bob Smith", password


Cheers,
Tink

Tink!! That is awesome! Thank you soo much!

I hate to ask another question since you've already helped out so much! But....

Is there anyway possible to get the first name and last name separatde by a comma? So they are two separate fields?

Thanks!

Vee

Tinkster 03-09-2009 04:34 PM

Quote:

Originally Posted by VeeDubbs (Post 3469170)
Tink!! That is awesome! Thank you soo much!

I hate to ask another question since you've already helped out so much! But....

Is there anyway possible to get the first name and last name separatde by a comma? So they are two separate fields?

Thanks!

Vee

Well, yes ... the only potential issue is about data integrity.
Will the names always come in a "First Name" "Last Name" pair, can
the order vary? Are there people with first name & middle name?

And here an untested variant ... it *should* take out the quotes,
and replace the separating space in the name with a comma and a space.

Code:

find -type d -name \*.macnt -exec awk -vdir={} 'BEGIN{FS="[=;]";printf "%s,", gensub( /..([a-z]+).macnt/, "\\1", 1, dir)}/RealName/{name=gensub(/ /, ", ", "g", gensub(/"/, "", "g", $2))}/ Password/{pwd=$2}END{printf "%s,%s\n",name,pwd}' {}/account.settings \;


Cheers,
Tink

Tinkster 03-12-2009 06:23 PM

And did this do the trick?

VeeDubbs 03-13-2009 08:09 AM

Quote:

Originally Posted by Tinkster (Post 3473713)
And did this do the trick?

Almost Tinkster. I get:

username,, first name, last name, password

Those double commas I can't seem to find where it's coming from.

Thanks for the help again!

Tinkster 03-13-2009 10:55 PM

Code:

find -type d -name \*.macnt -exec awk -vdir={} 'BEGIN{FS="[=;]";printf "%s,", gensub( /..([a-z]+).macnt/, "\\1", 1, dir)}/RealName/{name=gensub(/ /, ", ", "g", gensub(/"/, "", "g", $2))}/ Password/{pwd=$2}END{printf "%s,%s\n",name,pwd}' {}/account.settings \;
Remove the red, bold one ...


All times are GMT -5. The time now is 01:17 PM.