logo
down
shadow

How do I replace a random line in a file without copying it into another file using Perl


How do I replace a random line in a file without copying it into another file using Perl

Content Index :

How do I replace a random line in a file without copying it into another file using Perl
Tag : perl , By : Thx1138.6
Date : November 29 2020, 01:01 AM


Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Perl: How do I remove the first line of a file without reading and copying whole file


Tag : perl , By : PaulPlum
Date : March 29 2020, 07:55 AM
may help you . I do have a whole bunch of files in a directory and from every file I want to remove the first line (including carriage return). I can read the whole file into an array of strings and write all but the first element to a new file, but that looks a bit cumbersome to me are there better ways? Oh the prefered language is Perl. , Try this one liner
perl -pi -e '$_ = "" if ( $. == 1 );' filename

Copying a string(passed as command line arguments to Perl) into text file


Tag : perl , By : mlapida
Date : March 29 2020, 07:55 AM
I wish this helpful for you If you want a white space separated argument treated as a single argument, with most programs, you need to surround it with " "
e.g run_cmd.pl "abc xyz def" filename
p.StartInfo.Arguments = "c:\\root\\run_cmd.pl \"" + str + "\" " + text_file;

How do I replace a line in a file using Perl?


Tag : perl , By : Ken
Date : March 29 2020, 07:55 AM
hop of those help? The only actual problem with your code was that you were printing the line to the new file in the wrong place in the loop. You need to print every line from the old file into the new file.
Having tidied your file a little and updated some of the idioms, I end up with this:
#!/usr/bin/perl

use strict;
use warnings;
use 5.010;

my $old_file = 'in.txt';
my $new_file = 'out.txt';

my $CD_VER = 'CD29';

open my $rf, '<', $old_file or die "Cannot open $old_file for reading.";
open my $wf, '>'. $new_file or die "Cannot open $new_file for writing.";

while ( <$rf> ) {
    if (/CODEDROP/ and /regular/) {
        s/regular/$CD_VER/ ;
    }
    print $wf $_;
}

close $rf;
close $wf;

Replace a line in a text file using sed but keeping \ as part of the replacement line from perl script


Tag : linux , By : DicksGarage
Date : March 29 2020, 07:55 AM
should help you out What I need done is to replace a line in a file from perl using a sed command. The issue is the line I need to put into the file has \ in several places and I have not figured out how to kepp the \ without perl/sed removing them. , As noted in a comment:
#!/usr/bin/perl

use strict;
use warnings;

my $NAME = "Alexander";
my $USER_NAME = "The Great";
system "sed", "-e", "s/^destinaion=.*/destination=Manager {manager hostname\\\\=$NAME,Manager port\\\\=8443} $USER_NAME/", "-i.bak", "data";
##destinaion=Manager {manager hostname=Bill,Manager port=8443} Fred
destinaion=Manager {manager hostname=Bill,Manager port=8443} Fred
##destinaion=Manager {manager hostname=Bill,Manager port=8443} Fred
destination=Manager {manager hostname\=Alexander,Manager port\=8443} The Great
#!/usr/bin/perl

use strict;
use warnings;

my $NAME = "Alexander";
my $USER_NAME = "The Great";
system "sed", "-e", "s/^D1=.*/destination1=Manager {manager hostname\\\\=$NAME,Manager port\\\\=8443} $USER_NAME/", "-i.bak", "data";
system "sed -e \"s/^D2=.*/destination2=Manager {manager hostname\\\\\\\\=$NAME,Manager port\\\\\\\\=8443} $USER_NAME/\" -i.bak data";
system "sed -e 's/^D3=.*/destination3=Manager {manager hostname\\\\=$NAME,Manager port\\\\=8443} $USER_NAME/' -i.bak data";
D1=Manager {manager hostname=Bill,Manager port=8443} Fred
D2=Manager {manager hostname=Bill,Manager port=8443} Fred
D3=Manager {manager hostname=Bill,Manager port=8443} Fred
destination1=Manager {manager hostname\=Alexander,Manager port\=8443} The Great
destination2=Manager {manager hostname\=Alexander,Manager port\=8443} The Great
destination3=Manager {manager hostname\=Alexander,Manager port\=8443} The Great

