chillypedia
chillypedia

Reputation: 1

Assistance needed with bash script parsing data from a file

Would first like to thank everyone for taking the time and reviewing this, and providing some assistance.

I am stuck on this bash script project I have been working on. This script is supposed to pull data from this file, export it to a csv, and then email it out. I was able to grab the required data and email it to myself but the problem is that the groups in the file have special characters. I need to have the lsgroup command executed on those groups in order to retrieve the users and then have it exported to the csv file.

For example, below is sample data that are in the file and how it looks like:

[Skyrim]
  comment = Elder Scrolls  
  path = /export/skyrim/elderscrolls 
  valid users = @dawnstar nords @riften 
  invalid users = @lakers

[SONY]
  comment = PS4
  path = /export/Sony/PS4
  valid users = @insomniac @activision 
  invalid users =  peterparker controller @pspro

The script is supposed to be gathering the name, comment, path, valid users, invalid users, and exporting them to the csv

So far this is what I have that works,

out="/tmp/parsed-report.csv"
file="/tmp/file.conf"

echo "name,comment,path,valid_users,invalid_users" > $out;

scp -q server:/tmp/parse/file.conf $out

grep "^\[.*\]$" $file |grep -Ev 'PasswordPickup|global' | while read shr ; do

    shr_regex=$(echo "$shr" | sed 's/[][]/\\&/g')
    shr_print=$(echo "$shr"|sed 's/[][]//g')

com=$(grep -p "$shr_regex"  $file|grep -v "#"| grep -w "comment"| awk -F'=' '{print $2}'|sed 's/,/ /g')
path=$(grep -p "$shr_regex" $file|grep -v "#"| grep -w "path"| awk -F'=' '{print $2}')
val=$(grep -p "$shr_regex"  $file|grep -v "#"| grep -w "valid users"| awk -F'=' '{print $2}')
inv=$(grep -p "$shr_regex"  $file|grep -v "#"| grep -w "invalid users"| awk -F'=' '{print$2}')

echo "$shr_print,$com,$path,$val,$inv" >> $out

done

exit 0

The text with '@' are considered groups so if $var3='@' then run the lsgroup command and export the data to csv file under the correct category, else if $vars3!='@' then export users to the csv file.

This is what I tried to come up with:

vars3="$val$inv"
Server="server_1"
for lists in $(echo "$vars3"); do
  if [[ $lists = *[!\@]* ]]; then
    ssh -q $Server "lsgroup -a users $(echo "$lists"|tr -d /@/)|awk - 
    F'=' '{print $1}'" > print to csv file as valid or invalid users
  else [[ $lists != *[!\@]* ]]; then
    echo "users without @" > to csv file as valid or invalid users

With the right commands the output should look like this

: skyrim
Comment: Elder Scrolls
Path:  /export/skyrim/elderscrolls  
Valid Users:  dragonborn argonian kajit nords
Invalid Users :  Shaq Kobe Phil Lebron 

: SONY
Comment: PS4
Path:  /export/Sony/PS4
Valid Users:   spiderman ratchet&clank callofduty spyro
Invalid Users :  peterparker controller 4k

Upvotes: 0

Views: 92

Answers (2)

Cyrus
Cyrus

Reputation: 88766

Create a file file.sed with this content:

s/\[/: /            # replace [ with : and one space
s/]//               # remove ]
s/^  //             # remove leading spaces
s/ = /:  /
s/@lakers/Shaq Kobe Phil Lebron/
s/^comment/Comment/

# Can be completed by you here.

and then use

sed -f file.sed your_sample_data_file

Output:

: Skyrim
Comment:  Elder Scrolls
path:  /export/skyrim/elderscrolls
valid users:  @dawnstar nords @riften
invalid users:  Shaq Kobe Phil Lebron

: SONY
Comment:  PS4
path:  /export/Sony/PS4
valid users:  @insomniac @activision
invalid users:   peterparker controller @pspro

Upvotes: 1

cfelipe
cfelipe

Reputation: 350

Parsing things is a hard problem and, in my opinion, writing your own parser is unproductive.

Instead, I highly advise you to take your time and learn about grammars and parsing generators. Then you can use some battle tested library such as textX to implement your parser.

Upvotes: 0

Related Questions