为什么我的 sed 搜索在字段中找不到 alpha? [英] Why does my sed search not find alpha in field?
问题描述
我想知道为什么 myfile.sed 中的搜索条件不会过滤.我只想过滤第一列中的任何 alpha.那就是我想在行首后过滤任何字母,后跟 "
字符并以 "
字符和 ,
字符结尾,但我在后续列中获取数据:
I want to know why the search criteria in myfile.sed won't filter. I want to filter any alpha within the first column only. That is I want to filter any alpha after the start of line, followed by a "
chracter and ending with a "
character and a ,
character, but I am getting data in subsequent columns:
我正在使用
sed -f myfile.sed in-file
myfile.sed 的内容如下
The contents of myfile.sed looks like
{
/^"[0-9]+[^0-9]+[0-9]*",/p
/^"[0-9]+",""/p
}
包含我要过滤的数据的文件内的前几个字段
The first few fields of in-file containing the data I want to filter
"1866TL","1866TL","ME","SUBA","GRY","B","25","40","LAKEHILL AVE"
我有兴趣打印上面的行,因为非数字 -- TL -- 出现在第一列中.
I'm interested in printing the line above, because a non-numeric -- TL -- appears in the first column.
为了完成这项工作,我尝试将 "
字符替换为 [^0-9A-Za-z]
.
To make this work, I have tried substituting the "
character with [^0-9A-Za-z]
.
我曾尝试只搜索几个数字后跟一个字符,但无法实现.
I have tried just searching for a few numbers followed by one character, but cannot make this work.
我做错了什么?
从@choroba 的回答中更正 myfile.sed 后看起来像这样:
After correcting myfile.sed to look like this from @choroba 's answer:
{
/^"[0-9]\+[^0-9]\+[0-9]*",/p
/^"[0-9]\+",""/p
}
我正在获取我不打算获取的其他字段.如何在逗号后切断搜索?
I'm getting other fields I did not intend to get. How do I cut off the search after the comma?
"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.",
推荐答案
Plus 必须反斜杠才能有特殊含义:
Plus must be backslashed to have the special meaning:
/^"[0-9]\+[^0-9]\+[0-9]*",/p
如果您不希望否定字符类匹配逗号和双引号,请包括它们:
If you do not want the negated character class to match commas and double quotes, include them:
/^"[0-9]\+[^0-9",]\+[0-9]*",/p
这篇关于为什么我的 sed 搜索在字段中找不到 alpha?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!