avatar
Usage of Grep???help!!!# Programming - 葵花宝典
s*f
1
I have data files with format like this:
year,mon,day,data.....
All are numbers and are seperated by coma. Can someone give me some suggestion
how I can use grep to extract the data at certain month, say, March-May.
I tried :
grep "^[[:digit:]\{\4\}[, ][3-5]]" inputfile >outputfile
It doesn't work.
Thanks!!!
avatar
a*n
2
awk -F, '{if($2==4 || $2==5) print}' inputfile > outputfile

【在 s*******f 的大作中提到】
: I have data files with format like this:
: year,mon,day,data.....
: All are numbers and are seperated by coma. Can someone give me some suggestion
: how I can use grep to extract the data at certain month, say, March-May.
: I tried :
: grep "^[[:digit:]\{\4\}[, ][3-5]]" inputfile >outputfile
: It doesn't work.
: Thanks!!!

avatar
c*t
3
egrep "March|May" ..?

【在 s*******f 的大作中提到】
: I have data files with format like this:
: year,mon,day,data.....
: All are numbers and are seperated by coma. Can someone give me some suggestion
: how I can use grep to extract the data at certain month, say, March-May.
: I tried :
: grep "^[[:digit:]\{\4\}[, ][3-5]]" inputfile >outputfile
: It doesn't work.
: Thanks!!!

avatar
a*n
4
A perl solution:
#!/usr/bin/perl
# To run: perl test.pl inputfile
while(<>){
my @line = split(",",$_);
print $_ if($line[1]==3 || $line[1]==4 || $line[1]==5);
}

【在 s*******f 的大作中提到】
: I have data files with format like this:
: year,mon,day,data.....
: All are numbers and are seperated by coma. Can someone give me some suggestion
: how I can use grep to extract the data at certain month, say, March-May.
: I tried :
: grep "^[[:digit:]\{\4\}[, ][3-5]]" inputfile >outputfile
: It doesn't work.
: Thanks!!!

avatar
i*h
5
What do you mean it doesn't work? No match or too many matches?
1. Why do you need a space in [, ], the format does not imply that.
2. The outside bracket is not necessary.
3. And you don't need another "\" before "4".
Try
^[:digit:]\{4\},[3-5]

【在 s*******f 的大作中提到】
: I have data files with format like this:
: year,mon,day,data.....
: All are numbers and are seperated by coma. Can someone give me some suggestion
: how I can use grep to extract the data at certain month, say, March-May.
: I tried :
: grep "^[[:digit:]\{\4\}[, ][3-5]]" inputfile >outputfile
: It doesn't work.
: Thanks!!!

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。