Replacing the last character for each line in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing the last character for each line in a file
# 1  
Old 05-27-2009
Replacing the last character for each line in a file

Hello,
I have a csv file and will like to replace the last character of each line in the file with Z
# 2  
Old 05-27-2009
what have you got?
# 3  
Old 05-27-2009

Use awk's substr and length functions.
# 4  
Old 05-27-2009
Example of file content:
ksh: cat result.dat
1.11a,Test result for scenario 1,1 2 3 4,p
1.12b,Test result for scenario 2,incomplete,q
1.13,Test result for scenario 3, 4 5 6,P
...

The number of characters per line varies but the last field is always one character (e.g ,p) and the number of fields per line is always 4


Thanks for assisting
# 5  
Old 05-28-2009
this will do i guess
Code:
awk -F"," '{gsub($NF,"z")}{print}' filename

# 6  
Old 05-28-2009
For easier understanding:

Code:
 awk '{printf("%s",substr($0,1,length($0)-1));b=substr($0,length($0)-1,1);sub(b,"Z",b);print b}' infile

-Devaraj Takhellambam
# 7  
Old 05-28-2009
awk '{printf("%sZ\n",substr($0,1,length($0)-1)) ;}' test
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk sed to repeat every character on same position from the upper line replacing whitespace

Hello is it possible with awk or sed to replace any white space with the previous line characters in the same position? I am asking this because the file I have doesn't always follow a pattern. For example the file I have is the result of a command to obtain windows ACLs: icacls C:\ /t... (5 Replies)
Discussion started by: nakaedu
5 Replies

2. Shell Programming and Scripting

Replacing line 'i' of file1 with line 'j' of file 2

Hi All, As mentioned in the title I have two text files and I would like to replace line number 5 of file #1 with line number 4 of file #2 e.g. file 1 wqwert 4.4464002 3 319 286 369 46.320002 56.150002 45.100002 1 1 1 0.723 (12 Replies)
Discussion started by: f_o_555
12 Replies

3. HP-UX

How to remove new line character and append new line character in a file?

Hi Experts, I have data coming in 4 columns and there are new line characters \n in between the data. I need to remove the new line characters in the middle of the row and keep the \n character at the end of the line. File is comma (,) seperated. Eg: ID,Client ,SNo,Rank 37,Airtel \n... (8 Replies)
Discussion started by: sasikari
8 Replies

4. Shell Programming and Scripting

Renaming a file and replacing the special character in the name with date

HI all, How can i rename some files and replace the special character in the name with todays date ex: Name#file1.txt Name#file2.txt to be renamed as Name.20091119.file1.txt Name.20091119.file2.txt (11 Replies)
Discussion started by: abhinav192
11 Replies

5. Shell Programming and Scripting

Replacing Character in a file based on element

Hi, I have file like below. Unix:/pclls/turc>cat tibc.property executeReceiver=Y executeSender=Y I want to replace executeSender=N in the file. My file should be like below. executeReceiver=Y executeSender=N I tried with the below command, its giving error. cat tibc.property |... (2 Replies)
Discussion started by: senthil_is
2 Replies

6. Shell Programming and Scripting

Awk not working due to missing new line character at last line of file

Hi, My awk program is failing. I figured out using command od -c filename that the last line of the file doesnt end with a new line character. Mine is an automated process because of this data is missing. How do i handle this? I want to append new line character at the end of last... (2 Replies)
Discussion started by: pinnacle
2 Replies

7. Shell Programming and Scripting

error while replacing a string by new line character in sed

hi, when i am doing the following things getting error Can anyone please suggest i have a file where there is a line like the following branch=dev sdf dev jin kilii fin kale boyle dev james dev i want to search the existance of dev in the above line. cat "$file" | sed -n... (8 Replies)
Discussion started by: millan
8 Replies

8. Shell Programming and Scripting

Replacing a character string in a file

Hi there, I have a paramater file that looks like this :- IRL|07122005|27389|VTIEpay|email address|5|200 When my program finishes I want to replace the seventh field. the existing code is like this cat <<-EOF | ed -s $PARFILE 1,$ g/^$ICO/s/$prvdate/$TODAY/ 1,$... (13 Replies)
Discussion started by: rjsha1
13 Replies

9. UNIX for Dummies Questions & Answers

Replacing a character in a line

Hi All I want to replace a character in a line, but position will be different form one iteration to another. So i m keeping the position i a variable. I am trying with following code pos=3 echo "Hello World, Good Morning" | sed 's/\(.\{$pos\}\)./\1Y/' But its not working, Can you... (2 Replies)
Discussion started by: Usha Shastri
2 Replies

10. Shell Programming and Scripting

Replacing a line in a file - HELP!!!

I have a problem in the following code ... while read line do #Get Line Number OLDLINE=`sed -n $Lineno $filename` echo "Un Changed Line : "$OLDLINE echo "Enter a New Pattern : " read NewPattern <&1 echo "NewPattern :"$NewPattern NEWLINE=`cat $filename | sed -n... (1 Reply)
Discussion started by: maxmave
1 Replies
Login or Register to Ask a Question