Replace 2nd to last Character with SED
I have a quick question. I use a command that pulls computer name and would like to use SED to change the second to last character from W to M. An example of command output would be MACIMAW1 and I would like to change the second to last character to M so in this case MACIMAW1 would change to MACIMAM1. For this is used networksetup -getcomputername |sed s/W/M/, This work fine until I get a result with multiple W's for example WWHILW1, in that case my SED string would return MWILW1. Any help would be greatly appreciated. Thanks
|
Quote:
Code:
networksetup -getcomputername | sed -re 's/W([0-9a-zA-Z])$/M\1/' |
sed -r 's/(.*)W(.)$/\1M\2/' should probably do it
<Edit:> thanks to kenoshi, why don't I simplify it to ... sed -r 's/W(.)$/M\1/' |
awk
Code:
# echo MACIMAW1 | awk 'BEGIN{OFS=FS=""}{$(NF-1)="M"}1' |
Awk and sed
Thanks Guys for all the help.
For some reason the Mac OS version of sed does not like -r option and none of the Sed command worked for me I got error Code:
sed: illegal option -- r Code:
sed: 1: "s/W(.)$/M\1/": \1 not defined in the RE |
Quote:
And, just for fun, here's a Perl version: Code:
echo MACIMAW1 | perl -pe 'substr($_, -3, 1, 'M')' - |
All times are GMT -5. The time now is 02:28 AM. |