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
# 8  
Old 12-29-2013
Quote:
Originally Posted by rohit_shinez
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
I assume that last line is not blank

Try :

Code:
#!/bin/bash

# Replace here whatever you want in 2nd field
replace=replaces

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

rm fl.tmp

If you want to run this on a Solaris/SunOS system, use /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk instead of the default /usr/bin/awk .

Last edited by Akshay Hegde; 12-29-2013 at 02:33 AM.. Reason: typo fix
This User Gave Thanks to Akshay Hegde For This Post:
# 9  
Old 12-29-2013
Here is an alternative way that doesn't require tac:
Code:
#!/bin/ksh
IAm=${0##*/}
if [ $# -lt 2 ]
then    printf "Usage: %s replacement_string file...\n" "$IAm" >&2
        exit 1
fi
rep="$1"
shift
for file in "$@"
do      printf "%s: Processing file %s\n" "$IAm" "$file"
        ed -s "$file" <<-EOF
                \$g/\([^,]*,\)[^,]*/s//\1$rep/
                w
                q
EOF
done

This was tested used ksh and bash, but any shell that recognizes basic Bourne shell syntax should work just as well. It was also tested using ed and ex. On most systems ed is a little bit smaller and faster than ex, but your mileage may vary.

To use it to make the changes requested in message #4 in this thread, save the above script in a file (e.g., tester), make it executable using:
Code:
chmod +x tester

and run it with:
Code:
./tester "10-Aug" file[123]

This User Gave Thanks to Don Cragun For This Post:
# 10  
Old 12-29-2013
hi it worked if i use tail -r instead of tac thanks for you help
# 11  
Old 01-31-2014
Hi Don,

Is it possible to replace any field or coloumn irrespective of 2nd column i have mentioned or can i use the which col to be replaced by giving user input in your code ???
# 12  
Old 01-31-2014
Quote:
Originally Posted by rohit_shinez
Hi Don,

Is it possible to replace any field or coloumn irrespective of 2nd column i have mentioned or can i use the which col to be replaced by giving user input in your code ???
I'm not sure I understand what you want to do.

I am guessing that you want the script to take an additional operand that specifies which field in the last line of each file is to be replaced, as in:
Code:
ScriptName replacement_string field_number file...

If that is what you want, we could add code before the for loop to calculate a BRE to use in ed to select the given number of fields minus one to be copied before the given field is replaced.

Note that my script verified that the last line in the file did have the appropriate number of fields and silently ignores the request if the last line is not in the proper format. Would you prefer to get an error message if the last line is not in the proper format?
# 13  
Old 01-31-2014
Hi don

Actually I need to replace the last line which comes in 4th position so is it possible to replace any position in last line using your code can you share it
# 14  
Old 01-31-2014
If by "last line in 4th position", you mean that you want to change the 4th comma delimited field in the last line, you could try something like:
Code:
#!/bin/ksh
IAm=${0##*/}
replacement="$1"
field="$2"
if [ $# -ge 3 ]
then    if [ ${#field} -eq 0 ] || [ "${field}" != "${field%*[^0-9]*}" ] ||
                [ $field -eq 0 ]
        then    printf "%s: invalid field# <%s>\n" "$IAm" "$field" >&2
                shift $#
        fi
fi
if [ $# -lt 3 ]
then    printf "Usage: %s replacement_string field# file...\n" "$IAm" >&2
        exit 1
fi
if [ "$2" -eq 1 ]
then    edCommandMiddle='[^,]*/'
else    BRE=''
        while [ $((field--)) -gt 1 ]
        do      BRE="${BRE}[^,]*,"
        done
        edCommandMiddle='\('"$BRE"'\)[^,]*/\1'
fi
shift 2
# printf "ed command will be: %s\n" '$'"s/^$edCommandMiddle$replacement/"
for file in "$@"
do      printf "%s: Processing file %s\n" "$IAm" "$file"
        ed -s "$file" <<-EOF
                \$s/^$edCommandMiddle$replacement/
                w
                q
EOF
done

As before, the 1st operand you need to give to this script is the string you want to use to replace the current contents of the selected field. The 2nd operand is the field number for the field you want to change. And the remaining operands are pathnames to the files you want to change. So, if you named this script tester and you want to change the 4th field of the last line in files named file1, file2, and file3 to "new text", use:
Code:
./tester 'new text' 4 file[123]

These 2 Users Gave Thanks to Don Cragun For This Post:
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