2012-03-13 18 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