2010-11-01 49 views
1

样品提取的字符串值:A.TXT用grep

Reading:RG1:+ /user/reading/Monday:12 
Reading:RG1:- /user/**/Friday:12 
Reading:RG1:- /user/**/*.txt:12 
Reading:RG1:- /user/tet/**/*.txt:12 

我期待提取使用字符串

after + or - what ever the string i want it 

cat a.txt | grep RG1|grep '+'| cut -d':' -f3| cut -d'+' -f2 |sed -e 's/ // 

我越来越 /用户/阅读/星期一

但我amlooking

/用户/阅读/周一:12

+0

我认为你接受的答案不适用于包含' - '情况的字符串。因为你只切割'+'':'only(not' - ') – Alam 2010-11-01 11:29:30

+0

我有相同的代码 - 也..它的工作.. – Tree 2010-11-01 11:57:31

回答

1

要修复您的命令,请使用-f3-,因为您需要从第3个字段到行尾的所有内容。

cat a.txt | grep RG1|grep '+'| cut -d':' -f3-| cut -d'+' -f2 |sed -e 's/ //' 
+0

sed -e'/ //'的用途是什么? – Alam 2010-11-01 11:18:14

+1

不起作用,只匹配'/ user/reading/Monday:12'。 – 2010-11-01 11:31:16

+1

这就是OP想要的。从问题:'我得到/用户/阅读/星期一但我正在看/用户/阅读/星期一:12'。我只是为他解决了他的命令。 – dogbane 2010-11-01 11:34:55

3

使用egrep -o

$ egrep -o '/user/reading/[A-Z][a-z]+day:[0-9]+' a.txt 
/user/reading/Monday:12 
/user/reading/Friday:12 

编辑:你的新的例子,使用类似

$ egrep -o '/user/[^ ]*:[0-9]+' a.txt 
/user/reading/Monday:12 
/user/**/Friday:12 
/user/**/*.txt:12 
/user/tet/**/*.txt:12 

假设你的路径中没有空格。

+0

我不知道这个路径所有的时间..像这样:/ user /阅读/。它不会是所有时间相同的格式是这样的..有些时候它可能是正则表达式也 – Tree 2010-11-01 11:01:17

+0

如果它并不总是这种格式,那么请张贴它看起来像。 – 2010-11-01 11:02:59

+0

done .............................. – Tree 2010-11-01 11:09:34

1
$ grep -Po '(?<=[-+]).*' a.txt 
/user/reading/Monday:12 
/user/**/Friday:12 
/user/**/*.txt:12 
/user/tet/**/*.txt:12 

变化与方括号中的字符改变你选择哪条线路。