LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-28-2013, 01:23 AM   #1
rohit_shinez
Member
 
Registered: Aug 2013
Posts: 40

Rep: Reputation: Disabled
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
 
Old 12-28-2013, 01:54 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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.
 
Old 12-28-2013, 07:48 AM   #3
rohit_shinez
Member
 
Registered: Aug 2013
Posts: 40

Original Poster
Rep: Reputation: Disabled
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
 
Old 12-28-2013, 07:58 AM   #4
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
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.
 
Old 12-28-2013, 08:14 AM   #5
rohit_shinez
Member
 
Registered: Aug 2013
Posts: 40

Original Poster
Rep: Reputation: Disabled
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
 
Old 12-28-2013, 08:31 AM   #6
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
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
 
Old 12-28-2013, 11:50 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
hmmm ... I was thinking if it is always 3 fields separated by commas just grab the lot:
Code:
sed '$s/,.*,/,10-Aug,/' files
 
1 members found this post helpful.
Old 12-28-2013, 12:24 PM   #8
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
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.
 
Old 12-28-2013, 01:42 PM   #9
Sydney
Member
 
Registered: Mar 2012
Distribution: Scientific Linux
Posts: 147

Rep: Reputation: 36
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;
}
 
Old 12-29-2013, 10:30 PM   #10
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
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
 
Old 12-30-2013, 01:16 AM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
@Sydney; you can replace the loop file-into-array
Code:
while ( <$fh> ) {
	push ( @lines,$_ );
}

#Use this one-line 'slurp' method instead
@lines = <$fh>
 
Old 12-30-2013, 11:04 AM   #12
Sydney
Member
 
Registered: Mar 2012
Distribution: Scientific Linux
Posts: 147

Rep: Reputation: 36
Quote:
#Use this one-line 'slurp' method instead
@lines = <$fh>
Nice Thanks
 
  


Reply

Tags
unix



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find and replace where the find is on two lines and replace on first line andrew777 Linux - General 9 08-05-2013 01:50 PM
search folders, find a line in a file, and replace it with a new line ithirdeye Programming 2 04-07-2012 04:27 PM
Python: find defined text string in a file, and replace the whole line Dark Carnival Programming 6 05-22-2007 06:02 AM
command line edit -- global find/replace on text file w/o going into vi car182 Linux - Newbie 4 05-25-2006 05:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration