Results 1 to 14 of 14
  1. #1
    Newbie
    Join Date
    January 18th, 2005
    Posts
    46
    RegEx Help
    Can someone help with regex for replacing text in a text file as under:

    First Column > Second Column > Third Column

    I need to remove: First Column >

    Thanks.

  2. #2
    Affiliate Manager adambha's Avatar
    Join Date
    October 20th, 2006
    Posts
    301
    Quote Originally Posted by ssgupta
    Can someone help with regex for replacing text in a text file...
    Well, I'm not a regex guru, but I can hack around text from the command line. This will do what you're looking for:

    Code:
    cat myfilename.txt | awk -F\ \> '{print $2,$3}' | sed -e s/^\ //

  3. #3
    Moderator MichaelColey's Avatar
    Join Date
    January 18th, 2005
    Location
    Mansfield, TX
    Posts
    16,232
    What you probably want is something like this:

    s/^.*? *> *//

    "^" anchors the search to the left column
    ".*?" matches the minimal text necessary that still matches the rest
    " *" matches 0 or more spaces before the >
    ">" matches the ">"
    " *" matches 0 or more spaces after the >
    Michael Coley
    Amazing-Bargains.com
     Affiliate Tips | Merchant Best Practices | Affiliate Friendly? | Couponing | CPA Networks? | ABW Tips | Activating Affiliates
    "Education is the most powerful weapon which you can use to change the world." Nelson Mandela

  4. #4
    Newbie
    Join Date
    January 18th, 2005
    Posts
    46
    Many thanks for the posts. I, however, couldn't make them work - probably my lack of understanding. I will try to rephrase my question:

    I have a datafeed in which categories appear like this:

    First Column > Second Column > Third Column

    The Second Column is a repetition of the First Column; when imported in the database the category names are shown twice. So I need to remove "First Column >" using a text editor or Excel.

    For example, in Excel -

    Find this: *>
    Replace with: [blank]

    removes both: First Column > Second Column >

    But I need to remove only the: First Column >

    The three "columns" are in a single row/field:
    NBA Tickets > NBA Tickets > Phoenix Suns Tickets

  5. #5
    Believe knight01's Avatar
    Join Date
    August 14th, 2006
    Location
    Dayton, Ohio
    Posts
    1,815
    Quote Originally Posted by ssgupta
    So I need to remove "First Column >" using a text editor or Excel.

    For example, in Excel -

    Find this: *>
    Replace with: [blank]

    removes both: First Column > Second Column >

    But I need to remove only the: First Column >

    The three "columns" are in a single row/field:
    NBA Tickets > NBA Tickets > Phoenix Suns Tickets

    In excel, can't you just delete the entire column? No need to find/replace anything since it's a duplicate of column 2.

    Click the letter A at the top of the column and the first column will be highlighted, then click delete. 2nd column moves to the first column spot... all done.


    Michael,
    That looks like some nice .htaccess rewriting... I'll have to hit you up sometime for some tips. I constantly struggle with doing those.
    Someday starts today
    Military Discounts

  6. #6
    Newbie
    Join Date
    January 18th, 2005
    Posts
    46
    Can't delete by simply deleting columns A, B. That would delete everything, even the part I wish to keep. The data is in single column.

  7. #7
    Member spawnua's Avatar
    Join Date
    January 18th, 2005
    Location
    Odessa, Ukraine
    Posts
    91
    Post
    This PHP code works for me:
    $string = "First Column > Second Column > Third Column";
    echo trim(preg_replace("/(.*)>(.*)>(.*)/", "\\2>\\3", $string));
    Last edited by spawnua; December 14th, 2007 at 05:51 AM.

  8. #8
    Newbie
    Join Date
    January 18th, 2005
    Posts
    46
    So how do I use it for manipultaing a (simple) text file?

  9. #9
    Member spawnua's Avatar
    Join Date
    January 18th, 2005
    Location
    Odessa, Ukraine
    Posts
    91
    Very simple example:

    Code:
    <?php
    
    $fcategory = 1; // 'Category' field number
    $filename = "simple.txt"; // tab-delimited text file
    $filename_out = "output.txt"; // output file name
    $delim = "\t"; // delimiter
    
    $lines = file($filename); // load file content in array
    
    // for each text line
    foreach ($lines as $line) { 
    	$fields = explode($delim, $line); // parse data
    	$fields[$fcategory] = trim(preg_replace("/(.*)>(.*)>(.*)/", "\\2>\\3", $fields[$fcategory])); // updating category name
    	$out[] = trim(implode($delim, $fields)); // add to output array
    }
    
    // open output file for writing
    if (!$handle = fopen($filename_out, "w")) {
    	print "Cannot open file ($filename_out)";
    	exit;
    }
    
    // writing data to output file
    if (!fwrite($handle, implode("\n", $out))) {
    	print "Cannot write to file ($filename_out)";
    	exit;
    }
    
    fclose($handle);
    
    echo "Done.";
    
    ?>

  10. #10
    ABW Ambassador Snib's Avatar
    Join Date
    January 18th, 2005
    Location
    Virginia
    Posts
    5,303
    Here's a simple solution to understand:

    Code:
    list($ignore, $item1, $item2) = split(" > ", $string);
    $new_string = "$item1 > $item2";
    - Scott
    Hatred stirs up strife, But love covers all transgressions.

  11. #11
    Newbie
    Join Date
    January 18th, 2005
    Posts
    46
    Many many thanks to everyone who is trying to help. But I don't need PHP code. I need to be able to edit the text file using, say, Notepad, WordPad or Excel - using find/search and replace function.

  12. #12
    ABW Ambassador Snib's Avatar
    Join Date
    January 18th, 2005
    Location
    Virginia
    Posts
    5,303
    Quote Originally Posted by ssgupta
    Many many thanks to everyone who is trying to help. But I don't need PHP code. I need to be able to edit the text file using, say, Notepad, WordPad or Excel - using find/search and replace function.
    Should really consider writing a program to do that for you. Otherwise you'll have to manually do this every time you need to update your datafeeds.

    - Scott
    Hatred stirs up strife, But love covers all transgressions.

  13. #13
    ABW Ambassador 2busy's Avatar
    Join Date
    January 17th, 2005
    Location
    Tropical Mountaintop
    Posts
    5,636
    Try PSPad if you want an app. It's at http://www.pspad.com/en/ and it's free. Under Edit>Lines Manipulation there are choices that would be able to remove characters from the start of all lines.

  14. #14
    Full Member
    Join Date
    January 18th, 2005
    Posts
    396
    Can you import your data into Excel using '>' as the delimiter between columns and once data is in three columns, delete the first column.

  15. Newsletter Signup

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •