根据IF条件读取多行

时间:2022-05-10 21:51:55

I have a very big file that contains these data :

我有一个包含这些数据的非常大的文件:

qry> /opt/ADL_db/Users/mkhalil/PipeLineWork/2-OutputPlatesTest/20150615_053914.455_0_Front.Frontview.png
cls> /opt/ADL_db/Users/mkhalil/PipeLineWork/1-OutputPlatesReference/20150612_055509.656_0_Front.Frontview.png       (99%)
cls> /opt/ADL_db/Users/mkhalil/PipeLineWork/1-OutputPlatesReference/20150612_083627.005_0_Front.Frontview.png       (99%)
cls> /opt/ADL_db/Users/mkhalil/PipeLineWork/1-OutputPlatesReference/20150612_054920.969_0_Front.Frontview.png       (99%)

qry> /opt/ADL_db/Users/mkhalil/PipeLineWork/2-OutputPlatesTest/20150615_054239.612_0_Front.Frontview.png
cls> /opt/ADL_db/Users/mkhalil/PipeLineWork/1-OutputPlatesReference/20150612_060212.816_0_Front.Frontview.png       (99%)
cls> /opt/ADL_db/Users/mkhalil/PipeLineWork/1-OutputPlatesReference/20150612_091652.202_0_Front.Frontview.png       (99%)
cls> /opt/ADL_db/Users/mkhalil/PipeLineWork/1-OutputPlatesReference/20150612_081529.893_0_Front.Frontview.png       (99%)
cls> /opt/ADL_db/Users/mkhalil/PipeLineWork/1-OutputPlatesReference/20150612_061203.680_0_Front.Frontview.png       (99%


qry> /opt/ADL_db/Users/mkhalil/PipeLineWork/2-OutputPlatesTest/20150615_054241.898_0_Front.Frontview.png
cls> /opt/ADL_db/Users/mkhalil/PipeLineWork/1-OutputPlatesReference/20150612_055047.746_0_Front.Frontview.png       (99%)
cls> /opt/ADL_db/Users/mkhalil/PipeLineWork/1-OutputPlatesReference/20150612_061414.016_0_Front.Frontview.png       (99%)
cls> /opt/ADL_db/Users/mkhalil/PipeLineWork/1-OutputPlatesReference/20150612_054643.282_0_Front.Frontview.png       (99%)
cls> /opt/ADL_db/Users/mkhalil/PipeLineWork/1-OutputPlatesReference/20150612_090622.440_0_Front.Frontview.png       (99%)
cls> /opt/ADL_db/Users/mkhalil/PipeLineWork/1-OutputPlatesReference/20150612_083110.342_0_Front.Frontview.png       (99%)

What I would like to read is just the qry> line and the first line that comes after it , i don't know how to do it using awk or sed or bash

我想读的只是qry> line和它之后的第一行,我不知道如何使用awk或sed或bash

2 个解决方案

#1


2  

You could get that line and the line after it with GNU grep with the -A flag

您可以使用带有-A标志的GNU grep获取该行和后面的行

grep -A1 'qry>' <filename>

which will match the line containing qry> and the -A flag will tell it to also grab 1 line after the match as well.

它将匹配包含qry>的行,并且-A标志也会告诉它在匹配后也抓住1行。

Or you could do it more POSIX-ly with sed like:

或者你可以用sed做更多的POSIX-ly:

sed -n '/qry>/ {p;n;p;}' <filename>

Here's how that works:

这是如何工作的:

  • -n tells sed not to print lines unless we explicitly do it
  • -n告诉sed不要打印行,除非我们明确地这样做
  • /qry>/ matches lines that contain that string
  • / qry> /匹配包含该字符串的行
  • {p;n;p;} prints the current line (the one matching qry>), go to the next line, then print that one too
  • {p; n; p;}打印当前行(匹配qry>的那一行),转到下一行,然后打印出那一行

To do it in pure bash so you can operate on the lines more easily you could do it like so

要用纯粹的bash来做,这样你就可以更容易地在线上操作了

while read -r cur; do
    if [[ "$cur" =~ 'qry>' ]]; then
        read -r result
        # Do something here with the query in $cur and the first line in $result
        printf "query line: %s\nnext line: %s\n" "$cur" "$result"
    fi
done < your_input_file

#2


0  

You can use awk:

你可以使用awk:

$ awk 'BEGIN{flag=0} flag{print;flag=0} /qry>/{print;flag=1}' <filename>

#1


2  

You could get that line and the line after it with GNU grep with the -A flag

您可以使用带有-A标志的GNU grep获取该行和后面的行

grep -A1 'qry>' <filename>

which will match the line containing qry> and the -A flag will tell it to also grab 1 line after the match as well.

它将匹配包含qry>的行,并且-A标志也会告诉它在匹配后也抓住1行。

Or you could do it more POSIX-ly with sed like:

或者你可以用sed做更多的POSIX-ly:

sed -n '/qry>/ {p;n;p;}' <filename>

Here's how that works:

这是如何工作的:

  • -n tells sed not to print lines unless we explicitly do it
  • -n告诉sed不要打印行,除非我们明确地这样做
  • /qry>/ matches lines that contain that string
  • / qry> /匹配包含该字符串的行
  • {p;n;p;} prints the current line (the one matching qry>), go to the next line, then print that one too
  • {p; n; p;}打印当前行(匹配qry>的那一行),转到下一行,然后打印出那一行

To do it in pure bash so you can operate on the lines more easily you could do it like so

要用纯粹的bash来做,这样你就可以更容易地在线上操作了

while read -r cur; do
    if [[ "$cur" =~ 'qry>' ]]; then
        read -r result
        # Do something here with the query in $cur and the first line in $result
        printf "query line: %s\nnext line: %s\n" "$cur" "$result"
    fi
done < your_input_file

#2


0  

You can use awk:

你可以使用awk:

$ awk 'BEGIN{flag=0} flag{print;flag=0} /qry>/{print;flag=1}' <filename>