LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 03-03-2009, 01:52 PM   #1
VeeDubbs
Member
 
Registered: Feb 2006
Location: Wisconsin
Distribution: SuSe 10. Ubuntu
Posts: 47

Rep: Reputation: 15
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?
 
Old 03-03-2009, 08:39 PM   #2
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 3,379

Rep: Reputation: 824Reputation: 824Reputation: 824Reputation: 824Reputation: 824Reputation: 824Reputation: 824
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

Last edited by allend; 03-03-2009 at 08:40 PM.
 
Old 03-04-2009, 02:41 AM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
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

Last edited by Tinkster; 03-04-2009 at 02:47 AM.
 
Old 03-04-2009, 07:04 AM   #4
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 3,379

Rep: Reputation: 824Reputation: 824Reputation: 824Reputation: 824Reputation: 824Reputation: 824Reputation: 824
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.
 
Old 03-04-2009, 06:58 PM   #5
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,263

Rep: Reputation: 1085Reputation: 1085Reputation: 1085Reputation: 1085Reputation: 1085Reputation: 1085Reputation: 1085Reputation: 1085
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.
 
Old 03-08-2009, 10:01 PM   #6
VeeDubbs
Member
 
Registered: Feb 2006
Location: Wisconsin
Distribution: SuSe 10. Ubuntu
Posts: 47

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Tinkster View Post
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
 
Old 03-09-2009, 04:34 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
Quote:
Originally Posted by VeeDubbs View Post
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
 
Old 03-12-2009, 06:23 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
And did this do the trick?
 
Old 03-13-2009, 08:09 AM   #9
VeeDubbs
Member
 
Registered: Feb 2006
Location: Wisconsin
Distribution: SuSe 10. Ubuntu
Posts: 47

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Tinkster View Post
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!
 
Old 03-13-2009, 10:55 PM   #10
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
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 ...
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
having a good time:) sycamorex General 3 11-11-2008 08:48 AM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM
Good time to everybody CrackHarday LinuxQuestions.org Member Intro 3 01-17-2006 07:23 AM
Good tutorial for sed/awk or both !! paonethestar Programming 2 11-08-2003 03:38 PM
time for a good cheer! gui10 Linux - General 0 05-08-2002 04:27 AM


All times are GMT -5. The time now is 03:59 AM.

Main Menu
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