Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to add one character before the last one in each line... 1

Status
Not open for further replies.

ogniemi

Technical User
Joined
Nov 7, 2003
Messages
1,041
Location
PL
in sed and awk?


thx!
 
Hi

Assuming the character to be added is "x" :
Code:
awk '{$0=substr($0,1,length($0)-1)"x"substr($0,length($0))}1' /input/file

[gray]# or ( gawk only )[/gray]

awk '{print gensub(/(.)$/,"x\\1","")}' /input/file

[gray]# or[/gray]

sed 's/\(.\)$/x\1/' /input/file

Feherke.
 
To add an X before the last char of every line:

Code:
sed 's/.$/X&/' </path/to/input >/path/to/output


HTH,

p5wizard
 
Hi

Of course, you are right ! That [tt]&[/tt] works in regular [tt]awk[/tt] implementations too, so can be used in [tt]sub()[/tt].

Thank you, p5wizard. [medal]

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top