How to replace a random IP-address in a file (Perl, Windows)


Tag : windows , By : Dave
Date : March 29 2020, 07:55 AM
wish of those help If your file has an IPv4 address, using the Regexp::Common module makes this an easy one-liner:
perl -MRegexp::Common=net -pe "s/$RE{net}{IPv4}/10.10.111.222/" FtpCommands.txt > FtpCommands_mod.txt
Related Posts Related QUESTIONS :
  • Using Perl to replace empty string with space
  • Displaying 12-h time in Perl using DateTime
  • Assign temp file from cgi and read it in .sh
  • why doesn't make -C change $PWD as seen through a scripting language such as Perl?
  • Using Win32::Env perl module, environment variable not setting permanently in windows
  • How to fetch a one table from HTML source file using lwp module?
  • How to process variable dimension array in Perl
  • perl print to a file and STDOUT which is the file
  • Get array in perl from json_encode
  • How to get unicode code points in perl v5.24?
  • using an array as a reference is deprecated in perl
  • Using Perl's File::Basename to rename a file extension
  • Unable to get page via HTTPS with LWP::Simple in Perl
  • Perl DBI case insensitive methods
  • How to resolve @INC related issue in Perl
  • How to do unary and/reduction of bus to a bit in Perl?
  • What does perl -pe "s|$PASSWORD|<PASSWORD>|g"?
  • I want to replace last / by ,
  • Strange regular expression
  • What does "!" mean before a variable in perl
  • Getting the inner hash in Perl
  • perl : can't require $value from other perl file and use it in 3rd perl file
  • perl assign reference to subroutine
  • Troubleshooting Perl CGI app - Can't locate object method "new" via package
  • How can I write a Template.pm filter to generate PNG output from LaTeX source code?
  • Error: Cannot parse function definition from ' hello()' in Mytest.xs, line 9
  • What's the proper way to fork() in FastCGI?
  • How can I use Math::Business::EMA with DBI to calculate exponential moving averages in Perl?
  • How can I print the calling program/module inside a method in Perl?
  • Which module should I use to parse mediawiki text into a Perl data structure?
  • How can I efficiently group a large list of URLs by their host name in Perl?
  • How can I restore STDOUT after redirecting it to a file in a Perl script?
  • How can I parse this configuration file format (allowing comments) in Perl?
  • How To Avoid a Perl script calling an Another Perl Script
  • Perl - Internal File (create and execute)
  • How can I call a PL/Perl function from another PL/Perl function?
  • How can I terminate a system command with alarm in Perl?
  • When should I use a factory to wrap a constructor in Perl?
  • How can I extract sentences with a particular phrase, in Perl?
  • Dequeue an array in perl with thread::queue
  • How do I tell perl to execute a piece of generated Perl code?
  • Why does a positive lookahead lead to captures in my Perl regex?
  • In a digital photo, how can I detect if a mountain is obscured by clouds?
  • How can I pass a hash to a Perl subroutine?
  • Why do I get 'use: command not found' when I run my Perl script?
  • How can I take a reference to specific hash value in Perl?
  • How do I find the sum of all values from two different arrays in Perl?
  • How can I determine which package my constructor was called from?
  • How do I install Perl's Lingua::Lid?
  • How can I copy a directory but ignore some files in Perl?
  • How can I build something like Amazon S3 in Perl?
  • What does Perl's -p command-line switch do?
  • How can I get the high-res mtime for a symbolic link in Perl?
  • How can I get the ultimate URL without fetching the pages using Perl and LWP?
  • How can I create a new file using a variable value as the name in Perl?
  • Why does my Perl CGI program fail with "Software error: ..."?
  • How can I handle -r=<pattern> with Perl's Getopt::Long?
  • Is there a way that I can force mod_perl to re-use buffer memory?
  • How do I issue an HTTP redirect with Perl's CGI.pm?
  • Can I pass a regex to isa() with Moose-based objects?
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com