我有一个看起来像这样的文件
the vote will take place tomorrow at card (0.046786214525982)
vote will take place tomorrow at card noon (0.027725129127836)
vote will take place tomorrow at card am (0.024548598987843)
我想只提取每一行的这一部分
the vote will take place tomorrow at card
vote will take place tomorrow at card noon
vote will take place tomorrow at card am
我的Perl脚本就是这个
perl -ne 'print [split /(/]->[0]' 8-8TRI.txt
但它不起作用
它说
Unmatched ( in regex; marked by <-- HERE in m/( <-- HERE / at -e line 1.
答案 0 :(得分:2)
perl -ne 'print [ split /\(/ ]->[0]' 8-8TRI.txt
^__ backslash for '('
您不需要数组引用,所以
perl -ne 'print( (split /\(/ )[0] )' 8-8TRI.txt
答案 1 :(得分:1)
split /(/
中的正则表达式无效,因为(
是正则表达式中的特殊元字符,需要相应的结束)
才能使用它。
您可以使用反斜杠\(
来转义左括号,但如果使用单行命令,shell也会使用反斜杠,因此使用字符类[(]
会更清楚。
喜欢这个
perl -l -pe "s/[(].+//" myfile
这使用替换s///
来删除第一个左括号及其后面的所有内容。
请注意,它不会在左括号之前删除任何空格或制表符,因此输出将包含不可见的尾随空格字符
the vote will take place tomorrow at card
vote will take place tomorrow at card noon
vote will take place tomorrow at card am
答案 2 :(得分:0)
您可以像这样使用perl:
perl -pe 's/^([^(]*).*$/\1/' file
the vote will take place tomorrow at card
vote will take place tomorrow at card noon
vote will take place tomorrow at card am