Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| 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-14-2007, 11:45 AM
|
#1
|
|
LQ Newbie
Registered: Sep 2006
Posts: 17
Rep:
|
a script to parse a file
Good day,
What I am trying to do is parse a file that contains a list of policies that are active or inactive.
I need to go over each policy and see if it is active, if it is then execute a command. If not then go the next policy and see if it active/inactive;
The list is built like so:
Code:
Policy Name: POLICY_NAME_ONE
.. //useless info
.. //useless info
Active: YES
.. //more useless info
.. //more useless info
Policy Name: POLICY_NAME_TWO
.. //useless info
.. //useless info
Active: NO
.. //more useless info
.. //more useless info
I've never done alot of scripting before so I am not exactly sure how to approach this.
Here's my miserable attempt so far:
Code:
#!/usr/bin/ksh
FILENAME="bppllist_U.txt"
if[[grep "Policy Name: \|Active:" $FILENAME = "yes" ]] ; then
echo "FIRE!";
fi
Last edited by SamuelHenderson; 03-14-2007 at 11:58 AM.
|
|
|
|
03-14-2007, 11:55 AM
|
#2
|
|
Member
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938
Rep:
|
Go to
http://www.google.com
and google this:
You'll find a wealth of material. Some of it is too simple for what you want to do; some of it might be too complex.
Hope this helps.
|
|
|
|
03-14-2007, 01:36 PM
|
#3
|
|
Member
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451
Rep:
|
Hmmm, sed 
This is a start:
Code:
# sed -ne '/Policy/,/Active/s/Policy\sName:\s*\(.*\)\|\(\s*Active:\s*\([a-zA-Z]*\)\)/\1\3/p' file
POLICY_NAME_ONE
YES
POLICY_NAME_TWO
NO
But, basicly you only want the names of the 'Active: YES' policies ....
|
|
|
|
03-14-2007, 08:05 PM
|
#4
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
if you have Python, here's an alternative:
Code:
#!/usr/bin/python
data = open("file").readlines()
for num,line in enumerate(data):
if 'Active: YES' in line:
print data[num-3].split(":")[1].strip(), "Active"
output:
Code:
POLICY_NAME_ONE Active
|
|
|
|
03-14-2007, 11:43 PM
|
#5
|
|
Member
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383
Rep:
|
Quote:
|
Originally Posted by muha
Hmmm, sed 
This is a start:
Code:
# sed -ne '/Policy/,/Active/s/Policy\sName:\s*\(.*\)\|\(\s*Active:\s*\([a-zA-Z]*\)\)/\1\3/p' file
POLICY_NAME_ONE
YES
POLICY_NAME_TWO
NO
But, basicly you only want the names of the 'Active: YES' policies ....
|
just this would do,
Code:
sed -ne '/Policy/,/Active/s/Policy Name:\s *\s\|\s*Active:\s*//p' filename
|
|
|
|
03-15-2007, 03:23 AM
|
#6
|
|
Moderator
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,916
|
Or in awk:
Code:
awk 'BEGIN{FS="\n";RS="\n\n"} /Active: +YES/ {print gensub("Policy Name: +(.+)", "\\1", 1, $1)}' file
That will spit out only the name(s) of active policies.
Code:
$ cat file
Policy Name: POLICY_NAME_ONE
.. //useless info
.. //useless info
Active: YES
.. //more useless info
.. //more useless info
Policy Name: POLICY_NAME_TWO
.. //useless info
.. //useless info
Active: NO
.. //more useless info
.. //more useless info
$ awk 'BEGIN{FS="\n";RS="\n\n"} /Active: +YES/ {print gensub("Policy Name: +(.+)", "\\1", 1, $1)}' file
POLICY_NAME_ONE
Cheers,
Tink
|
|
|
|
| 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 02:04 AM.
|
|
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
|
|