ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
its not that always its 4 lines and start with same "Dodd". I have just given a sample example but it contains N number of line and starts with any character data
Yes, do what grail said. I didn't notice that those are separate files and you want to change the last line of each of those files. Furthermore, if you want to make the change in place and you have a GNU sed, you can use “-i” switch.
This Perl script will replace the last line of any text file with whatever you want it to be. It has options to write the file back to the original and/or the default just prints to standard out.
Code:
#!/usr/bin/perl
# replaceLastLine.pl
# sydney 12/28/2013
# This script is provided for rohit_shinez and has no warranty.
# It is in response to http://www.linuxquestions.org/questions/programming-9/find-and-replace-last-line-in-a-file-4175489359/
use strict;
use warnings;
# Command Line Arguments
my $file = shift( @ARGV );
my $replace = shift( @ARGV );
my $write = "0";
my $help = "0";
my $suppress = "0";
for ( @ARGV ) {
if (( $_ eq "-w" ) || ( $_ eq "--write" )) {
$write = "1";
}
if (( $_ eq "-h" ) || ( $_ eq "--help" )) {
$help = "1";
}
if (( $_ eq "-s" ) || ( $_ eq "--suppress" )) {
$suppress = "1";
}
}
# Help Section and Usage
if (
( not defined $file ) ||
( not defined $replace ) ||
( $file eq "-h" ) ||
( $file eq "--help" ) ||
( $replace eq "-h" ) ||
( $replace eq "--help" ) ||
( $help eq "1" )
) {
print "Usage: replaceLastLine.pl [FILE] \"replacement text\" [OPTIONS] \n -h, --help print help \n -w,--write Replace the original file.\n -s,--suppress Suppress standard output (do not print to screen)\n";
exit;
}
# Open File
my $fh;
my @lines;
open ( $fh, '<' , $file ) or die;
# Collect the Lines
while ( <$fh> ) {
push ( @lines,$_ );
}
close $fh;
# Remove the last line
pop( @lines );
# Add new last line
push ( @lines , $replace . "\n" );
# Print to Standard Out
if ( $suppress eq "0" ) {
print @lines;
}
# Print New File
if ( $write eq "1" ) {
open ( $fh, '>' , $file ) or die;
print $fh @lines;
close $fh;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.