0

我想知道为什么 myfile.sed 中的搜索条件不会过滤。我只想过滤第一列中的任何 alpha。那就是我想在行首之后过滤任何 alpha,然后是一个字符并以一个字符和一个字符"结尾,但是我在后续列中获取数据:",

我在用

sed -f myfile.sed in-file

myfile.sed 的内容看起来像

{
        /^"[0-9]+[^0-9]+[0-9]*",/p
        /^"[0-9]+",""/p
}

包含我要过滤的数据的文件内的前几个字段

"1866TL","1866TL","ME","SUBA","GRY","B","25","40","LAKEHILL AVE"

我有兴趣打印上面的行,因为非数字——TL——出现在第一列。

为了完成这项工作,我尝试将"角色替换为[^0-9A-Za-z].

我试过只搜索几个数字后跟一个字符,但无法完成这项工作。

我究竟做错了什么?

从@choroba 的回答中将 myfile.sed 更正为如下所示:

{
        /^"[0-9]\+[^0-9]\+[0-9]*",/p
        /^"[0-9]\+",""/p
}

我正在获得其他我不打算获得的领域。如何切断逗号后的搜索?

"940915","L33677","MA","CHEV","RED","R","25","54","GROVE ST",
"1866TL","1866TL","ME","SUBA","GRY","B","25","40","LAKEHILL AVE",
"966011","1942758","NH","AUDI","BLU","","25","13","MT. VERNON ST.",
4

2 回答 2

2

加号必须加反斜杠以具有特殊含义:

/^"[0-9]\+[^0-9]\+[0-9]*",/p

如果您不希望否定字符类匹配逗号和双引号,请包括它们:

/^"[0-9]\+[^0-9",]\+[0-9]*",/p
于 2013-07-09T15:39:40.050 回答
0

sed 是用于在单行上进行简单替换的出色工具,其他任何事情只需使用 awk:

$ cat file
"940915","L33677","MA","CHEV","RED","R","25","54","GROVE ST",
"1866TL","1866TL","ME","SUBA","GRY","B","25","40","LAKEHILL AVE",
"966011","1942758","NH","AUDI","BLU","","25","13","MT. VERNON ST.",

$ awk -F, '$1 ~ /[[:alpha:]]/' file
"1866TL","1866TL","ME","SUBA","GRY","B","25","40","LAKEHILL AVE",

再简单不过了,对吧?

FWIW 这是 sed 等价物:

$ sed -n '/^[^,]*[[:alpha:]]/p' file
"1866TL","1866TL","ME","SUBA","GRY","B","25","40","LAKEHILL AVE",
于 2013-07-09T16:08:24.623 回答