Print lines after regex


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print lines after regex
# 1  
Old 09-18-2010
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!
# 2  
Old 09-18-2010
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"?
# 3  
Old 09-18-2010
Hello,
this is part of the file:

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

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

This is what I would like to obtain:

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

Last edited by Scott; 09-18-2010 at 03:29 PM.. Reason: Please use CODE tags
# 4  
Old 09-18-2010
This should do (didn't test it though):
Code:
perl -n0e 'while (/ORGANISM.*?\./sg){$&=~s/\n/ /g;print "$&\n"}' file

# 5  
Old 09-18-2010
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.
# 6  
Old 09-18-2010
this might do it.

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

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

   END {printf "\n"}
' foo

# 7  
Old 09-18-2010
Code:
sed -n '/ORGANISM/{n;p;n;p;n;p;n;p}'|tr -d '\n' && echo

OR(can be not work)
Code:
sed -n '/ORGANIZM/{n;p;n;p;n;p;n;p}'| sed -n 's/\n//gp'


Last edited by Scott; 09-18-2010 at 05:41 PM.. Reason: Fixed code tags
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