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
# 8  
Old 05-28-2009
Hi Jagadeeswaran,
This is an interesting command (awk '{printf("%sZ\n",substr($0,1,length($0)-1)) ;}' test).
Can please explain how it works

Thanks
# 9  
Old 05-28-2009
Code:
awk '{printf("%sZ\n",substr($0,1,length($0)-1)) ;}' test

print the line till the end (except last character) using

Code:
substr($0,1,length($0)-1)

. "Z" is the the character tht u want to be the last one so he is printing ..U can print wht evr character u wish at the end.
# 10  
Old 05-28-2009

In a slightly simpler form it may be easier to understand:

Code:
awk '{ print substr( $0 , 1, length-1 ) "Z" }'

# 11  
Old 05-28-2009
Quote:
Originally Posted by panyam
. "Z" is the the character tht u want to be the last one so he is printing ..U can print wht evr character u wish at the end.
Hello,

Per our forum rules, all users must write in English, use semi-formal or formal English language and style, and correct spelling errors.

The reason for this is that most software and operating systems are written in English and these are software related technical forums.

In addition, nearly 95% of all visitors to this site come here because they are referred by a search engine. In order for future searches on your post (with answers) to work well, you need to spell correctly!

So, as a benefit and courtesy to current and future knowledge seekers, please be careful with your language, check your spelling and correct your spelling errors. You might receive a forum infraction if you don't pay attention to this.

Also, do not write in cyberpunk or abbreviated chat style under any circumstances and do not use profanity. This is not a chat room, it is a formal knowledge base to serve you and everyone, of all ages and cultural backgrounds.

Thanks!

The UNIX and Linux Forums
# 12  
Old 05-28-2009
Alternatively, if you have perl:

Code:
$
$ perl -ne 'substr($_,-2,1)="Z"; print' result.dat
1.11a,Test result for scenario 1,1 2 3 4,Z
1.12b,Test result for scenario 2,incomplete,Z
1.13,Test result for scenario 3, 4 5 6,Z
$

tyler_durden
# 13  
Old 05-28-2009
Quote:
Originally Posted by Jagadeeswaran.K
awk '{printf("%sZ\n",substr($0,1,length($0)-1)) ;}' test
Can you please explain how this command works

Thanks
# 14  
Old 05-28-2009

Here it is broken into its parts:

Code:
awk '{
 ll = length($0)                      ## length of the line
 linewanted = substr( $0, 1, ll - 1 ) ## line minus last character
 print linewanted "Z"                 ## print linewanted plus "Z"
 }'

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