在shell中,如何处理这一行,以便提取我想要的文件

在shell中,如何处理这一行,以便提取我想要的文件,shell,Shell,我在平面文件中有几行。以2行为例: 1 aa bb 05 may 2014 cc G 14-MAY-2014 hello world j sd az 20140505 sd G 14-MAY-2014 hello world haha 也许你已经注意到了,我既不能计算字符的数量,也不能计算空格的数量,因为行没有很好地对齐,第四个字段,有时像20140505,有时像05 may 2014。因此,我想要的是尝试匹配G,或者匹配2014年5月14日的。然后我可以很容易地获得以下字段:he

我在平面文件中有几行。以2行为例:

1 aa bb 05 may 2014 cc G 14-MAY-2014 hello world
j  sd  az 20140505    sd  G 14-MAY-2014 hello world haha

也许你已经注意到了,我既不能计算字符的数量,也不能计算空格的数量,因为行没有很好地对齐,第四个字段,有时像
20140505
,有时像
05 may 2014
。因此,我想要的是尝试匹配
G
,或者匹配
2014年5月14日的
。然后我可以很容易地获得以下字段:
helloworld
helloworld haha
。有人能帮我吗?谢谢大家!

假设您的行位于名为
test.txt的文件中:

 cat test.txt | sed -r 's/^.*-[0-9]{4}\s//'
这是在Linux系统上使用GNU。还有很多其他的方法。在这里,我只是简单地删除了行开始日期之前的所有内容

sed -r 's/^.*-[0-9]{4}\s//'

-r = extendes reg ex, makes things like the quantor {4} possible
's/ ... //' = s is for substitute, 
              it matches the first part and replaces it with the second.
              since the resocond part is empty, it's a remove/delete
^  = start of line
.* = any character, any number of times
-[0-9]{4} = a dash, followed by four digits ([0-9]), the year part of the date
\s = any white space

您可以使用perl的lookbehind regex:

perl -lne '/(?<=14-MAY-2014)(.*)/ && print $1' file

perl-lne'/(?现在我使用echo${ligne##*G}来获取'G'之后的字段。但是这个字段可以是'C'或'G'。那么如何像echo${ligne##*(G | C)}@ZHE.ZHAO答案被扩展为包括一些解释。很抱歉,这不起作用,我是shell的一名新生。假设现在我已经在变量$line中有了这一行……我尝试匹配“G”,并切断了以下字段
grep -Po '(?<=14-MAY-2014)(.*)'  file