Greping next line after grep match


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Greping next line after grep match
# 1  
Old 04-05-2011
MySQL Greping next line after grep match

Hi All
I have a file with tha same line multiple times and its easy to grep out those lines using
Code:
grep "pattern" file

although I need to know exactly what the next line after those lines are
Can anyone please shed some light on this on how i can simultaneously grep the pattern and the next line which follows the pattern matching line?
Cheers Smilie
# 2  
Old 04-05-2011
Try:
Code:
grep -A1 "pattern" file

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 04-05-2011
Bartus ... I cant stop saying thanks to you ... thats exactly what I wanted ... and I hv more questions coming on this thread ... the other one is rightfully closed ...was getting too long Smilie
Hv a great day !!
# 4  
Old 04-05-2011
of if your grep version doesn't support -A:

Code:
#  awk '/match this/ { print;getline;print}' infile
match this pattern
and print next line

HTH
This User Gave Thanks to Tytalus For This Post:
# 5  
Old 04-05-2011
Both of them worked and produced desirable results
Cheers Smilie

---------- Post updated at 08:06 AM ---------- Previous update was at 05:50 AM ----------

@Bartus
Hi Bartus I noticed that the code you provided here https://www.unix.com/302510509-post113.html is giving me unexpected result for the following sample file:
Code:
SK1.scplasm1
Code:
5057-5146
Ref:
AGAAATGCAACGCGAGAGCGCTATTTTACCAACGAAGAATCTGTGCTTCATTTTTGTAAAACAAAAATGCAACGCGAGAGCGCTAATTTT

Gen:
AGAAATGCAACGCGAGAGCGCTATTTTACCAACGAAGAATCTGTGCTTCATTTTTGTAAAACAAAAATGCAANNCGAGAGCGCTNNTTTT

SK1.scplasm1
2903-2922
Ref:
CCTTGAACTTGCATCTAAAC

Gen:
CCTTGAACTTGCATCTAAAC
I get result
Code:
5056 N's and then AGAAATGCAACGCGAGAGCGCTATTTTACCAACGAAGAATCTGTGCTTCATTTTTGTAAAACAAAAATGCAANNCGAGAGCGCTNNTTTT(which is Gen:5057-5146) and nothing else
However my expected output is
Code:
2092 N's (to fill gap from 1-2092) then CCTTGAACTTGCATCTAAAC(which is Gen:2903-2922) then 2135 N's to fill the gap from 2923-5056 then AGAAATGCAACGCGAGAGCGCTATTTTACCAACGAAGAATCTGTGCTTCATTTTTGTAAAACAAAAATGCAACGCGAGAGCGCTAATTTT (which is Gen:5057-5146)
and so on with the rest of the file. Can you help me figure out whats not working in the code or how it can be changed? Cheers and thanks in advance:) Good day!! ---------- Post updated at 08:14 AM ---------- Previous update was at 08:06 AM ---------- Correction However my expected output is Code: 2902 N's (to fill gap from 1-2902) then CCTTGAACTTGCATCTAAAC(which is Gen:2903-2922) then 2135 N's to fill the gap from 2923-5056 then AGAAATGCAACGCGAGAGCGCTATTTTACCAACGAAGAATCTGTGCTTCATTTTTGTAAAACAAAAATGCAACGCGAGAGCGCTAATTTT (which is Gen:5057-5146)

# 6  
Old 04-05-2011
Try this:
Code:
#!/usr/bin/perl
open I, "$ARGV[0]";
local $/;
$_=<I>;
while (/(.*)\n(\d+)-(\d+)\nRef:\n.*\n\nGen:\n(.*)/g){
  $h{$1}{"$2-$3"}=$4;
}
for $i (keys %h){
  @arr=sort {(split /-/,$a)[0]<=>(split /-/,$b)[0]} (keys %{$h{$i}});
  $x=1;
  for $j (@arr){
    $j=~/(\d+)-(\d+)/;
    $s{$i}.="N" x ($1-$x) . $h{$i}{$j};
    $x=$2+1;
  }
}
for $i (keys %s){
  print "$i\n";
  print "$s{$i}\n";
}

This User Gave Thanks to bartus11 For This Post:
# 7  
Old 04-06-2011
Thanks Bartus .... that did the job as expected.
Could you please comment on the while loop especially the use of sort and split on @arr and of hashes %h and %s ... I will appreciate its need to understand the working of the code.
Cheers and have a nice daySmilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep: show only first match per line?

