Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using awk to parse off multiple records from the variable length file 1

Status
Not open for further replies.

amkipnis

Programmer
Apr 15, 2003
21
US
Greetings, could anyone please assist me with a following. I am trying to parse off and reconstruct the input file
that would have a multiple sections separated by "-------------".
I would like to extract two parts of each section. First is value of the records that starts with "Name:".
Second section is a more complex since I need to be able to extract a list of all role values under the "Roles:" section.
Please note, that the number of these roles can vary from section to section.

Thank you in advance.

Here is a fragment of the input file:

-------------
Name: 112233
ID: 95
E-mail address: (None)
E-mail form: Linked
E-mail notification type: Server
Home folder: (None)
Default printer: (None)
Max. priority: 500
Viewing preference: Default
Max. completed notices: 0
Success notification expr.: No limit
Failure notification expr.: No limit
Server notification type: All
Default file user privileges:
(None)
Default file role privileges:
(None)
Roles:
Role1
Role2
Role3
Groups:
(None)
Channels:
(None)
--------------
Name: 445566
ID: 95
E-mail address: (None)
E-mail form: Linked
E-mail notification type: Server
Home folder: (None)
Default printer: (None)
Max. priority: 500
Viewing preference: Default
Max. completed notices: 0
Success notification expr.: No limit
Failure notification expr.: No limit
Server notification type: All
Default file user privileges:
(None)
Default file role privileges:
(None)
Roles:
Role4
Role5
Role6
Role7
Role8
Groups:
(None)
Channels:
(None)
--------------


The output should look like this:
ADDUSER 112233
ADDUIG Role1 112233
ADDUIG Role2 112233
ADDUIG Role3 112233
ADDUSER 445566
ADDUIG Role4 445566
ADDUIG Role5 445566
ADDUIG Role6 445566
ADDUIG Role7 445566
ADDUIG Role8 445566
 
A starting point:
awk '
$1=="Name:"{n=$2;print "ADDUSER "n}
/Roles:/,/Groups:/{if($1!~":$")print "ADDUIG "$1" "n}
' /path/to/input > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
PHV, thank you very much. I got it working on my end. I had to use nawk, instead of awk. Otherwise, I was receiving an error:
awk: syntax error near line 1
awk: illegal statement near line 1

Thanks again.
 
PHV, could you please assist me with another filtration. Would I exclude the "All" value in Roles: section by using your command with a minor modification:
nawk '
$1=="Name:"{n=$2;print "ADDUSER "n}
/Roles:/,/Groups:/{if($1!~":$" && $1!~"All")print "ADDUIG "$1" "n}
'/path/to/input > output


Thanks again.
 
Or this:
if($1!~":$" && $1!="All")

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
[tt]
# Trim leading and trailing blanks.
{ gsub( /^ +| +$/, "" ) }

/^Name:/ { name=$2; print "ADDUSER " name }

/^Roles:/ { r++; next }
/^Groups:/ { r=0 }

r && !/^All$/ { print "ADDUIG " $1, name }
[/tt]
 
Code:
# Trim leading and trailing blanks.
{ gsub( /^ +| +$/, "" ) }

/^Name:/ { name=$2; print "ADDUSER " name }

/^Groups:/ { r=0 }

r && !/^All$/ { print "ADDUIG " $1, name }

/^Roles:/ { r++ }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top