Greenguy's Board

Greenguy's Board (http://www.greenguysboard.com/board/index.php)
-   General Business Knowledge (http://www.greenguysboard.com/board/forumdisplay.php?f=10)
-   -   Does Anyone know how to rearrage a text file (http://www.greenguysboard.com/board/showthread.php?t=51454)

Tommy 2009-02-13 11:21 AM

Does Anyone know how to rearrage a text file
 
Like for instance you have


URL|TITLE|DECRIPTION

I wanna rearrange it so its
TITLE|URL|DECRIPTION

NY Jester 2009-02-13 11:51 AM

Tommy, Im experiencing the same thing, so I'd be interested in the answer to this as well.

I have it with just two variables TITLE|URL and need it reversed to URL|TITLE and a simple find / replace will not work

Beaver Bob 2009-02-13 12:04 PM

You could do it with a small script. Open the contents of the file and put it into a variable. explode() into an array for each carriage return.. and then for each element in that array, explode() on the pipe symbol, and then just rewrite the data to a new file.

bDok 2009-02-13 12:15 PM

Quote:

Originally Posted by Beaver Bob (Post 440835)
You could do it with a small script. Open the contents of the file and put it into a variable. explode() into an array for each carriage return.. and then for each element in that array, explode() on the pipe symbol, and then just rewrite the data to a new file.

exactly... I have to run right now to get the car serviced. If nobody has done the code by the time I get back I'll write something real quick.

cd34 2009-02-13 12:35 PM

upload the following and your file to be converted as filename.txt:

Code:

  $contents = file_get_contents("filename.txt");
  $line_by_line = explode("\n", $contents);
  array_pop($line_by_line); // get rid of the empty line at the end that explode creates
  $results = array();

  foreach ($line_by_line as $line) {
    list ($url, $title, $description) = explode("|", $line);
    print "$title|$url|$description\n";
  }
?>

The php version will output the result from your screen so that you can cut and paste it into whatever.

alternatively if you have ssh access, you could do:

Code:

awk < filename.txt -F\| '{ print $2 "|" $1 "|" $3 }' > outfile.txt
and end up with the result in outfile.txt which could be downloaded.

Tommy 2009-02-13 01:01 PM

Thank you CD

i put that code in my the top of my txt file ??

Beaver Bob 2009-02-13 01:08 PM

Quote:

Originally Posted by Tommy (Post 440853)
Thank you CD

i put that code in my the top of my txt file ??

no, you rename your text file to filename.txt and upload what CD wrote into a seperate php file. upload them both to the same folder and run the php file.

Tommy 2009-02-13 01:11 PM

wow... i just did... CD 34 I fucking Love you !!!

thank you Bob !!

Simon 2009-02-13 01:20 PM

Tommy -- also, if you use a text editor with 'Grep' search and replace capabilities you can do it easily.

search string would be:

(.*)\|(.*)\|(.*)\r

Replace with:

\2|\1|\3\r

and 'replace all'


BBEdit on the Mac has had that kind of search built in for years.
Not sure what the comparable Windows text editor would be, but the search and replace string would be the same in any editor that can do regex (regular expression) searching.

HTH

NY Jester 2009-02-13 01:29 PM

Kind of new at the php thng, so if create the file rearrange .php using the code above along with the txt file that has the lines I want to change then just access rearrange. php and it will out put on the screen the rearrange strings?

Thanks in advance.

Beaver Bob 2009-02-13 02:40 PM

Quote:

Originally Posted by NY Jester (Post 440858)
Kind of new at the php thng, so if create the file rearrange .php using the code above along with the txt file that has the lines I want to change then just access rearrange. php and it will out put on the screen the rearrange strings?

Thanks in advance.

Yes.

Or you could output it all to a new txt file by making a few modifcations
PHP Code:

<?php
  $contents 
file_get_contents("filename.txt");
  
$line_by_line explode("\n"$contents);
  
array_pop($line_by_line); // get rid of the empty line at the end that explode creates
  
$results = array();

  
$new_contents '';
  foreach (
$line_by_line as $line) {
    list (
$url$title$description) = explode("|"$line);
    
$new_contents .= "$title|$url|$description\n";
  }

  
$file fopen("new-filename.txt");
  
fwrite($file$new_contents);
  
fclose($file);
?>


tickler 2009-02-13 02:59 PM

You could always use Excel and cut/paste columns.

NY Jester 2009-02-13 03:42 PM

Thanks Bob and CD, I'll be working on this over the weekend Ill post my results LOL wish me luck. haha

plateman 2009-02-13 05:06 PM

off topic here but text magician can save a bunch of time when making text files for importing galls into tgp scripts

like adding custom stuff that a lot of sponsor text file tools won't add

dunc 2009-02-13 08:29 PM

Sort of related - I have a bunch of stories which I want to use - but all the lines are double spaced :(

Is there a way of clearing all empty lines using php...

Beaver Bob 2009-02-13 08:38 PM

Quote:

Originally Posted by dunc (Post 440900)
Sort of related - I have a bunch of stories which I want to use - but all the lines are double spaced :(

Is there a way of clearing all empty lines using php...

try this

PHP Code:

<?php
  $contents 
file_get_contents("filename.txt");
  
$line_by_line explode("\n"$contents);
  
array_pop($line_by_line); // get rid of the empty line at the end that explode creates
  
$results = array();

 
$counter 0;
  foreach (
$line_by_line as $line) {
    if(
$counter == 0) {
       print 
"$line\n";
    }
    
$counter++;
  }

?>


MeatPounder 2009-02-13 10:13 PM

Quote:

Originally Posted by Simon (Post 440856)
Tommy -- also, if you use a text editor with 'Grep' search and replace capabilities you can do it easily.

search string would be:

(.*)\|(.*)\|(.*)\r

Replace with:

\2|\1|\3\r

and 'replace all'


BBEdit on the Mac has had that kind of search built in for years.
Not sure what the comparable Windows text editor would be, but the search and replace string would be the same in any editor that can do regex (regular expression) searching.

HTH

Find and replace all in notepad?

cd34 2009-02-13 10:14 PM

Code:

if (trim($line) != "") {
// rest of stuff
}

would eliminate blank lines assuming that the file MIGHT not have every other line as blank.

dunc 2009-02-13 10:30 PM

Thanks Bob - perfect :)

Beaver Bob 2009-02-13 10:33 PM

Quote:

Originally Posted by dunc (Post 440905)
Thanks Bob - perfect :)

actually cd34's is the better way, mine was going under the assumption that every other line was blank.. his checks if the line is blank or not.

theres usually millions of ways you can accompilsh the same thing with programming.

Mr Spock 2009-02-13 11:31 PM

Quote:

Originally Posted by tickler (Post 440864)
You could always use Excel and cut/paste columns.

Easiest way , especially if you are not a code head |jester|

dunc 2009-02-13 11:56 PM

Well even more thanks to cd34 (I didn't even see that you posted before my reply :))

I don't seem to be getting thread reply notifications anymore :( - I'll check my options

Simon 2009-02-14 06:21 AM

Quote:

Sort of related - I have a bunch of stories which I want to use - but all the lines are double spaced
Is there a way of clearing all empty lines using php...
As an alternative, using regex in a text editor you can do this...

Search for: \r\r
Replace with: \r

Finds two carriage returns in a row and replaces them with one carriage return.

If I'm searching for *nix line feeds instead of carriage returns, I use \n instead of \r in the search and replace.

Again, I'm not sure which Windows editors do these kinds of searches, but BBEdit on the Mac lets you run these kinds of searches on a whole folder full of text files and process them as a batch right from the text editor.

If you don't need to preserve the order of the lines, one other option would be to sort the text file, which would put all the blank lines together so you can delete them. Then cleaned text file could be opened in Excel or other spreadsheet program where the lines can be sorted randomly again if you need to do that before importing to somewhere.

secretagentwilly 2009-02-14 09:50 AM

Quote:

Originally Posted by Mr Spock (Post 440913)
Easiest way , especially if you are not a code head |jester|

I use excel for this...If I have a url|title|description and I need them rearranged, I first paste in a text editor like textpad or dreamweaver and do a find & replace for | with a tab. Doing this will make it so there's enough space between each variable that excel will put each in it's own column...that's just my way...but this php stuff looks great...complicated, but great...

JackDaniel's 2009-02-14 10:16 AM

This is really helpful ! Thanks guys ;)


All times are GMT -4. The time now is 12:14 AM.

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
© Greenguy Marketing Inc