Hi, can I make grep stop after finding the first occurrence of a regex in a line? Given: file with various regex patterns file to be grep'ed Sadly some of the regex patterns cannot be limited any further, so grep -Eiof patterns.txt file.txt (GNU grep 2.20) will give me possibly n hits... (4 Replies)
Discussion started by: stresing
4 Replies

2. Shell Programming and Scripting

Grep command to search a regular expression in a line an only print the string after the match

Hello, one step in a shell script i am writing, involves Grep command to search a regular expression in a line an only print the string after the match an example line is below /logs/GRAS/LGT/applogs/lgt-2016-08-24/2016-08-24.8.log.zip:2016-08-24 19:12:48,602 ERROR... (9 Replies)
Discussion started by: Ramneekgupta91
9 Replies

3. Shell Programming and Scripting

Grep or sed - printing line only with exact match

Hello. In my script, some command return : q | kernel-default | package | 3.19.0-1.1.g8a7d5f9 | x86_64 | openSUSE-13.2-Kernel_stable_standard | kernel-default | package | 3.19.0-1.1.g8a7d5f9 | i586 | openSUSE-13.2-Kernel_stable_standard | kernel-default ... (3 Replies)
Discussion started by: jcdole
3 Replies

4. Shell Programming and Scripting

Need to print the next word from the same line based on grep string condtion match.

I need to fetch particular string from log file based on grep condition match. Actual requirement is need to print the next word from the same line based on grep string condtion match. File :Java.lanag.xyz......File copied completed : abc.txt Ouput :abc.txt I have used below... (5 Replies)
Discussion started by: siva83
5 Replies

5. Shell Programming and Scripting

Grep range of lines to print a line number on match

Hi Guru's, I am trying to grep a range of line numbers (based on match) and then look for another match which starts with a special character '$' and print the line number. I have the below code but it is actually printing the line number counting starting from the first line of the range i am... (15 Replies)
Discussion started by: Kevin Tivoli
15 Replies

6. UNIX for Dummies Questions & Answers

Grep help (match two strings one line)

Hello, Here I have some grep command which is not working correctly: cat file1.txt: apples Date: Sun, 24 Feb 2013 8:14:06 -0800 peaches melons cherry sky cloud green purple yellow cat file2.txt: apples Date peaches melons 0800 cherry sky cloud green purple black (2 Replies)
Discussion started by: holyearth
2 Replies

7. Shell Programming and Scripting

fetch last line no form file which is match with specific pattern by grep command

Hi i have a file which have a pattern like this Nov 10 session closed Nov 10 Nov 9 08:14:27 EST5EDT 2010 on tty . Nov 10 Oct 19 02:14:21 EST5EDT 2010 on pts/tk . Nov 10 afrtetryytr Nov 10 session closed Nov 10 Nov 10 03:21:04 EST5EDT 2010 Dec 8 Nov 10 05:03:02 EST5EDT 2010 ... (13 Replies)
Discussion started by: Himanshu_soni
13 Replies

8. UNIX for Dummies Questions & Answers

grep N lines after match and then print them on 1 line each

Hello I have a silly question. I need to grep a match in text file and then print 5 lines after it. grep -A 5 .... do it. OK The next thing I can not handle is I need each output to be on 1 line match line2 line3 line4 line5 match line2 line3 line4 line5 etc.. I will really... (10 Replies)
Discussion started by: alekkz
10 Replies

9. Shell Programming and Scripting

grep N lines after match and then print them on 1 line each

Hello I need some help with this job. file.txt ----- cut ---- TARGET 13/11/08 20:43:21 POINT 1 MOVE 8 772102y64312417771 TARGET 13/11/08 21:10:01 POINT 2 MOVE 5 731623jjd12njhd ----- cut ---- this is the example. i need to grep for the word TARGET and print next 4 lines like... (1 Reply)
Discussion started by: alekkz
1 Replies

10. Shell Programming and Scripting

Awk+Grep Input file needs to match a column and print the entire line

I'm having problems since few days ago, and i'm not able to make it works with a simple awk+grep script (or other way to do this). For example, i have a input file1.txt: cat inputfile1.txt 218299910417 1172051195 1172070231 1172073514 1183135117 1183135118 1183135119 1281440202 ... (3 Replies)
Discussion started by: poliver
3 Replies
Login or Register to Ask a Question