PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > insert a field seperator
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

insert a field seperator

Réponse
 
LinkBack Outils de la discussion
Vieux 09/12/2006, 16h47   #1
cconnell_1@lycos.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut insert a field seperator

Hi,
If I have the string as follows in my script:

00145E86B099

How do I use awk or sed to put a : every 2nd charcater so I have

00:14:5E:86:B0:99

?

Thanks

  Réponse avec citation
Vieux 09/12/2006, 17h01   #2
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: insert a field seperator

2006-12-9, 08:47(-08), cconnell_1@lycos.com:
> Hi,
> If I have the string as follows in my script:
>
> 00145E86B099
>
> How do I use awk or sed to put a : every 2nd charcater so I have
>
> 00:14:5E:86:B0:99

[...]

string=00145E86B099
printf '%s\n' "$string" | fold -w2 | paste -sd: -

printf '%s\n' "$string" | sed 's/../&:/g;s/:$//'

--
Stéphane
  Réponse avec citation
Vieux 09/12/2006, 17h04   #3
RolandRB
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: insert a field seperator


cconnell_1@lycos.com wrote:
> Hi,
> If I have the string as follows in my script:
>
> 00145E86B099
>
> How do I use awk or sed to put a : every 2nd charcater so I have
>
> 00:14:5E:86:B0:99
>
> ?
>
> Thanks


$ echo 00145E86B099 | sed 's%..%&:%g'
00:14:5E:86:B0:99:

  Réponse avec citation
Vieux 09/12/2006, 17h09   #4
RolandRB
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: insert a field seperator


RolandRB wrote:
> cconnell_1@lycos.com wrote:
> > Hi,
> > If I have the string as follows in my script:
> >
> > 00145E86B099
> >
> > How do I use awk or sed to put a : every 2nd charcater so I have
> >
> > 00:14:5E:86:B0:99
> >
> > ?
> >
> > Thanks

>
> $ echo 00145E86B099 | sed 's%..%&:%g'
> 00:14:5E:86:B0:99:


To get rid of the last ":"
$ echo 00145E86B099 | sed 's%..%&:%g;s%:$%%'
00:14:5E:86:B0:99

  Réponse avec citation
Vieux 09/12/2006, 17h28   #5
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: insert a field seperator

On 2006-12-09, cconnell_1@lycos.com wrote:
> Hi,
> If I have the string as follows in my script:
>
> 00145E86B099
>
> How do I use awk or sed to put a : every 2nd charcater so I have
>
> 00:14:5E:86:B0:99


This will be many times faster than solutions using external
commands:

var=00145E86B099
newvar=
time while :
do
case $var in
???*) temp=${var#??}
newvar=${newvar:+"$newvar":}${var%"$temp"}
var=$temp
;;
?*) newvar=${newvar:+"$newvar":}$var; break ;;
*) newvar=$newvar$var; break ;;
esac
done
echo $newvar


--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
  Réponse avec citation
Vieux 09/12/2006, 17h44   #6
John W. Krahn
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: insert a field seperator

cconnell_1@lycos.com wrote:
> Hi,
> If I have the string as follows in my script:
>
> 00145E86B099
>
> How do I use awk or sed to put a : every 2nd charcater so I have
>
> 00:14:5E:86:B0:99


$ echo 00145E86B099 | perl -lne'print join ":", /../g'
00:14:5E:86:B0:99


John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
  Réponse avec citation
Vieux 09/12/2006, 18h17   #7
Robert Katz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: insert a field seperator

cconnell_1@lycos.com wrote:
> Hi,
> If I have the string as follows in my script:
>
> 00145E86B099
>
> How do I use awk or sed to put a : every 2nd charcater so I have
>
> 00:14:5E:86:B0:99
>


sed 's/\(..\)/&:/g;s/:$//'


BEGIN { FS = "" }
{
for (i=1; i<=NF; i+=2)
printf "%s%s%s", $i, $(i+1), (i<NF-2) ? ":" : "\n"
}

--
Regards,

---Robert
  Réponse avec citation
Vieux 09/12/2006, 18h22   #8
Robert Katz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: insert a field seperator

Robert Katz wrote:
> cconnell_1@lycos.com wrote:
>> Hi,
>> If I have the string as follows in my script:
>>
>> 00145E86B099
>>
>> How do I use awk or sed to put a : every 2nd charcater so I have
>>
>> 00:14:5E:86:B0:99

>
> sed 's/\(..\)/&:/g;s/:$//'
>


I'm obviously not making use of tags, so I guess I didn't need them.

sed 's/../&:/g;s/:$//'

>
> BEGIN { FS = "" }
> {
> for (i=1; i<=NF; i+=2)
> printf "%s%s%s", $i, $(i+1), (i<NF-2) ? ":" : "\n"
> }



--
Regards,

---Robert

  Réponse avec citation
Vieux 09/12/2006, 18h32   #9
cconnell_1@lycos.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: insert a field seperator


Robert Katz wrote:
> Robert Katz wrote:
> > cconnell_1@lycos.com wrote:
> >> Hi,
> >> If I have the string as follows in my script:
> >>
> >> 00145E86B099
> >>
> >> How do I use awk or sed to put a : every 2nd charcater so I have
> >>
> >> 00:14:5E:86:B0:99

> >
> > sed 's/\(..\)/&:/g;s/:$//'
> >

