Shell Script to ignore # and take corresponding user and group

Hi,

I have a following file:

role.IMPACT_USER.user=admin
role.IMPACT_USER.user=dd12345
role.IMPACT_USER.user=ss76767
#role.IMPACT_USER.user=root
#role.IMPACT_USER.group=System
role.IMPACT_USER.group=ImpactUser
#Description: Allow users to login in to Impact, start and stop service but no creating or editing policies
role.NETCOOL_ADMIN.user=admin
role.IMPACT_USER.user=ss76767
#role.NETCOOL_ADMIN.group=System
role.NETCOOL_ADMIN.group=NetcoolAdmin
#Description: Allow users to login in to Impact, start and stop service and create policies 

role.OPVIEW_USER.user=admin
role.IMPACT_USER.user=rr76767
role.IMPACT_USER.user=ss76767
role.IMPACT_USER.user=dd12345
#role.OPVIEW_USER.group=
role.OPVIEW_USER.group=OpViewUser
#Description: Allow users to login in to Impact, start and stop service and create operator view
 

This file contains lots of # marks.

I want to collect the corresponding user, group and Description (which contains hash mark)

A user may belong to many group so I want to collect those users and make it in the format given below:

Login ID: user
Application Function Title Code: group
Application Function Title Code Description: Description

Any script to start up should be fine.

Thanks,

Please, put an example of the output for the input above!

It is not clear what you want (at least to me!).

Hi

Thanks for your reply.

The output should look like this:

 
Login ID     Appl Func Title Code   Appl Func Title Code Description
admin          ImpactUser            Allow users to login in to <snip>
admin          NetcoolAdmin   Allow users to login in to Impact start<snip> 
admin          OpViewUser          Allow users to login create operator
dd12345       ImpactUser         Allow users to login in to <snip>
ss76767       ImpactUser         Allow users to login in to <snip>
ss76767       OpViewUser        Allow users to login create operator          
ss76767       NetcoolAdmin    Allow users to login in to Impact start<snip>  
rr76767        OpViewUser      Allow users to login create operator<snip>

Thanks,

