1

I have binary log file with such similar lines which splitted by non-printable char "^A".

I see this symbol backlit white when I open file with command "less"

blah9234^Azzz123^A1=123
blah6344^Azzz123^A1=456
blah4555^Azzz123^A1=78912

I need convert it to:

zzz123^A1=123^Ablah9234
zzz123^A1=456^Ablah6344
zzz123^A1=78912^Ablah4555

so I need take first part

blah9234

add non-printable symbol ^A

^Ablah9234

and put it into the and of line

zzz123^A1=123^Ablah9234

to start I've tried to add ^A to the beginning of the line:

sed 's/^.*blah/^Ablah/' my.log > new.log

but it just adds '^' and 'A' and I got:

^Ablah9234^Azzz123^A1=123

and first ^A doesn't backlit white in less viewer.

Any advice's would be helpful. Thanks.

J. Allan
  • 1,418
  • 1
  • 12
  • 23
Trav Erse
  • 191
  • 2
  • 3
  • 12

1 Answers1

2

You can do this:

sed 's/^.*blah'$'\001''blah/' my.log > new.log

Or:

#get the value of the ^A control character
ctrlACode=$'\001'

#use it when we call sed
sed 's/^.*blah'"$ctrlACode"'blah/' my.log > new.log

To understand the second example, you need to understand this. Basically, $'\0xx' is equal to the control character that has that value in octal. For instance, $'\000' is the NUL control character. $'\001' is ^A, the SOH control character, etc.


An even easier way is found here.

In this way, you type ctrl-v then ctrl-a to insert ^A.

Community
  • 1
  • 1
J. Allan
  • 1,418
  • 1
  • 12
  • 23