سؤال

I have a report that outputs to a text file in linux and I need it modified a bit.

The file starts out like this if you cat it:

  6 E8 B7 48 36 8C AE
  8 00 0C 85 F2 F9 07
  8 44 03 A7 C0 0D 26
  8 C8 4C 75 5C B1 55
 10 00 05 00 E7 5B 9F
 10 00 17 C5 69 49 A1
 10 00 1D A2 E7 BC F1
 12 00 16 9C 6C 53 C0
 14 00 0C 85 F2 F9 08
 26 00 05 00 E7 5B B7

Some random text will be down here also...

I'm wanting it to look like this:

  6 E8:B7:48:36:8C:AE
  8 00:0C:85:F2:F9:07
  8 44:03:A7:C0:0D:26
  8 C8:4C:75:5C:B1:55
 10 00:05:00:E7:5B:9F
 10 00:17:C5:69:49:A1
 10 00:1D:A2:E7:BC:F1
 12 00:16:9C:6C:53:C0
 14 00:0C:85:F2:F9:08
 26 00:05:00:E7:5B:B7

Some random text will be down here also...

I was hoping to use sed to accomplish this.

هل كانت مفيدة؟

المحلول 3

One way using sed:

sed 's/  */:/3g'  file

نصائح أخرى

I think this would be easier with awk:

awk '{ printf("%2.2s %s:%s:%s:%s:%s:%s\n", $1, $2, $3, $4, $5, $6, $7); }' file

Following the answer of @Guru, you can try this one with sed:

sed -r 's/(\w) +/\1:/2g' file

You have to catch one letter before as sed does not support lookbehind.

Using Perl, this will work with any number of alignment spaces:

perl -lane '($p,$m)=/^(\s*\d+\s*)(.*)$/; $m =~ s/ /:/g; print $p,$m' input
sed "
: doublept
   s/^\( *[[:digit:]]\{1,\}\)\( \{1,\}\)\(.*\) \([[:alnum:]]\{1,\}\)/\1\2\3:\4/
   t doublept
"

work also for bigger "mac address" if any (i think mainly same kind of problem for IP adress and IPv6)

If you always know the line range for the numbers, you can use:-

sed 1,10s'/ /:/'g

The single quotes are important.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top