我如何grep出直到特定模式的行

时间:2019-07-06 05:47:44

标签: bash awk grep

我需要从文件中提取出匹配某些参数的行,然后在该模式上方直到在行首找到“#”为止。

例如,这是文件,我需要过滤掉“ 00:21:44”及其上方的行,直到找到#。

有任何线索吗?

# CSV: Date,Score,URL,IP                                                             #
######################################################################################
"06-07-19 00:23:46","4.5","www.origina-l-diploms.com/wp-includes/fonts/mtbonline/update.htm","198.252.101.174"
"06-07-19 00:23:16","4.5","www.deehhayus.com/sqL/img/home/amazon","202.182.120.120"
"06-07-19 00:22:44","5.9","www.bricktechindia.in/fonts/www.bancoestado.cl/imagenes/comun2008/banca-en-linea-personas.html","43.255.154.40"
"06-07-19 00:22:14","3.7","www.bekenjekleurinstijl.nl/wp-admin/Alibaba/vqcr8bp0gud&lc=1033&id=64855&mkt=en-us&cbcxt=mai&snsc.php?email=nobody@mycraftmail.com","37.46.194.80"
"06-07-19 00:21:44","7.7","www.search-5.com/apx26e/verification/N76C72ED2B98CM9A99BC/qes.php","162.241.130.152"

1 个答案:

答案 0 :(得分:-1)

这个For example here is the file and I need filter out "00:21:44" and lines above that till # is found.我想过滤掉所有从#00:21:44的东西吗?

如果是这样,应该这样做:

示例数据:

"06-07-19 00:24:16","4.5","www.deehhayus.com/sqL/img/home/amazon","202.182.120.120"
# CSV: Date,Score,URL,IP                                                             #
######################################################################################
"06-07-19 00:23:46","4.5","www.origina-l-diploms.com/wp-includes/fonts/mtbonline/update.htm","198.252.101.174"
"06-07-19 00:23:16","4.5","www.deehhayus.com/sqL/img/home/amazon","202.182.120.120"
"06-07-19 00:22:44","5.9","www.bricktechindia.in/fonts/www.bancoestado.cl/imagenes/comun2008/banca-en-linea-personas.html","43.255.154.40"
"06-07-19 00:22:14","3.7","www.bekenjekleurinstijl.nl/wp-admin/Alibaba/vqcr8bp0gud&lc=1033&id=64855&mkt=en-us&cbcxt=mai&snsc.php?email=nobody@mycraftmail.com","37.46.194.80"
"06-07-19 00:21:44","7.7","www.search-5.com/apx26e/verification/N76C72ED2B98CM9A99BC/qes.php","162.241.130.152"
"06-07-19 00:20:16","4.5","www.deehhayus.com/sqL/img/home/amazon","202.182.120.120"

结果

awk '/#/{f=1;print} !f; /00:21:44/{f=0}' file
"06-07-19 00:24:16","4.5","www.deehhayus.com/sqL/img/home/amazon","202.182.120.120"
# CSV: Date,Score,URL,IP                                                             #
######################################################################################
"06-07-19 00:20:16","4.5","www.deehhayus.com/sqL/img/home/amazon","202.182.120.120"