LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Find and replace last line in a file (https://www.linuxquestions.org/questions/programming-9/find-and-replace-last-line-in-a-file-4175489359/)

rohit_shinez 12-28-2013 01:23 AM

Find and replace last line in a file
 
Hi i am having multiple files with different date at last line which s at 2nd position

is it possible to achieve to change the 2nd position date to one single date
for eg:


Code:

file1
Aaa,bb,cc,dd
Xxx,yy,d,12
Dodd,12-Jun,t

file2
Aaa,bb,cc,dd
Xxx,yy,d,12
Dodd,10-Jun,t

file3
Aaa,bb,cc,dd
Xxx,yy,d,12
Dodd,16-Dec,t

i need to replace in all the files last line like below without affecting any other data in each of the files

Code:

file1
Aaa,bb,cc,dd
Xxx,yy,d,12
Dodd,10-Aug,t

file2
Aaa,bb,cc,dd
Xxx,yy,d,12
Dodd,10-Aug,t

file3
Aaa,bb,cc,dd
Xxx,yy,d,12
Dodd,10-Aug,t


grail 12-28-2013 01:54 AM

Show us your attempt and where you are stuck? If not sure where to start I would suggest sed as this should be rather simple.

rohit_shinez 12-28-2013 07:48 AM

This what i have tried

sed -e '$s/10-Jun/10-Aug/' file its working but since i am having multiple files and also multiple dates to replace with 10-Aug i am struck on it

mina86 12-28-2013 07:58 AM

If each group is always 4 lines followed by an empty line you could just do “p” three times, followed by “s/,.*,/,10-Aug/” and another “p”.

Or, if the line you want to change always starts with “Dodd”, you can just use that in your “s” command.

rohit_shinez 12-28-2013 08:14 AM

Hi,

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

sycamorex 12-28-2013 08:31 AM

If that's the only date in the files and the formatting is consistent across all files, you could try replacing it as follows:

Code:

sed -n 's/[0-9][0-9]*-[A-Z][a-z][a-z]/10-Aug/' files
It will match: [1 or 2 digits] - [capital letter][lower case letter][lower case letter] as in 12-Aug

grail 12-28-2013 11:50 AM

hmmm ... I was thinking if it is always 3 fields separated by commas just grab the lot:
Code:

sed '$s/,.*,/,10-Aug,/' files

mina86 12-28-2013 12:24 PM

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.

Sydney 12-28-2013 01:42 PM

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;
}


kurumi 12-29-2013 10:30 PM

if you have Ruby
Code:

contents  = File.open("file").readlines      # read whole file
last_line = contents[-1].split(",")          # split last line
last_line[1] = "new date"                    # change date
contents[-1] = last_line.join(",")            # join back
File.open( "file", "w+" ).write( contents )  # update


chrism01 12-30-2013 01:16 AM

@Sydney; you can replace the loop file-into-array
Code:

while ( <$fh> ) {
        push ( @lines,$_ );
}

#Use this one-line 'slurp' method instead
@lines = <$fh>


Sydney 12-30-2013 11:04 AM

Quote:

#Use this one-line 'slurp' method instead
@lines = <$fh>
Nice Thanks


All times are GMT -5. The time now is 07:03 AM.