Print lines after regex


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print lines after regex
# 8  
Old 09-18-2010
Try:
Code:
perl -n0e 'while (/ORGANISM.*?\./sg){$s=$&;$s=~s/\n/ /g;print "$s\n"}' file

This User Gave Thanks to bartus11 For This Post:
# 9  
Old 09-18-2010
Code:
sed -n '/ORGANISM/{n;N;N;N;s/[ \n]//g;s/;/; /g;p;}'

Code:
$ 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:
Code:
sed -n ' 
    /ORGANISM/ {
        n; N; N; N 
        s/[ \n]//g
        s/;/; /g
        p
    }' data

This User Gave Thanks to alister For This Post:
# 10  
Old 09-18-2010
It works too:
Code:
sed -n '/ORGANISM/{n;N;N;N;s/\n/ /gp}'

readable format:
Code:
sed -n '/ORGANISM/ {
                                       n;N;N;N
                                       s/\n/ /gp        }'


Last edited by john1212; 09-18-2010 at 06:42 PM..
This User Gave Thanks to john1212 For This Post:
# 11  
Old 09-18-2010
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

Code:
grep -A 4 'ORGANISM'

I got :

Code:
  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:
Code:
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 ----------

Thanks Alister! It worked

Last edited by Scott; 09-18-2010 at 05:43 PM.. Reason: Code tags, please...
# 12  
Old 09-18-2010
Now,it's new homework?
# 13  
Old 09-18-2010
thanks... problem solve
# 14  
Old 01-05-2011
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.

Code:
$ 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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Print number of lines for files in directory, also print number of unique lines

I have a directory of files, I can show the number of lines in each file and order them from lowest to highest with: wc -l *|sort 15263 Image.txt 16401 reference.txt 40459 richtexteditor.txt How can I also print the number of unique lines in each file? 15263 1401 Image.txt 16401... (15 Replies)
Discussion started by: spacegoose
15 Replies

2. Shell Programming and Scripting

(n)awk: print regex search output lines in one line

Hello. I have been looking high and low for the solution for this. I seems there should be a simple answer, but alas. I have a big xml file, and I need to extract certain information from specific items. The information I need can be found between a specific set of tags. let's call them... (2 Replies)
Discussion started by: Tobias-Reiper
2 Replies

3. Shell Programming and Scripting

To print lines between 2 timestamps using awk|sed and regex

Hi, I am using the following code to fetch lines that are generated in last 1 hr . Hence, I am using date function to calculate -last 1 hr & the current hr and then somehow use awk (or sed-if someone could guide me better) with some regex pattern. dt_1=`date +%h" "%d", "%Y\ %l -d "1 hour... (10 Replies)
Discussion started by: sarah-alikhan31
10 Replies

4. Shell Programming and Scripting

Print lines that match regex on xth string

Hello, I need an awk command to print only the lines that match regex on xth field from file. For example if I use this command awk -F"|" ' $22 == "20130117090000.*" 'It wont work, I think, because single quotes wont allow the usage of the metacharacter star * . On the other hand I dont know... (2 Replies)
Discussion started by: black_fender
2 Replies

5. UNIX for Dummies Questions & Answers

read regex from ID file, print regex and line below from source file

I have a file of protein sequences with headers (my source file). Based on a list of IDs (which are included in some of the headers), I'd like to print out only the specified sequences, with only the ID as header. In other words, I'd like to search source.txt for the terms in IDs.txt, and print... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

6. Shell Programming and Scripting

Nawk - print only a string containing a regex.

Hi again, I'm looking for some help with nawk, I can print a line which has a regex match in it from a file using /pattern/ but I'm looking for a way to only print the $tring which contains the pattern, rather than the whole line. This $tring may be of variable length, may occur at any point... (1 Reply)
Discussion started by: spynappels
1 Replies

7. Shell Programming and Scripting

print first few lines, then apply regex on a specific column to print results.

abc.dat tty cpu tin tout us sy wt id 0 0 7 3 19 71 extended device statistics r/s w/s kr/s kw/s wait actv wsvc_t asvc_t %w %b device 0.0 133.2 0.0 682.9 0.0 1.0 0.0 7.2 0 79 c1t0d0 0.2 180.4 0.1 5471.2 3.0 2.8 16.4 15.6 15 52 aaaaaa1-xx I want to skip first 5 line... (4 Replies)
Discussion started by: kchinnam
4 Replies

8. Shell Programming and Scripting

print lines AFTER lines cointaining a regexp (or print every first and fourth line)

Hi all, This should be very easy but I can't figure it out... I have a file that looks like this: @SRR057408.1 FW8Y5CK02R652T length=34 AGCAGTGGTATCAACGCAGAGTAAGCAGTGGTAT +SRR057408.1 FW8Y5CK02R652T length=34 FIIHFF6666?=:88@@@BBD:::?@ABBAAA>8 @SRR057408.2 FW8Y5CK02TBMHV length=52... (1 Reply)
Discussion started by: kmkocot
1 Replies

9. Shell Programming and Scripting

print last five characters with PERL regex

greetings citizens of Unix.com I am perplexed with an issue. The issue is trying to print the last 5 characters of a string in PERL. Below are demonstrated my daft attempts at performing the forementioned task. $row =~ m/^.*(.....)\s$/; $row =~ m/\w{5}\s*$/i;$row =~... (3 Replies)
Discussion started by: simply seth
3 Replies

10. Shell Programming and Scripting

sed - print only matching regex

Hi folks, Lets say I have the following text file: name, lastname, 1234, name.lastname@test.com name1, lastname1, name2.lastname2@test.com, 2345 name, 3456, lastname, name3.lastname3@test.com 4567, name, lastname, name4.lastname4@test.com I now need the following output: 1234... (5 Replies)
Discussion started by: domi55
5 Replies
Login or Register to Ask a Question