Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

To repeat each word in a line

by kulua (Initiate)
on May 23, 2021 at 04:30 UTC ( [id://11132903]=perlquestion: print w/replies, xml ) Need Help??

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

I have a input.txt file which contains

module abc(

rist_top_tck,

rist_top_tdi,

rist_top_tdo,

The output file should be

module abc(

.rist_top_tck(rist_top_tck),

.rist_top_tdi(rist_top_tdi),

.rist_top_tdo(rist_top_tdo));

I search in the internet for perl code . Which repeat each line twice .

repeats=2 while read line; do yes $line|head --lines=$repeats; done < infile > o +utfile

But I I want the above pattern. What should be the modified code. I am new to perl.

Replies are listed 'Best First'.
Re: To repeat each word in a line
by kcott (Archbishop) on May 23, 2021 at 14:29 UTC

    G'day kulua,

    Welcome to the Monastery.

    "I am new to perl."

    As a first step, please read "perlintro: Perl introduction for beginners". It's not particularly long and will provide you with a basic understanding of the language. It's also peppered with links to more detailed information and advanced topics: use those as the need arises.

    Next, bookmark the "Perl Online Documentation". Initially, I'd suggest just familiarising yourself with the basic sections: it's a reference work; there's no expectation that you should read it in its entirety.

    "I search in the internet for perl code . ... What should be the modified code."

    Well, I don't know what search criteria you used, but you've posted what looks like some variety of Bourne shell code.

    Here's some fully-functional Perl code that achieves your stated objectives using the input data you provided:

    #!/usr/bin/env perl use strict; use warnings; use autodie; my $in_file = 'pm_11132903_input.txt'; my $out_file = 'pm_11132903_output.txt'; my $format = ".%s(%s),\n"; { open my $in_fh, '<', $in_file; open my $out_fh, '>', $out_file; while (<$in_fh>) { if (/^(rist_top_\w+)/) { printf $out_fh $format, ($1) x 2; } else { print $out_fh $_; } } } # Just for testing and demonstration print "*** INPUT: $in_file ***\n"; system cat => $in_file; print "*** OUTPUT: $out_file ***\n"; system cat => $out_file;

    Output:

    *** INPUT: pm_11132903_input.txt *** module abc( rist_top_tck, rist_top_tdi, rist_top_tdo, *** OUTPUT: pm_11132903_output.txt *** module abc( .rist_top_tck(rist_top_tck), .rist_top_tdi(rist_top_tdi), .rist_top_tdo(rist_top_tdo),

    — Ken

Re: To repeat each word in a line
by hippo (Bishop) on May 23, 2021 at 09:18 UTC

    This is not "modified code" since the code you have presented is not Perl at all. However here is just one of the many, many ways to achieve this in Perl.

    #!/usr/bin/env perl use strict; use warnings; use Path::Tiny; my @r = path (shift)->lines; s/(.*),$/.$1($1),/ for @r[1..$#r]; $r[-1] =~ s/,$/);/; print @r;

    And if you save it to a file called double.pl here is how it runs:

    $ cat input.txt module abc( rist_top_tck, rist_top_tdi, rist_top_tdo, $ ./double.pl input.txt module abc( .rist_top_tck(rist_top_tck), .rist_top_tdi(rist_top_tdi), .rist_top_tdo(rist_top_tdo)); $

    See the docs for Path::Tiny, s/// and print for more.


    🦛

      Can't locate Path/Tiny.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at abc.pl line 4.
Re: To repeat each word in a line
by AnomalousMonk (Archbishop) on May 23, 2021 at 06:57 UTC
    repeats=2 while read line; do yes $line|head --lines=$repeats; done < infile > o +utfile

    This is not Perl code. It looks like some kind of shell script.

    Here's another approach to the problem that uses a script rather than a one-liner. I'm assuming you know how to handle file input/output, so this is just an example of a regex. This code needs Perl version 5.10 or greater; if you have an earlier version of Perl, a fix can be supplied.

    Win8 Strawberry 5.30.3.1 (64) Sun 05/23/2021 2:40:51 C:\@Work\Perl\monks >perl use 5.010; # need regex extended pattern \K use strict; use warnings; use autodie; my $data = <<'EOD'; module abc( rist_top_tck, rist_top_tdi, rist_top_tdo, EOD open my $fh, '<', \$data; while (my $line = <$fh>) { $line =~ s{ \A (\w+) \K (?= ,) }{($1)}xms; print $line; } close $fh; ^Z module abc( rist_top_tck(rist_top_tck), rist_top_tdi(rist_top_tdi), rist_top_tdo(rist_top_tdo),


    Give a man a fish:  <%-{-{-{-<

Re: To repeat each word in a line
by NetWallah (Canon) on May 23, 2021 at 05:32 UTC
    Here is some working code:
    $ perl -F, -lanE 'm/module/?say:say qq|.$F[0]($F[0])$F[1]|' < input.tx +t module abc( .rist_top_tck(rist_top_tck) .rist_top_tdi(rist_top_tdi) .rist_top_tdo(rist_top_tdo)
    If you would like to learn how/why that works, please read the documentation, and ask specific questions.

                    "The difficult we do today; the impossible takes a little longer."

      How to execute this command from a perl file

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11132903]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2024-04-23 20:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found