0

I have a text file

$ cat a.txt
a_1  
a_2  
a_3  
a_4  
a_5  

If I search for a_2, I need to get all elements after a_2 including it.
Output expected is

a_2
a_3
a_4
a_5

Kindly help.

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
Harsha K L
  • 627
  • 1
  • 7
  • 16
  • @fedorqui, not necessarily a duplicate as the other question is specific to sed and this gives answers with different methods. –  Jul 31 '14 at 09:42
  • @Jidder have you seen the accepted answer in that question? Its solution is both in awk and sed. – fedorqui Jul 31 '14 at 09:44
  • There are many more ways to do this without sed or awk though! –  Jul 31 '14 at 10:04
  • @Jidder the point of marking as duplicate is to concentrate the knowledge in a point, rather than having good answers spread around different similar questions. So if you feel like adding a new good answer, you can do in the "main" question, that grows as a good point of knowledge. – fedorqui Jul 31 '14 at 10:14
  • @fedorqui Maybe the title of the other question could be changed to not be sed specific then ? –  Jul 31 '14 at 10:15
  • @fedorqui thanks. This is a duplicate of that question and i have flagged it :) –  Jul 31 '14 at 11:54

4 Answers4

5

Try this

awk "/a_2/ { flag = 1 }; flag" file
steffen
  • 16,138
  • 4
  • 42
  • 81
3

Its simple using awk.

awk '/a_2/{p=1}p' your_file
Vijay
  • 65,327
  • 90
  • 227
  • 319
1

Through sed,

$ sed -n '/a_2/,/&/p' file
a_2  
a_3  
a_4  
a_5 
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • 1
    could you explain the /&/ ? what is the reference behind this `&`, normaly this is the previous matching pattern but here ? and why not a simple `/a_2/,$p` if & is a "unfinding" pattern ? – NeronLeVelu Jul 31 '14 at 09:22
  • i think `/&/` matches upto the last line. – Avinash Raj Jul 31 '14 at 09:38
  • I'm fairly certain that `&` points to whatever `sed` holds in the pattern space. So it searches for `a_2`, finds and puts it in the pattern space and then searches again for `a_2` (referenced by the `&`) but is it never finds it, so it just goes on printing the rest of file – skamazin Jul 31 '14 at 12:46
  • `sed -n '/a_2/,$p' file` – Cyrus Jul 31 '14 at 18:52
1

Simplest way.

more +"a_2" file