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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
03-03-2009, 01:52 PM
|
#1
|
|
Member
Registered: Feb 2006
Location: Wisconsin
Distribution: SuSe 10. Ubuntu
Posts: 47
Rep:
|
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?
|
|
|
|
03-03-2009, 08:39 PM
|
#2
|
|
Senior Member
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 2,749
|
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.
|
|
|
|
03-04-2009, 02:41 AM
|
#3
|
|
Moderator
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,903
|
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.
|
|
|
|
03-04-2009, 07:04 AM
|
#4
|
|
Senior Member
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 2,749
|
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.
|
|
|
|
03-04-2009, 06:58 PM
|
#5
|
|
Senior Member
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 4,554
|
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.
|
|
|
|
03-08-2009, 10:01 PM
|
#6
|
|
Member
Registered: Feb 2006
Location: Wisconsin
Distribution: SuSe 10. Ubuntu
Posts: 47
Original Poster
Rep:
|
Quote:
Originally Posted by Tinkster
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
|
|
|
|
03-09-2009, 04:34 PM
|
#7
|
|
Moderator
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,903
|
Quote:
Originally Posted by VeeDubbs
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
|
|
|
|
03-12-2009, 06:23 PM
|
#8
|
|
Moderator
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,903
|
And did this do the trick?
|
|
|
|
03-13-2009, 08:09 AM
|
#9
|
|
Member
Registered: Feb 2006
Location: Wisconsin
Distribution: SuSe 10. Ubuntu
Posts: 47
Original Poster
Rep:
|
Quote:
Originally Posted by Tinkster
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!
|
|
|
|
03-13-2009, 10:55 PM
|
#10
|
|
Moderator
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,903
|
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 ...
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 01:37 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|