Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Text manipulation with perl

by jriggs420 (Sexton)
on Sep 29, 2005 at 21:04 UTC ( [id://496246] : perlquestion . print w/replies, xml ) Need Help??

jriggs420 has asked for the wisdom of the Perl Monks concerning the following question:

Hi all- I am new to perl and am trying to use it modify the text in a file. I have a good understanding of the fundamentals but am still struggling with pattern matching and substitution. Here is my problem:

I have a file with the text-
- Some words here
- some other words on this line
- you get the idea
I need to remove the '-' from each line and have the output look like this:
'SOME WORDS HERE'/
'SOME OTHER WORDS ON THIS LINE'/
'YOU GET THE IDEA'
(no forward slash on last line and each line starts a new line) for up to 30 lines. Any help with any part of this problem would be great. Thanks in advance for any suggestions -Joe

Replies are listed 'Best First'.
Re: Text manipulation with perl
by GrandFather (Saint) on Sep 29, 2005 at 21:18 UTC

    We would really really like to see that you have a good understanding of the fundamentals so that we know where you are having trouble. How about posting some sample code for us so that we can see where you are coming from and where you are having difficulty.

    Note that you should use code tags to get the sort of result shown below rather than breaks. See Writeup Formatting Tips to see how things get done.

    Here's a sample of how your sample code should be presented. You, of course, replace the comment lines with a first attempt at the code.

    use warnings; use strict; # my attempt at reading the data using <DATA> and printing # the output as I want it __DATA__ - Some words here - some other words on this line - you get the idea # Result should look like this: 'SOME WORDS HERE'/ 'SOME OTHER WORDS ON THIS LINE'/ 'YOU GET THE IDEA'

    Perl is Huffman encoded by design.
Re: Text manipulation with perl
by dimar (Curate) on Sep 29, 2005 at 22:28 UTC

    Well, since you said you are new to perl, the following gives you the benefit of the doubt, that you are at least willing to make a good faith effort to teach yourself, and not have the entire solution done for you by someone else.

    This should give you a good head start. If you can dissect this and add the missing pieces yourself, it shows you're putting in some effort. If not, it smells like someone asking for free homework.

    ### begin_: init perl use strict; use warnings; use YAML; ### begin_: init vars my $sYAML; my $oData; my $iM; ### begin_: main $sYAML = join"",(<DATA>); $oData = YAML::Load($sYAML."\n\n"); $iM = ((scalar(@{$oData}))-1<29)?scalar(@{$oData})-1:29; print join "/\n",map{sprintf"'%s'",uc($_);}@{$oData}[0..$iM] +; ### begin_: end perl 1; __DATA__ - Some words here - some other words on this line - you get the idea

    ...

    =oQDlNWYsBHI5JXZ2VGIulGIlJXYgQkUPxEIlhGdgY2bgMXZ5VGIlhGV
Re: Text manipulation with perl
by johndageek (Hermit) on Sep 29, 2005 at 21:13 UTC
    What have you tried? Please post your code.

    Enjoy!
    Dageek

      in reply to Text manipulation with perl WOW, I am suprised so many have taken interest in my little problem, as requested here is where I am at right now with the code:
      open(TXT,"$ARGV[0]") || die "Cannot open the $ARGV[0] file"; $instr = do{local $/; <TXT> }; close(TXT); chomp($instr); $instr =~ s/\n+/\n/g; $instr =~ s/\-\s+([^\n]+)/\'$1\'\//g; chop($instr); open(TXT,">$ARGV[1]") || die "Cannot open the $ARGV[1] file"; print TXT $instr; close(TXT);
      Any suggestions? P.S. I didn't do this all by myself, but I at least understand what is going on.
Re: Text manipulation with perl
by InfiniteSilence (Curate) on Sep 29, 2005 at 21:19 UTC
    Rather than open the file:

    perl -p -i.bak -e "s/^-//g" foo.dat

    Update: Forgot that you wanted to uppercase stuff:

    perl -p -i.bak -e "s/^-(.*)/uc($1)/e" foo.dat

    Update #2: You might also try Mastering Regular Expressions

    Celebrate Intellectual Diversity

Re: Text manipulation with perl
by ickyb0d (Monk) on Sep 29, 2005 at 21:15 UTC
    I'm not the best with RegEx's but this would probably work
    foreach my $line(@all_lines) { $line =~ s/-//g }
    or if you only want the one at the beginning of the line
    $line =~ s/^-//g
Re: Text manipulation with perl
by jriggs420 (Sexton) on Sep 30, 2005 at 13:32 UTC
    WOW, I am suprised so many have taken interest in my little problem, as requested here is where I am at right now with the code:
    open(TXT,"$ARGV[0]") || die "Cannot open the $ARGV[0] file"; $instr = do{local $/; <TXT> }; close(TXT); chomp($instr); $instr =~ s/\n+/\n/g; $instr =~ s/\-\s+([^\n]+)/\'$1\'\//g; chop($instr); open(TXT,">$ARGV[1]") || die "Cannot open the $ARGV[1] file"; print TXT $instr; close(TXT);
    Any suggestions? P.S. I didn't do this all by myself, but I at least understand what is going on.