sed replace multiple occurrences on the same line, but not all

Hi there!

I am really enjoying working with sed. I am trying to come up with a sed command to replace some occurrences (not all) in the same line, for instance:

I have a command which the output will be:

200.300.400.5 0A 0B 0C 01 02 03

being that the last 6 strings are actually one MAC address

I wanted to re-write the output of the command so it will come out like this:

200.300.400.5 0A0B.0C01.0203

Considering space " " as a delimiter, it will ALWAYS join 2nd and 3rd fields, then replace " " with "." between 3rd and 4th fields, join 4th and 5th fields, replace " " with "." between rth and 6th fields.

It can be something other than sed as well.

It is a bonus if the script also transform "CAPS" into "small caps"

200.300.400.5 0a0b.0c01.0203

Cannot find how to make sed replace certain occurrences on the same line! :frowning:

Try awk

awk '{$0=tolower($1FS$2$3OFS$4$5OFS$6$7)}1' OFS="." file

Smell like a homework.

ok... supose the file has multiple lines... will that work?

even with one line I am trying here and I get error:

home:~> cat 10.3.2.10_VLAN36_MACS
248.88.41.141 00 0F F8 58 29 8D
home:~> awk '{$0=tolower($1FS$2$3OFS$4$5OFS$6$7)}1' OFS="." 10.10.10.10_VLAN36_MACS
awk: syntax error near line 1
awk: bailing out near line 1
home:~>

I am using Solaris 5.10 by the way

# cat 10.3.2.10_VLAN36_MACS
248.88.41.141 00 0F F8 58 29 8D

# awk '{$0=tolower($1FS$2$3OFS$4$5OFS$6$7)}1' OFS="." 10.3.2.10_VLAN36_MACS
248.88.41.141 000f.f858.298d

Use GNU awk (gawk), New awk (nawk) or POSIX awk (/usr/xpg4/bin/awk) and pay attention to file names :!

got nawk, works like a charm! thank you! :smiley:

by the way... not homework... trying to automate some work stuff :slight_smile:

$_="200.300.400.5 0A 0B 0C 01 02 03";
s/ (?=[^ ]+( [^ ]+ [^ ]+)*$)//g;
s/([A-Z]+)/lc($1)/eg;
my @tmp=split(" ",$_,2);
$tmp[1]=~s/ /./g;
print $tmp[0]," ",$tmp[1];

HOW can we swap two fields in every line of text using SED command
can this command works?
sed s/$1/$2/g file[/i][/b]

---------- Post updated at 12:24 PM ---------- Previous update was at 12:19 PM ----------

[quote=krkrishna.cse;302340582]
HOW can we swap two fields in every line of text using SED command
can this command works?
sed s/$1/$2/g file

@ krkrishna.cse : sed could do but awk is better in this case:

awk '{print $2, $1}' yourfile