LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   a script to parse a file (http://www.linuxquestions.org/questions/programming-9/a-script-to-parse-a-file-537442/)

SamuelHenderson 03-14-2007 11:45 AM

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


wjevans_7d1@yahoo.co 03-14-2007 11:55 AM

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.

muha 03-14-2007 01:36 PM

Hmmm, sed :D
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 ....

ghostdog74 03-14-2007 08:05 PM

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

kshkid 03-14-2007 11:43 PM

Quote:

Originally Posted by muha
Hmmm, sed :D
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

Tinkster 03-15-2007 03:23 AM

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


All times are GMT -5. The time now is 06:22 AM.