LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Replace 2nd to last Character with SED (https://www.linuxquestions.org/questions/programming-9/replace-2nd-to-last-character-with-sed-715608/)

elproducto 03-30-2009 05:44 PM

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

kenoshi 03-30-2009 06:40 PM

Quote:

Originally Posted by elproducto (Post 3493049)
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

You can try something like:
Code:

networksetup -getcomputername | sed -re 's/W([0-9a-zA-Z])$/M\1/'
There's probably better ways to do this, but hope this helps.

syg00 03-30-2009 06:46 PM

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/'

ghostdog74 03-30-2009 07:35 PM

awk
Code:

# echo MACIMAW1 | awk 'BEGIN{OFS=FS=""}{$(NF-1)="M"}1'
MACIMAM1


elproducto 03-31-2009 12:35 PM

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
usage: sed script [-Ealn] [-i extension] [file ...]
      sed [-Ealn] [-i extension] [-e script] ... [-f script_file] ... [file ...]

When I take out the -r, I get the following error

Code:

sed: 1: "s/W(.)$/M\1/": \1 not defined in the RE
The AWK Script works perfectly on Mac OS and also add the M1 to computer that do not have the W in the second position. Thanks again for the help

Telemachos 03-31-2009 12:41 PM

Quote:

Originally Posted by elproducto (Post 3493934)
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
usage: sed script [-Ealn] [-i extension] [file ...]
      sed [-Ealn] [-i extension] [-e script] ... [-f script_file] ... [file ...]


On OS X, you get the BSD version of sed rather than GNU sed, which is what most Linux distros have. The BSD version doesn't support all the options of the GNU version, and they can differ in subtle and annoying ways. (One of the great joys of the history of Unix and Linux is different implementations of core utilities.) If you want to get the GNU version, check out MacPorts or Fink.

And, just for fun, here's a Perl version:
Code:

echo MACIMAW1 | perl -pe 'substr($_, -3, 1, 'M')' -
(If it bothers you that the offset is -3 (three characters back starting at the end of the string), remember that Perl counts back starting from the newline: W1\n. I often forget this.)


All times are GMT -5. The time now is 02:28 AM.