Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Modifying text file

by torres09 (Acolyte)
on Jun 05, 2013 at 12:43 UTC ( #1037197=perlquestion: print w/replies, xml ) Need Help??
torres09 has asked for the wisdom of the Perl Monks concerning the following question:


I have a text file , so I want to convert that text file such that each word has a comma following it. please help

Replies are listed 'Best First'.
Re: Modifying text file
by toolic (Bishop) on Jun 05, 2013 at 12:52 UTC
    Here is one way to get started using s///
    use warnings; use strict; while (<DATA>) { s/(\w+)/$1,/g; print; } __DATA__ This is a word. Here is another sentence.


    This, is, a, word,. Here, is, another, sentence,.

    See also perlintro

      hey I have extracted my text file into an array by splitting it , so can I put it in a new array with each word terminated by comma , If we can plz tell me <\p>

        Show the code that you have so far. Then we can make specific recommendations.

      the problem I am having is say I have entry as -9.10 it will split it as -9,10 so it breaks one entity into two , but I want it to be a ......,-9.10,..... . so can you please help out

Re: Modifying text file
by hdb (Monsignor) on Jun 05, 2013 at 13:12 UTC

    I am smelling an XY Problem. So here is some code that takes $text and creates an array @words containing all the words in $text. In addition, it adds a comma to each word, but my guess is, that you are not really interested in that...

    use strict; use warnings; my $text = <<EOTEXT; This is a word. Here is another sentence. EOTEXT my @words = $text =~ /\b(\w+)\b/g; print "@words\n"; $_.="," for @words; print "@words\n";

    Thanks to toolic for the sample text.


      I actually want it , i.e. I want that each word in the text file should be in an array , with a comma in between .Even the spaces

        Apologies if you perceived my answer as being patronizing.

        If you want to split your text into words AND keep the spaces, split might be an alternative. Using a capture group in the regex in split, the delimiters are not discarded. So splitting on words will give you the words and the characters inbetween:

        use strict; use warnings; use Data::Dumper; my $text = <<EOTEXT; This is a word. Here is another sentence. EOTEXT my @words = split /\b(\w+)\b/, $text; print Dumper \@words;

        results in

        $VAR1 = [ '', 'This', ' ', 'is', ' ', 'a', ' ', 'word', '. ', 'Here', ' ', 'is', ' ', 'another', ' ', 'sentence', '. ' ];
Re: Modifying text file
by 2teez (Vicar) on Jun 05, 2013 at 13:07 UTC

    Hi torres09,
    Welcome to the Monastery.
    Though you have been given a head up, I think it worth the note to check this How do I post a question effectively?.
    And in addition to what has been given, you could also think of use the module Text::CSV or Text::CSV_XS

    Update: Code to illustrate...

    use warnings; use strict; use Text::CSV_XS; my $csv = Text::CSV_XS->new( { binary => 1, auto_diag => 1 } ); while (<DATA>) { for ( [split] ) { $csv->print( \*STDOUT, $_ ); print $/; } } __DATA__ Mary had a little lamb whose fleece was white as snow.

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: Modifying text file - I like PIE :-)
by space_monk (Chaplain) on Jun 05, 2013 at 13:23 UTC
    Something like:
    perl -pie 's/(\w+)/$1,/g' yourFile
    If you spot any bugs in my solutions, it's because I've deliberately left them in as an exercise for the reader! :-)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1037197]
Approved by toolic
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2018-06-21 15:16 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (118 votes). Check out past polls.