2

Is it possible to use binary values in sed pattern matching?

I have a one line strings which contain plain text fields separated by binary value 1 as separator.

Is it possible to use sed to much everything up to binary separator 1?

Or should I use awk?

Example string where \x1 represents binary value 1:

key1=value1\x1key2=value2\x1key3=value3

Example expected output, values for key1 and key2:

value1 value2
stefanB
  • 77,323
  • 27
  • 116
  • 141
  • 1
    What exactly are you trying to do? It would be helpful to see some expected output. – Andrew Clark May 18 '11 at 23:29
  • I'm matching the key and outputting the value for specific key only, for example I would only want to see output "value1 value2" – stefanB May 18 '11 at 23:55

3 Answers3

4

edit: Here are a couple of options for printing the values based on a list of keys, couldn't figure out a more concise way with awk but one probably exists:

$ echo -e 'key1=value1\001key2=value2\001key3=value3' > test

$ sed 's/\x01/\n/g' test | awk -F= '{if ($1 == "key1" || $1 == "key2") print $2}'
value1
value2

$ sed 's/\x01/\n/g' test | perl -pe 's/((key1|key2)=(.*)|.*)/\3/'
value1
value2

You can't match everything up to the first \x1 since sed does not support non-greedy matching, your options are to use a different language, or something like the following:

$ sed 's/\x01/\n/g' test | head -n 1
key1=value1

The answer to the following question has a good example of using a Perl regex for non-greedy matches:
Non greedy regex matching in sed?

Community
  • 1
  • 1
Andrew Clark
  • 202,379
  • 35
  • 273
  • 306
  • correct I need non-greedy, it seems like I might get this done in C++ faster than anything else – stefanB May 19 '11 at 00:11
  • Edited my answer with some potential solutions for what you need to do, but doing this in C++ might be preferable if that is an option. – Andrew Clark May 19 '11 at 00:24
0

You have to find a way to get the \x1 in binary in the command there as sed doesn't parse it. For example to convert them all to new lines:

sed -e "s/$(echo -e \\001)/\n/g" filename
linuts
  • 6,608
  • 4
  • 35
  • 37
0

Type Control-A at the point where you want the character \001 to appear.

I would find this a lot easier than handling all the necessary escaping to get echo to produce the correct string if there are any backslashes in the regex - and I find there often are such backslashes.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278