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?
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)
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
|