Find and replace last line in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find and replace last line in a file
# 1  
Old 12-01-2013
Find and replace last line in a file

Hi I am having a file which has like this content shown below
Code:
Aaa,bb,cc,dd
Xxx,yy,d,12
Dodd,12-Jun,t

I need to replace last line like this 
Aaa,bb,cc,dd
Xxx,yy,d,12
Dodd,10-August,t

# 2  
Old 12-01-2013
Try : for given input, with the assumption that there is no empty new line at the end

Code:
$ tac file | awk -F, 'NR==1{$2=replace}1' OFS=\, replace=10-August | tac
Aaa,bb,cc,dd
Xxx,yy,d,12
Dodd,10-August,t

OR

--edit----
Code:
$ awk -F, 'NR == 1 ; getline ; END{$2=replace;print}' OFS=\, replace=10-August file
Aaa,bb,cc,dd
Xxx,yy,d,12
Dodd,10-August,t


Last edited by Akshay Hegde; 12-01-2013 at 11:36 PM.. Reason: some more awk...
# 3  
Old 12-01-2013
Hi,
With sed ( no tested ) :
Code:
sed -e '$s/12-Jun/10-August/' file

This command replace first "12-Jun" by 10-August and only in last line of file.

Regards.
# 4  
Old 12-28-2013
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


Last edited by rohit_shinez; 12-28-2013 at 03:23 AM..
# 5  
Old 12-28-2013
Code:
sed -i '$s/,[^,]*,/,10-August,/' file1 file2


Last edited by MadeInGermany; 12-28-2013 at 05:51 AM..
# 6  
Old 12-28-2013
One way using tac and awk
Code:
#!/bin/bash

# Replace here whatever you want in 2nd field
replace=10-Aug

for file in file*; do
    tac $file | \
        awk -F, 'NR==1{ $2 = replace}1' OFS=, replace=$replace | tac >fl.tmp && cat fl.tmp >$file
done

rm fl.tmp

This User Gave Thanks to Akshay Hegde For This Post:
# 7  
Old 12-29-2013
Hi ,

i would say tac is not working in my shell even i tried in bash mode, can you help me how i can use tac command in sunsol OS
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Script to find line in one file and replace in another

Hey Guys, im looking for a script that will work under OSX. What i want to do is copy information from one file (Specific LIne) and write it to a certain line in another. To be more specific... I want the hostname of a mac to be gathered ( i assume its stored in a .plist file somewhere) and... (2 Replies)
Discussion started by: padgo
2 Replies

2. UNIX for Advanced & Expert Users

Find and replace the line in text file

I have two files a.txt b.txt I want to find a line in a.txt and replace by another line from b.txt a.txt asfsdfsfsfdfsf asfwererfgdgf wrerwetretfdg b.txt werdfgdfgf werergfdgd sfdfgfgfgfgg i want to replace the 1st line of a.txt by 1st line of b.txt i want out put as (5 Replies)
Discussion started by: rammm
5 Replies

3. Shell Programming and Scripting

Find Node and replace line(s) preceding in xml file

Hello, I have an xml file whose contacts are like below: <Node>Apple <B>Value1</B> <B>Value2</B> <B>Value3</B> </Node> <Node>Mango <B>Value1</B> <B>Value2</B> <B>Value3</B> </Node> <Node>Apple <B>Value1</B> <B>Value2</B> <B>Value3</B> </Node> <Node>Bannana (3 Replies)
Discussion started by: umarsatti
3 Replies

4. Shell Programming and Scripting

find and replace a line in a file

Hi, I am want find and replace in following content in the file. i want to repalce a word TABLESPACE XCRM_<ANY_CHAR> to TABLESPACE XCRM Sample File to Replace : LOB(COMPLEX_VALUE) STORE AS ( TABLESPACE XCRM_MED_D_NEW STORAGE(INITIAL 64K BUFFER_POOL DEFAULT) ENABLE... (3 Replies)
Discussion started by: gavemani
3 Replies

5. Emergency UNIX and Linux Support

Find a line using a condition and replace a string in that line

Hello, I have a 100 line code. I have given a sample of it below: ABC*654654*1*54.54*21.2*87*1*654654654654 CCC*FS*FS*SFD*DSF GGG*FGH*CGB*FBDFG*FGDG ABC*654654*1*57.84*45.4*88*2*6546546545 CCC*WSF*SG*FGH*GHJ ADA*AF*SFG*DFGH*FGH*FGTH I need to select the line starting with "ABC" its... (3 Replies)
Discussion started by: nithins007
3 Replies

6. Shell Programming and Scripting

Find a line using a condition and replace a string in that line

Hello, I have a 100 line code. I have given a sample of it below: ABC*654654*1*54.54*21.2*87*1*654654654654 CCC*FS*FS*SFD*DSF GGG*FGH*CGB*FBDFG*FGDG ABC*654654*1*57.84*45.4*88*2*6546546545 CCC*WSF*SG*FGH*GHJ ADA*AF*SFG*DFGH*FGH*FGTH I need to select the line starting with "ABC" its... (6 Replies)
Discussion started by: nithins007
6 Replies

7. Shell Programming and Scripting

Find in first column and replace the line with Awk, and output new file

Find in first column and replace the line with Awk, and output new file File1.txt"2011-11-02","Georgia","Atlanta","x","","" "2011-11-03","California","Los Angeles","x","","" "2011-11-04","Georgia","Atlanta","x","x","x" "2011-11-05","Georgia","Atlanta","x","x","" ... (4 Replies)
Discussion started by: charles33
4 Replies

8. Shell Programming and Scripting

Find 5 lines and replace with 18 line in sql file where it contains multiple blocks.

My sql file xyz_abc.sql in this file there are multiple sql block in this block I need to find the following block rem Subset Rows (&&tempName.*) CREATE VIEW &&tempName.* AS SELECT * FROM &&tempName.* WHERE f is not null and replace with following code rem Subset Rows... (9 Replies)
Discussion started by: Zaheer.mic
9 Replies

9. Shell Programming and Scripting

Find and replace some line

Hi all I have a file which has n no. of lines. I want to find string called "John" and add 5 lines below the string. Help me to do it using script? (1 Reply)
Discussion started by: johnl
1 Replies

10. UNIX for Advanced & Expert Users

find a shell and replace the line

I need a shell which makes a search of an UNIX script and them modifies. :confused: vi $(grep -l 5 $(find . -name 'vellon.bcf' -print)) (1 Reply)
Discussion started by: jvellon
1 Replies
Login or Register to Ask a Question