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 Code:
file1 |
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.
|
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 |
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. |
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 |
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 |
hmmm ... I was thinking if it is always 3 fields separated by commas just grab the lot:
Code:
sed '$s/,.*,/,10-Aug,/' files |
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 |
if you have Ruby
Code:
contents = File.open("file").readlines # read whole file |
@Sydney; you can replace the loop file-into-array
Code:
while ( <$fh> ) { |
Quote:
|
All times are GMT -5. The time now is 07:03 AM. |