Re: [SLUG] text manipulation

From: Matthew Rogers (matt@runithard.com)
Date: Mon Jan 07 2008 - 09:46:37 EST


steve szmidt wrote:
> On Monday 07 January 2008, Eben King wrote:
>
>> Hey scripters:
>>
>> What can I run text through that'll change it from
>>
>> url1
>> filename1
>> url2
>> filename2
>>
>> to
>>
>> url1 filename1
>> url2 filename2
>>
>> Strictly speaking, it doesn't have to be in that format as long as I can
>> use both parts in the same command.
>>
>> I tried sed (specifically the "H" and "x" commands), but it seems my sed-fu
>> is lacking. "paste"?
>>
>
> You could read it into a variable, then parse it. Depending on amount of data
> this works:
>
> You have a file with:
> url1
> file1
> url2
> file2
> url3
> file3
> url4
> file4
>
> The script:
>
> #!/bin/sh
> #
> Data=`cat datalist`
> Count=1
> echo "" > sortedlist
> for URL in $Data ; do
> if [ "$Count" -eq "1" ] ;then
> Line="$URL"
> else
> echo "$Line $URL"
> echo "$Line $URL" >> sortedlist
> Line=""
> Count=0
> fi
> Count=$(($Count+1))
> done
> echo
> echo "Result file:"
> cat sortedlist
>
> The sortedlist:
> url1 file1
> url2 file2
> url3 file3
> url4 file4
>
Perl Version....

#!/usr/bin/perl

open(INFILE,"filename");
my $line="";

while ( <INFILE> ){
    chomp;
   
    if(length($line) gt 0){
         $line = $line . " " . $_ . "\n";
         print $line;
          $line = "";
    } else {
         $line = $_;
    }
   
}
-----------------------------------------------------------------------
This list is provided as an unmoderated internet service by Networked
Knowledge Systems (NKS). Views and opinions expressed in messages
posted are those of the author and do not necessarily reflect the
official policy or position of NKS or any of its employees.



This archive was generated by hypermail 2.1.3 : Fri Aug 01 2014 - 20:12:37 EDT