>
> I'm obviously not making use of tags, so I guess I didn't need them.
>
> sed 's/../&:/g;s/:$//'
>
> >
> > BEGIN { FS = "" }
> > {
> > for (i=1; i<=NF; i+=2)
> > printf "%s%s%s", $i, $(i+1), (i<NF-2) ? ":" : "\n"
> > }

>
>
> --
> Regards,
>
> ---Robert


Wow! Im overwhelmed with all the responses on this, Im trying them all
out and they work well. Cheers :-)

  Réponse avec citation
Vieux 09/12/2006, 18h42   #10
cconnell_1@lycos.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: insert a field seperator


cconnell_1@lycos.com wrote:
> Robert Katz wrote:
> > Robert Katz wrote:
> > > cconnell_1@lycos.com wrote:
> > >> Hi,
> > >> If I have the string as follows in my script:
> > >>
> > >> 00145E86B099
> > >>
> > >> How do I use awk or sed to put a : every 2nd charcater so I have
> > >>
> > >> 00:14:5E:86:B0:99
> > >
> > > sed 's/\(..\)/&:/g;s/:$//'
> > >

> >
> > I'm obviously not making use of tags, so I guess I didn't need them.
> >
> > sed 's/../&:/g;s/:$//'
> >
> > >
> > > BEGIN { FS = "" }
> > > {
> > > for (i=1; i<=NF; i+=2)
> > > printf "%s%s%s", $i, $(i+1), (i<NF-2) ? ":" : "\n"
> > > }

> >
> >
> > --
> > Regards,
> >
> > ---Robert

>
> Wow! Im overwhelmed with all the responses on this, Im trying them all
> out and they work well. Cheers :-)


Also - u are all a bunch of clever dudes!!

  Réponse avec citation
Vieux 09/12/2006, 20h45   #11
Michael Paoli
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: insert a field seperator

cconnell_1@lycos.com wrote:
> If I have the string as follows in my script:
> 00145E86B099
> How do I use awk or sed to put a : every 2nd charcater so I have
> 00:14:5E:86:B0:99


First of all - specification. Since computers and UNIX in general
tend to do what one tells them - which may be distinct from what one
wants or intends, let's first try to carefully define the
specification. Your subject says, "insert a field seperator (sic)",
but the body says "put a : every 2nd charcater (sic)", but the
example seems to also imply : is added as a separator after each pair
of characters where such is before one or more following non-newline
characters. So we'll go with 2 out of 3 and use : as a separator,
not a terminator of character pairs. No mention is made of cases
where : is present in the input data, or where the line contains an
odd number of non-: non-newline characters., so we'll not treat those
cases specially, and just do what was specified anyway. No mention
was made about string(s) or line missing a trailing newline (e.g.
file with string that is missing final newline). Since this was
unspecified and awk and sed were requested, we'll presume our results
may be unspecified in such a case, and will just do whatever awk or
sed does in such a case.

Now that we've got a quite complete specification :-) ... some of
the other examples cited won't quite meet that particular
specification criteria (but may well fit within any ambiguities of
the earlier and less complete specification).

Now, for some test data (first line blank):

0
00
001
0014
00145
00145E86B099
:
0:
00:
001:
0014:
00145:
00145E86B099:
:
0::
00::
00:1:
00:14:
00:145:
00:145E86B099:

awk '{
#while our string is more than 2 characters long ...
while(length>2){
#print the first two characters followed by a colon
printf("%s:",substr($0,1,2));
#remove the first two characters from our string
$0=substr($0,3);
#repeat as necessary
}
#print whatever is left of our line, including any newline
print $0;
}'

sed -ne '
#place an embedded newline after every 2nd character on line
s/../&\
/g
#strip off any trailing embedded newline we added
s/\
$//
#replace the embedded newlines with :
s/\
/:/g
#print it, we are done with that line
p
'

And in testing with our test input data, both of these example gives us
the same output:

0
00
00:1
00:14
00:14:5
00:14:5E:86:B0:99
:
0:
00::
00:1:
00:14::
00:14:5:
00:14:5E:86:B0:99::
:
0:::
00:::
00::1::
00::1:4:
00::1:45::
00::1:45:E8:6B:09:9:

That may or may not be exactly what would be desired, but it does
precisely satisfy exactly what we've specified thus far.

  Réponse avec citation
Vieux 09/12/2006, 21h25   #12
William James
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: insert a field seperator


cconnell_1@lycos.com wrote:
> Hi,
> If I have the string as follows in my script:
>
> 00145E86B099
>
> How do I use awk or sed to put a : every 2nd charcater so I have
>
> 00:14:5E:86:B0:99
>
> ?
>
> Thanks


echo 00145E86B099| ruby -ne 'puts scan(/../).join(":")'

  Réponse avec citation
Vieux 09/12/2006, 21h35   #13
Kenny McCormack
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: insert a field seperator

In article <1165699511.799016.167550@j72g2000cwa.googlegroups .com>,
William James <w_a_x_man@yahoo.com> wrote:
>
>cconnell_1@lycos.com wrote:
>> Hi,
>> If I have the string as follows in my script:
>>
>> 00145E86B099
>>
>> How do I use awk or sed to put a : every 2nd charcater so I have
>>
>> 00:14:5E:86:B0:99
>>
>> ?
>>
>> Thanks

>
>echo 00145E86B099| ruby -ne 'puts scan(/../).join(":")'


What part of "awk or sed" did you not understand?

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h04.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,27010 seconds with 21 queries