|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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: |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
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 :-) |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
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!! |
|
|
|
#11 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#12 |
|
Messages: n/a
Hébergeur: |
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(":")' |
|
|
|
#13 |
|
Messages: n/a
Hébergeur: |
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? |
|
![]() |
| Outils de la discussion | |
|
|