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!

Flat File String Manipulation

Status
Not open for further replies.

DodgyDavid

Programmer
Nov 28, 2010
3
GB
Hi there,

I have some XML data that I need to work with, that is processed sequentially through a flat file before being manipulated and output in a legacy format for importing on an older system.

In the final phase of the program data must be in a format that has field name on one line followed by all of the data on the next and so on e.g.

Field 1
Data
Field 2
Data etc

What I have encountered is a data stream that looks like this...

<Data>
Data line 1
Data line 2
Data line 3
</Data>

This needs to end up in the format...

<Data>
Data line 1; Data line 2; Data line 3;
</Data>

Because of the nature of the rest of the data in the file, at this point it is not possible to parse the data into arrays, everything must be handled as a global modification.

However I am struggling to find a regexp that will do what I need with the string. Can anyone help?

Regards,

David
 
If you're just trying to process the DATA block value, then just do the translation in multiple steps like the following.
Code:
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$data[/blue] = [red]qq{[/red][purple][/purple]
[purple]    Data line 1[/purple]
[purple]    Data line 2[/purple]
[purple]    Data line 3[/purple]
[purple][/purple][red]}[/red][red];[/red]

[blue]$data[/blue] =~ [red]s/[/red][purple]^(?:[purple][b]\s[/b][/purple]|[purple][b]\n[/b][/purple])*[/purple][red]/[/red][purple][/purple][red]/[/red][red];[/red]    [gray][i]# Remove Starting spacing[/i][/gray]
[blue]$data[/blue] =~ [red]s/[/red][purple][purple][b]\s[/b][/purple]*[purple][b]\n[/b][/purple][purple][b]\s[/b][/purple]*[/purple][red]/[/red][purple]; [/purple][red]/[/red][red]g[/red][red];[/red]    [gray][i]# Translate line breaks into '; '[/i][/gray]
[blue]$data[/blue] =~ [red]s/[/red][purple] $[/purple][red]/[/red][purple][/purple][red]/[/red][red];[/red]             [gray][i]# Remove extraneous space on end[/i][/gray]

[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]'[blue]$data[/blue]'[/purple][red]"[/red][red];[/red]
- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top