Print lines after regex

Hello,

I need to print four lines inmediatly after the regexp, but not the line containing the regexp. The print should show the four lines together in one.

Thanks!

Please provide sample input, the regex and the code you have written even if it does not work properly. What do you mean by "show the four lines together in one"?

Hello,
this is part of the file:

 ORGANISM  Lates calcarifer
            Eukaryota; Metazoa; Chordata; Craniata; Vertebrata; Euteleostomi;
            Actinopterygii; Neopterygii; Teleostei; Euteleostei; Neoteleostei;
            Acanthomorpha; Acanthopterygii; Percomorpha; Perciformes;
            Percoidei; Latidae; Lates.

I want to print up to the end (... ; Lates.) in one line I tried using sed:

sed -n '/ORGANISM/{n;1-4;p}' still producing only the first line.

This is what I would like to obtain:

Eukaryota; Metazoa; Chordata; Craniata; Vertebrata; Euteleostomi; Actinopterygii; Neopterygii; Teleostei; Euteleostei; Neoteleostei; Acanthomorpha; Acanthopterygii; Percomorpha; Perciformes; Percoidei; Latidae; Lates.

One line without hard returns for each line.

This should do (didn't test it though):

perl -n0e 'while (/ORGANISM.*?\./sg){$&=~s/\n/ /g;print "$&\n"}' file

Hello,
I tried but it didn't work. This is what I got:

Modification of a read-only value attempted at -e line 1, <> chunk 1.

this might do it.

awk '
   /ORGANISM/{found=1;next}

   {
      if(found>=1 && found<=4) {  
         printf("%s",$0)
      }
      found++
   }

   END {printf "\n"}
' foo
sed -n '/ORGANISM/{n;p;n;p;n;p;n;p}'|tr -d '\n' && echo

OR(can be not work)

sed -n '/ORGANIZM/{n;p;n;p;n;p;n;p}'| sed -n 's/\n//gp'

Try:

perl -n0e 'while (/ORGANISM.*?\./sg){$s=$&;$s=~s/\n/ /g;print "$s\n"}' file
1 Like
sed -n '/ORGANISM/{n;N;N;N;s/[ \n]//g;s/;/; /g;p;}'
$ cat data
 ORGANISM  Lates calcarifer
            Eukaryota; Metazoa; Chordata; Craniata; Vertebrata; Euteleostomi;
            Actinopterygii; Neopterygii; Teleostei; Euteleostei; Neoteleostei;
            Acanthomorpha; Acanthopterygii; Percomorpha; Perciformes;
            Percoidei; Latidae; Lates.
$ sed -n '/ORGANISM/{n;N;N;N;s/[ \n]//g;s/;/; /g;p;}' data
Eukaryota; Metazoa; Chordata; Craniata; Vertebrata; Euteleostomi; Actinopterygii; Neopterygii; Teleostei; Euteleostei; Neoteleostei; Acanthomorpha; Acanthopterygii; Percomorpha; Perciformes; Percoidei; Latidae; Lates.

Regards,
Alister

---------- Post updated at 03:29 PM ---------- Previous update was at 03:22 PM ----------

With readable formatting:

sed -n ' 
    /ORGANISM/ {
        n; N; N; N 
        s/[ \n]//g
        s/;/; /g
        p
    }' data
1 Like

It works too:

sed -n '/ORGANISM/{n;N;N;N;s/\n/ /gp}'

readable format:

sed -n '/ORGANISM/ {
                                       n;N;N;N
                                       s/\n/ /gp        }'
1 Like

thanks for your quick answer.

Still doesn't produce what I need. I have 725 records, in each record contein the match ORGANISM with 4 lines on each. I would like to have at the end 725 lines with this four lines in one

example:
if I use

grep -A 4 'ORGANISM' 

I got :

  ORGANISM  Ventrifossa garmani
            Eukaryota; Metazoa; Chordata; Craniata; Vertebrata; Euteleostomi;
            Actinopterygii; Neopterygii; Teleostei; Euteleostei; Neoteleostei;
            Acanthomorpha; Paracanthopterygii; Gadiformes; Macrouridae;
            Macourinae; Ventrifossa.
  ORGANISM  Bathygadus antrodes
            Eukaryota; Metazoa; Chordata; Craniata; Vertebrata; Euteleostomi;
            Actinopterygii; Neopterygii; Teleostei; Euteleostei; Neoteleostei;
            Acanthomorpha; Paracanthopterygii; Gadiformes; Macrouridae;
            Bathygadinae; Bathygadus.

and what I need is this:

ORGANISM  Ventrifossa garmani Eukaryota; Metazoa; Chordata; Craniata Actinopterygii; Neopterygii; Teleostei Acanthomorpha; Paracanthopterygii; Gad Macourinae; Ventrifossa.
ORGANISM  Bathygadus antrodes Eukaryota; Metazoa; Chordata; Craniata Actinopterygii; Neopterygii; Teleostei Acanthomorpha; Paracanthopterygii; Gad Bathygadinae; Bathygadus.

---------- Post updated at 01:28 PM ---------- Previous update was at 01:13 PM ----------

[/COLOR]Thanks Alister! It worked

Now,it's new homework?

thanks... problem solve

Hello,

I tried this command and it worked fine.
My question is that how can we replace the N by a variable, to print for instance a big number of lines. It means if I want 100 lines after an expression, to not put N 100 time in the sed.

$ sed -n '/aaa/{n;N;N;s/[ \n]//g;s/;/; /g;p;}' ttt2
1
2
3
 
$ cat ttt2
0
aaa
1
2
3
4
5
6
7
8
9
10

Thx & Regs,
Rany.