Shell script method. With hindsight the processing would have been easier by reading the file backwards!
Lose the "cut" for the final version.
Script reads each line (ignoring blank lines and lines starting #role) and saves the value of "group" and saves each user role record until it finds a Description line .. when it outputs one reformatted line for each user role line. These lines are sorted to alphabetic order using "sort" on the whole output of the Shell block.

#!/bin/ksh
workfile=/tmp/`basename $0`.tmp
>${workfile}
#
echo "Login ID     Appl Func Title Code   Appl Func Title Code Description"
(
grep -v \^\#"role" filename.txt | sed -e "/^$/d" | while read line
do
        echo "${line}"|awk -F: '{print $1,$2}' | \
                read description_record description_text
        if [ "${description_record}" = "#Description" ]
        then
                # End of data block processing. Read back role lines
                cat ${workfile} | while read line2
                do
                        echo "${line2}"|awk -F\. '{print $1,$2,$3}' | \
                                read record_type role rest
                        echo "${rest}"|awk -F\= '{print $1,$2}' | \
                                read user_group1 user_group2
                # Display final user record
                echo "${user_group2} ${save_group} ${description_text}" | \
                        cut -c1-70
                done
                #
                >${workfile}
                save_group=""
                continue
        fi
        echo "${line}"|awk -F\. '{print $1,$2,$3}' | \
                read record_type role rest
        echo "${rest}"|awk -F\= '{print $1,$2}' | \
                read user_group1 user_group2  
        #
        # Save group name
        if [ "${user_group1}" = "group" ]
        then
                save_group="${user_group2}"
                continue
        fi
        #
        # Save user role records until we see a Description record
        echo "${line}" >> ${workfile}
done
) | sort
#
rm ${workfile}


Login ID     Appl Func Title Code   Appl Func Title Code Description
admin ImpactUser Allow users to login in to Impact, start and stop ser
admin NetcoolAdmin Allow users to login in to Impact, start and stop s
admin OpViewUser Allow users to login in to Impact, start and stop ser
dd12345 ImpactUser Allow users to login in to Impact, start and stop s
dd12345 OpViewUser Allow users to login in to Impact, start and stop s
rr76767 OpViewUser Allow users to login in to Impact, start and stop s
ss76767 ImpactUser Allow users to login in to Impact, start and stop s
ss76767 NetcoolAdmin Allow users to login in to Impact, start and stop
ss76767 OpViewUser Allow users to login in to Impact, start and stop s

Note: The Descriptions in your example output do not match the Descriptions in the data.

Hi Methyl,

Thanks for your excellent code. I was thinking I will get some startup with not sure how to start did not expect an full answer.

Thanks again

:b:

---------- Post updated at 08:01 PM ---------- Previous update was at 12:59 PM ----------

Hi Methyl,

Also we have another file like below:

########################################################## {COPYRIGHT-TOP-RM} ###
# Licensed Materials - Property of IBM
# "Restricted Materials of IBM"
#
# 5724-S43
# (C) Copyright IBM Corp. 2008, 2009
#
########################################################## {COPYRIGHT-END-RM} ###

###############################################################################
# INSTALL OPTIONS
###############################################################################
# multiple lines: installOption=xx yy zz
#installOption=-nopreCompileJSPs
#installOption=-distributeApp
#installOption=-nouseMetaDataFromBinary
#installOption=-nodeployejb
#installOption=-appname guiserver
#installOption=-createMBeansForResources
#installOption=-noreloadEnabled
#installOption=-nodeployws
#installOption=-validateinstall warn
#installOption=-noprocessEmbeddedConfig
#installOption=-filepermission .*\.dll=755#.*\.so=755#.*\.a=755#.*\.sl=755
#installOption=-noallowDispatchRemoteInclude
#installOption=-noallowServiceRemoteInclude
installOption=-MapRolesToUsers [[ IMPACT_USER AppDeploymentOption.No AppDeploymentOption.No @@role.IMPACT_USER.user@@ @@role.IMPACT_USER.group@@ ] [ NETCOOL_ADMIN AppDeploymentOption.No AppDeploymentOption.No @@role.NETCOOL_ADMIN.user@@ @@role.NETCOOL_ADMIN.group@@ ] [ OPVIEW_USER AppDeploymentOption.No AppDeploymentOption.No @@role.OPVIEW_USER.user@@ @@role.OPVIEW_USER.group@@ ]]
#installOption=-MapWebModToVH [[jmx-gui.war jmx-gui.war,WEB-INF/web.xml default_host] [nsservices nameserver.war,WEB-INF/web.xml default_host] [nci.war nci.war,WEB-INF/web.xml default_host] [netcool-security.war netcool-security.war,WEB-INF/web.xml default_host] [netcool.war netcool.war,WEB-INF/web.xml default_host] [opview.war opview.war,WEB-INF/web.xml default_host] ]

###############################################################################
# ROLE SETTINGS
#
# The format of the options below is role.<rolename>.[user|group], where
# rolename, for the GUI Server, can take on the values of IMPACT_USER,
# NETCOOL_ADMIN, or OPVIEW_USER. These roles must be granted to either users
# or groups managed within VMM (Virtual Member Manager). Please put each
# user/group mapping on its own line.
#
# NOTE: A user must be assigned both the IMPACT_USER and NETCOOL_ADMIN roles
#       in order to access the Impact GUI Server interface 
###############################################################################
role.IMPACT_USER.user=admin
#role.IMPACT_USER.user=root
#role.IMPACT_USER.group=System
role.IMPACT_USER.group=ImpactUser
#Description: Allow users to login in to Impact, start and stop service but no creating or editing policies
role.NETCOOL_ADMIN.user=admin
#role.NETCOOL_ADMIN.group=System
role.NETCOOL_ADMIN.group=NetcoolAdmin
#Description: Allow users to login in to Impact, start and stop service and create policies 
role.OPVIEW_USER.user=admin
#role.OPVIEW_USER.group=
role.OPVIEW_USER.group=OpViewUser
#Description: Allow users to login in to Impact, start and stop service and create operator view
 

How to just get the following output

Login ID     Appl Func Title Code   Appl Func Title Code Description
admin             ImpactUser        Allow users to login in to Impact, start and stop service but no creating or editing policies
admin     NetcoolAdmin      Allow users to login in to Impact, start and stop service and create policies 
admin     OpViewUser  Allow users to login in to Impact, start and stop service and create operator view

Thanks

This is the same format file as before but with an awkward header section.
We can jump the header section with "sed" and start from the first line starting with "role".

Minor change to original code:
sed -n '/^role/,$ p' filename.txt | grep -v \^\#"role" |  .... etc