2012-03-13 82 views
0

我有4列CSV文件:印刷线

01, cat, animal, it catches mice 
  • 该文件包含来自UTF-各种语言的字符8。

如何在第2列中只打印包含正好2个字符的行,同时还与第4列中该行上任何位置的模式“/ to”匹配?

回答

1

您可以使用AWK:

$ cat /tmp/l 
01, cat, animal, it catches mice 
02, ok, aaa, e/tomos 
03, bad, qux, vb/tomos 

$ awk -F"," 'length($2) == 3 && $4 ~ /\057to/' /tmp/l 
02, ok, aaa, e/atmos 
+0

这给出:'bash:$:command not found'。 – Village 2012-03-13 01:37:38

+2

'$'不是键入的,它表示shell提示符 – 2012-03-13 01:39:15

2

试试这个:

egrep "[^,]+,\s+[^,]{2},|([^,]+,\s+){3}.*/to.*" your_file 

尝试使用这个文件:

01, cat, animal, it catches mice 
01, ab, animal, it catches/o mice 
01, ca, animal, it catches/to mice 
01, cat, animal, it catches m/toice 

,并返回:

01, ab, animal, it catches/o mice 
01, ca, animal, it catches/to mice 
01, cat, animal, it catches m/toice 
+0

结果输出似乎只打印部分行。我有'egrep'版本2.6.3。 – Village 2012-03-13 01:09:00

+0

你能发布你的结果吗?我在Fedora 15中使用grep 2.9 – PasteBT 2012-03-13 01:13:38

+0

我发现我的错误。如果我的CSV使用替代符号,我需要替换哪些逗号? – Village 2012-03-13 01:17:11

1

这可能适合你:

sed '/^[^,]*,\s*..,[^,]*,.*\/to/!d' file