使用 sed,如何匹配字符类中的方括号? [英] using sed, how does one match square brackets in a character class?

查看:80
本文介绍了使用 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|

这是一个十六进制转储,我有兴趣隔离文本部分.这是一个几乎有效的 sed 表达式:

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 

还有:

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

根本不工作(他们无法翻译任何内容).

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

还有:

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

显然不行.

感谢您的帮助.

推荐答案

您几乎拥有了它.

查看 Unix 正则表达式教程的这一部分.

你的方法是在你开始你的角色课程后立即放置 ][ .

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

所以,试试 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.

此外,作为进一步编辑,尝试输入 man cut 并使用 Tomasz 在评论中所说的内容.

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

cut -d='|'-f2 hex.dat 将剪切您的文件,在管道上分隔,并获取第二个字段.

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

这篇关于使用 sed,如何匹配字符类中的方括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