Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using sed, how does one match square brackets in a character class?

Tags:

sed

Here's a chunk of the raw data:

00000000  54 6f 70 69 63 20 46 6f  72 75 6d 20 52 65 70 6c  |Topic Forum Repl|
00000010  69 65 73 20 4c 61 73 74  20 70 6f 73 74 20 31 20  |ies Last post 1 |
00000020  4c 69 6e 75 78 20 54 6f  64 61 79 20 31 34 3a 34  |Linux Today 14:4|
00000030  36 3a 35 37 20 62 79 20  4c 69 6e 75 78 20 4f 75  |6:57 by Linux Ou|
00000040  74 6c 61 77 73 20 32 36  39 20 e2 80 93 20 53 6f  |tlaws 269 ... So|
00000050  6d 65 6f 6e 65 20 4b 6c  6f 73 65 20 54 68 61 74  |meone Klose That|
00000060  20 4f 75 74 6c 61 77 73  20 32 38 20 73 79 73 79  | Outlaws 28 sysy|
00000070  70 68 75 73 2e 6a 6f 6e  65 73 20 48 6f 6c 65 20  |phus.jones Hole |
00000080  62 79 20 59 4f 42 41 20  5b 20 31 20 32 20 5d 20  |by YOBA [ 1 2 ] |
00000090  32 20 4c 69 6e 75 78 20  26 20 54 6f 64 61 79 20  |2 Linux & Today |
000000a0  31 31 3a 34 34 3a 35 31  20 62 79 20 4c 6f 6f 6b  |11:44:51 by Look|
000000b0  73 20 6c 69 6b 65 20 43  61 6e 6f 6e 69 63 61 6c  |s like Canonical|
000000c0  20 69 73 20 61 6e 6e 6f  75 63 69 6e 67 20 70 6c  | is annoucing pl|
000000d0  61 6e 73 20 46 72 65 65  64 6f 6d 20 31 20 6b 72  |ans Freedom 1 kr|

It's a hex dump and I'm interested in isolating the text part. Here's a sed expression that almost works:

$ sed 's/.* |\([a-zA-Z0-9:& \.]*\)|$/\1/g' hex.dat 
Topic Forum Repl
ies Last post 1 
Linux Today 14:4
6:57 by Linux Ou
tlaws 269 ... So
meone Klose That
 Outlaws 28 sysy
phus.jones Hole 
00000080  62 79 20 59 4f 42 41 20  5b 20 31 20 32 20 5d 20  |by YOBA [ 1 2 ] |
2 Linux & Today 
11:44:51 by Look
s like Canonical
 is annoucing pl
ans Freedom 1 kr

Almost. But how to filter that last line though?

$ sed 's/.* |\([a-zA-Z0-9:&\[\] \.]*\)|$/\1/g' hex.dat 

And:

    $ sed 's/.* |\([a-zA-Z0-9:&\\[\\] \.]*\)|$/\1/g' hex.dat 

Don't work at all (they fail to translate anything).

And:

$ sed 's/.* |\([a-zA-Z0-9:&[] \.]*\)|$/\1/g' hex.dat 

obviously can't work.

Thanks for any help.

like image 212
Thorsen Avatar asked Jun 26 '12 19:06

Thorsen


1 Answers

You almost had it.

Look at this section of a Unix regular expressions tutorial.

The way that yours could be done is by placing ][ immediately after you begin your character class.

So, try sed 's/.* |\([][a-zA-Z0-9:& \.]*\)|$/\1/g' hex.dat

For clarification, it does not matter where in the character class the [ is, so long as the closing bracket you intend to include in your character class (]) immediately follows the opening of your character class.

Also, as a further edit, try typing man cut and using what Tomasz said in a comment.

cut -d='|' -f2 hex.dat will cut your file, delimiting on a pipe, and take the second field.

like image 113
twmb Avatar answered Oct 06 '22 15:10

twmb