LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-30-2009, 05:44 PM   #1
elproducto
LQ Newbie
 
Registered: Mar 2009
Posts: 2

Rep: Reputation: 0
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
 
Old 03-30-2009, 06:40 PM   #2
kenoshi
Member
 
Registered: Sep 2007
Location: SF Bay Area, CA
Distribution: CentOS, SLES 10+, RHEL 3+, Debian Sarge
Posts: 159

Rep: Reputation: 32
Quote:
Originally Posted by elproducto View Post
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.
 
Old 03-30-2009, 06:46 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,353

Rep: Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042Reputation: 1042
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/'

Last edited by syg00; 03-30-2009 at 06:51 PM.
 
Old 03-30-2009, 07:35 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,696
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
awk
Code:
# echo MACIMAW1 | awk 'BEGIN{OFS=FS=""}{$(NF-1)="M"}1'
MACIMAM1
 
Old 03-31-2009, 12:35 PM   #5
elproducto
LQ Newbie
 
Registered: Mar 2009
Posts: 2

Original Poster
Rep: Reputation: 0
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
 
Old 03-31-2009, 12:41 PM   #6
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 59
Quote:
Originally Posted by elproducto View Post
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.)

Last edited by Telemachos; 03-31-2009 at 12:49 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to replace special character by any word in a string. mksc Linux - Newbie 1 08-21-2008 02:33 AM
Perl: replace character in file vippie Programming 4 03-23-2007 04:26 AM
find certain character and replace with a number yongitz Programming 1 01-18-2007 07:40 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM
Replace windows with 2nd distribution pk108 Fedora 2 01-06-2004 04:24 PM


All times are GMT -5. The time now is 11:38 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration