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

what have you got?

Use awk's substr and length functions.

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

this will do i guess

awk -F"," '{gsub($NF,"z")}{print}' filename

For easier understanding:

 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

awk '{printf("%sZ\n",substr($0,1,length($0)-1)) ;}' test

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

awk '{printf("%sZ\n",substr($0,1,length($0)-1)) ;}' test

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

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.

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

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

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

Alternatively, if you have perl:

$
$ 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

Can you please explain how this command works

Thanks

Here it is broken into its parts:

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"
 }'

Hello
How can I print the 2 fields if the last field is p for a csv file.
Example
1.11a,test2,Test result for scenario 1,1 2 3 4,v
1.12b,test4,Test result for scenario 2,incomplete,p
1.13,test6,Test result for scenario 3, 4 5 6,p

Desired result is
test4
test6

Thanks

people have shown you how to use awk already. last field value stands for $NF, second field stands for $2. therefore, pls try and show some code. Read the awk docs especially.

sed 's/.$/z/'

this expression allow to replace last character by z

-----Post Update-----

try1 is your file

cat try1 | while read line; do res=$(echo $line | awk -F "," '{print $NF}');if [ $res = "p" ];then var=$(echo $line | awk -F "," '{print $2}');echo $var;fi; done

-----Post Update-----

try1 is your file

cat try1 | while read line; do res=$(echo $line | awk -F "," '{print $NF}');if [ $res = "p" ];then var=$(echo $line | awk -F "," '{print $2}');echo $var;fi; done

no need while loop, cat and those extra if/else and awk. Just one awk command can do it.

certainly, but i haven't good knowledges in command awk.
I'm a beginner.

not sure if this works for csv
sed 's/$/z/g' filename