Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 384
  • Last Modified:

Need to reformat WWN string

Hi All,

I need a quick fix;

I have a load of WWN's in the format "0123456789012345"; I need them reformatted to "01:23:45:67:89:01:23:45"
0
jools
Asked:
jools
  • 2
  • 2
  • 2
  • +1
2 Solutions
 
ozoCommented:
WWN="0123456789012345"
WWN=`echo $WWN  | perl -lpe '$_=join":",unpack"(a2)*"'`
0
 
TintinCommented:

echo 0123456789012345 | sed "s/\(..\)/\1:/g"

Open in new window

0
 
ozoCommented:
echo 0123456789012345 | sed "s/\(..\)/\1:/g"
gives an extra : at the end
0
Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

 
RowleyCommented:
echo $WWN | sed "s/\(..\)/\1:/g;s/:$//"

fixes that.
0
 
RowleyCommented:
Similarly, I get an error when trying out your suggestion:

[root@centos01 tmp]# cat file1
0123456789012345


[root@centos01 tmp]# cat wwn2
#!/usr/bin/perl
#
while (<>) {
 $_=join":",unpack"(a2)*";
 print;
}


[root@centos01 tmp]# ./wwn2 file1
Not enough arguments for unpack at ./wwn2 line 4, near ""(a2)*";"
Execution of ./wwn2 aborted due to compilation errors.


0
 
joolsAuthor Commented:
Thanks guys,

I just got out of the meeting and knew you'd come thru :-)

I got the right result with this;
   WWN=`echo "${U_WWN}" | sed -e 's/-/:/g' | sed -e 's/0x//' | sed -e "s/\(..\)/\1:/g;s/:$//"`

I'd still be interested to see the perl workaround though.
0
 
joolsAuthor Commented:
Thanks Guys,

It would have been nice to see a perl fix as well (always good to keep options open and learn new things).
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now