LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 03-14-2007, 11:45 AM   #1
SamuelHenderson
LQ Newbie
 
Registered: Sep 2006
Posts: 17

Rep: Reputation: 0
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.
 
Old 03-14-2007, 11:55 AM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
Go to

http://www.google.com

and google this:

Code:
bash tutorial
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.
 
Old 03-14-2007, 01:36 PM   #3
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 37
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 ....
 
Old 03-14-2007, 08:05 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
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
 
Old 03-14-2007, 11:43 PM   #5
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
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
 
Old 03-15-2007, 03:23 AM   #6
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:
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
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help w/ script to read file and parse variables cslink23 Linux - General 18 11-26-2006 02:22 AM
perl script to parse this file ohcarol Programming 10 11-02-2006 09:50 AM
ssimple shell script to parse a file ~sed or awk stevie_velvet Programming 7 07-14-2006 03:41 AM
Need help with perl/bash script to parse PicBasic file cmfarley19 Programming 13 11-18-2004 05:06 PM
use php script to parse a file. blackzone Linux - Software 1 07-07-2004 04:43 AM


All times are GMT -5. The time now is 08:07 PM.

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