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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formatting repeating block of texts

Status
Not open for further replies.

viadisky

Technical User
Jun 19, 2003
110
GB
Hi,

I have this data to work with:

<ConfigPolicyVO>
<ConfigPolicyID>61</ConfigPolicyID>
<ConfigPolicyName>access-list 99</ConfigPolicyName>
<Description>access-list 99</Description>
<Comments></Comments>
<Status>1</Status>
<CreateDate>2006-02-23 17:10:47:000</CreateDate>
<LastModifiedDate>2006-02-27 16:12:50:000</LastModifiedDate>
<LastModifiedUserID>1</LastModifiedUserID>
<InUse>1</InUse>
</ConfigPolicyVO>

<ConfigPolicyVO>
<ConfigPolicyID>35</ConfigPolicyID>
<ConfigPolicyName>Cisco Interfaces Up Down</ConfigPolicyName>
<Description>Cisco Interface Up Down</Description>
<Comments>Cisco Interface Up Down</Comments>
<Status>1</Status>
<CreateDate>2005-12-14 12:03:18:000</CreateDate>
<LastModifiedDate>2006-02-27 16:14:41:000</LastModifiedDate>
<LastModifiedUserID>1</LastModifiedUserID>
<InUse>1</InUse>
</ConfigPolicyVO>

How easy it is to format it this way?

ConfigPolicyName,Description,Status
access-list 99,access-list 99,1
Cisco Interfaces Up Down,Cisco Interface Up Down,1

Please help :)

Cheers!
 
Hi

Excepting the heading line :
Code:
awk -F '[<>]+' '/ConfigPolicyName|Description|Status/{e[$2]=$3}/^$/{print e["ConfigPolicyName"] "," e["Description"] "," e["Status"]}' /input/file

Feherke.
 
Hi Feherke,

I tried to run it but the output only contains the first block of data:

awk -F '[<>]+' '/ConfigPolicyName|Description|Status/{e[$2]=$3}/^$/{print e["ConfigPolicyName"] "," e["Description"] "," e["Status"]}' test.txt

access-list 99,access-list 99,1

Cheers,
Maria
 
Hi

Uff. That is because the [tt]print[/tt] is executed on empty lines, which I considered section separator. And in your file there is no newline character at the end.

Try this, is much more correct :
Code:
awk -F '[<>]+' '/ConfigPolicyName|Description|Status/{e[$2]=$3}/\/ConfigPolicyVO/{print e["ConfigPolicyName"] "," e["Description"] "," e["Status"]}' test.txt

Feherke.
 
Hi Feherke,

Thanks, it worked!

How can I adjust the script to accomodate additional data, I'm trying to understand the way you put everything together and I manage to change "Status" entries with "Comments". The problem is when i tried to add Status again (with existing Comments field), it isn't working.

Many thanks for the time you spent helping me.

Best regards,
Maria
 
Hi

Now that is a good question. I set the field separator to one or more characters of [tt]<[/tt] and [tt]>[/tt]. This will mess up the things in case of empty fields like the Comments in the first section, because the ending /Comments tag will shift one place left becoming field $2. So change the field separator to just on character, by removing the plus ( + ) sign :
Code:
awk -F '[<>]' '[gray]...[/gray]

But I have no idea what could be the problem when you put the Status back. The above problem should appear before that. Give more details about the problem.

Feherke.
 
Hi Feherke,

Removing the plus (+) sign made the script more flexible :)

Thanks a lot! Hope you will continue helping beginners like me :)

Best regards,
Maria
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top