0

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

jijinp
  • 2,592
  • 1
  • 13
  • 15
M.Cesar
  • 89
  • 8

2 Answers2

2

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

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.

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

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

Here's how that works:

  • -n tells sed not to print lines unless we explicitly do it
  • /qry>/ matches lines that contain that string
  • {p;n;p;} prints the current line (the one matching qry>), go to the next line, then print that one too

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

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
Eric Renouf
  • 13,950
  • 3
  • 45
  • 67
  • 1
    sed can be more succinct. `sed '/qry>/!d;N' file` – 123 Apr 25 '16 at 14:43
  • @M.Cesar what is "this" that you want to read? Do you want to get the full output of all the lines together as a single string in your variable, or did you want to get each pair in a variable to operate on separately? – Eric Renouf Apr 26 '16 at 12:03
  • each pair to operate separately – M.Cesar Apr 26 '16 at 13:20
  • @M.Cesar see my update for a bash version that will give you each line in a variable that you can operate on – Eric Renouf Apr 26 '16 at 13:57
0

You can use awk:

$ awk 'BEGIN{flag=0} flag{print;flag=0} /qry>/{print;flag=1}' <filename>
Ren
  • 2,852
  • 2
  • 23
  • 45