nixCraft Linux Forum

nixCraft

Linux / UNIX Tech Support Forum

Splitting strings on complex criteria

This is a discussion on Splitting strings on complex criteria within the Coding in General forums, part of the Development/Scripting category; I am having a 'bad brain' day... What I am trying to do is parse a string and in a ...


Go Back   nixCraft Linux Forum > Development/Scripting > Coding in General

Linux answers from nixCraft.


Coding in General Discussion on PHP/Perl/Python/Ruby/GNU C or C++. MySQL, PgSQL and (X)HTML or any other programming languages you want.

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-11-2008, 02:04 PM
DragonMaster's Avatar
Junior Member
User
 
Join Date: Oct 2008
OS: Debian, Ubuntu Kubuntu
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
DragonMaster is on a distinguished road
Default Splitting strings on complex criteria

I am having a 'bad brain' day... What I am trying to do is parse a string and in a situation where I have four spaces followed by an 8 or 10 digit number, I want to replace the spaces with a 'pipe' character, but NOT in a situation where there are 4 spaces and no number...

I have used :
Code:
$SampleData = preg_replace("/(\s{4})/", "|", $Data);
successfully - right up to the point where I have the 4 spaces and no number scenario, but by my logic
Code:
$SampleData = preg_replace("/(\s{4})(\d{8,1})/", "|", $Data);
will not preserve the number.

It all sounds complicated, but I am looking to parse a string containing epoch timestamp and variable length data and separate into an array split on the epoch (or spaces) e.g.
Code:
1182169673  jdegr  Affected Services set to DIGITAL  Number of Customers Affected set to 326  Assignee Group set to Dispatch  Priority set to 1  Ticket Status set to Assigned  Outage Start set to 18/06/2007 13:23:04  Region set to UK  Short Description set to Loss Equipt - B715  0*.   B716 0*    1182169679  jdegr  Target Fix Time set to 18/06/2007 17:23:04    1182169715  p2026145  High Priority Owner set to BAR, Foo    1182169764  J2026913  Assignee Name set to BLOGGS, Joe    1182170416  r1006992  Ticket Status set to Accepted  Target Fix Time set to 18/06/2007 17:23:04    1182177812  s2024123  Fix Details set to Replaced PSU service now retored  Ticket Status set to Restored  Outage End set to 18/06/2007 15:40:58    1182177893  s2024123  Actual Fix Date/Time set to 18/06/2007 15:41:00  Finding Code set to RP_POWER_SUPPLY_UNIT_(PSU)  Reason Code set to W1_EQUIP_FAIL_NORMAL_END_OF_LIFE  Solution Code set to REPLACED    1182177909  s2024123  Fix Details set to Replaced PSU service now restored    1182177922  s2024123  Assignee Group set to 2nd Line  Ticket Status set to Fixed    1182177922  s2024123  Assignee Group set to Holding Queue  Ticket Status set to Pending Callbacks    1182240543  b2019505  Assignee Group set to 2nd Line  Ticket Status set to Fixed    1182240647  p2026145  Ticket Status set to Closed
is parsed to form:
Code:
Array
(
    [0] => 1182169673  jdegr  Affected Services set to DIGITAL  Number of Customers Affected set to 326  Assignee Group set to Dispatch  Priority set to 1  Ticket Status set to Assigned  Outage Start set to 18/06/2007 13:23:04  Region set to UK  Short Description set to Loss Equipt - B715  0*.   B716 0*
    [1] => 1182169679  jdegr  Target Fix Time set to 18/06/2007 17:23:04
    [2] => 1182169715  p2026145  High Priority Owner set to BAR, Foo
    [3] => 1182169764  J2026913  Assignee Name set to BLOGS, Joe
    [4] => 1182170416  r1006992  Ticket Status set to Accepted  Target Fix Time set to 18/06/2007 17:23:04
    [5] => 1182177812  s2024123  Fix Details set to Replaced PSU service now retored  Ticket Status set to Restored  Outage End set to 18/06/2007 15:40:58
    [6] => 1182177893  s2024123  Actual Fix Date/Time set to 18/06/2007 15:41:00  Finding Code set to RP_POWER_SUPPLY_UNIT_(PSU)  Reason Code set to EQUIP_FAIL_NORMAL_END_OF_LIFE  Solution Code set to REPLACED
    [7] => 1182177909  s2024123  Fix Details set to Replaced PSU service now restored
    [8] => 1182177922  s2024123  Assignee Group set to 2nd Line  Ticket Status set to Fixed
    [9] => 1182177922  s2024123  Assignee Group set to Holding Queue  Ticket Status set to Pending Callbacks
    [10] => 1182240543  b2019505  Assignee Group set to 2nd Line   Ticket Status set to Fixed
    [11] => 1182240647  p2026145  Ticket Status set to Closed
)
Hope this makes sense, and I guess there is a very simple solution that I am missing...
Reply With Quote
  #2 (permalink)  
Old 05-11-2008, 08:47 PM
DragonMaster's Avatar
Junior Member
User
 
Join Date: Oct 2008
OS: Debian, Ubuntu Kubuntu
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
DragonMaster is on a distinguished road
Default

OK, I think I may have cracked it..
Code:
  $FourSpacesPlus = preg_match_all("/(\s{4})(\d{8,10})/", $Data, $temp);
  for ($i=0; $i < count($temp[2]); $i++) {
    $Data = preg_replace("/".$temp[0][$i]."/", "|".$temp[2][$i], $Data, -1, $Count);# replace the spaces & timestamp with "|" plus the times
tamp.
  }
This will then identify all the entries that need to be processed, and store them in $temp. Then running a replace on element 0 of the temp array (which holds the full string) and replacing it with the new delimiter followed by element 2 of the array (which is only the latter part of the string). If anyone has a better suggestion, please let me know.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
How can I awk, grep, sed, etc. the real and Im parts of a complex number?!? satyavan Shell scripting 0 06-05-2008 09:38 PM


All times are GMT +5.5. The time now is 06:47 AM.


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38