Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore all lines before a match occurs in bash?

Tags:

I would like ignore all lines which occur before a match in bash (also ignoring the matched line. Example of input could be

R1-01.sql R1-02.sql R1-03.sql R1-04.sql R2-01.sql  R2-02.sql R2-03.sql 

and if I match R2-01.sql in this already sorted input I would like to get

R2-02.sql R2-03.sql 
like image 237
geoaxis Avatar asked May 09 '11 10:05

geoaxis


People also ask

How do I skip a line in a bash script?

Using head to get the first lines of a stream, and tail to get the last lines in a stream is intuitive. But if you need to skip the first few lines of a stream, then you use tail “-n +k” syntax. And to skip the last lines of a stream head “-n -k” syntax.

How do I continue a new line in bash?

Linux Files, Users, and Shell Customization with Bash If you want to break up a command so that it fits on more than one line, use a backslash (\) as the last character on the line. Bash will print the continuation prompt, usually a >, to indicate that this is a continuation of the previous line.


2 Answers

Many ways possible. For example: assuming that your input is in list.txt

PATTERN="R2-01.sql" sed "0,/$PATTERN/d" <list.txt 

because, the 0,/pattern/ works only on GNU sed, (e.g. doesn't works on OS X), here is an tampered solution. ;)

PATTERN="R2-01.sql" (echo "dummy-line-to-the-start" ; cat - ) < list.txt | sed "1,/$PATTERN/d" 

This will add one dummy line to the start, so the real pattern must be on line the 1 or higher, so the 1,/pattern/ will works - deleting everything from the line 1 (dummy one) up to the pattern.

Or you can print lines after the pattern and delete the 1st, like:

sed -n '/pattern/,$p' < list.txt | sed '1d' 

with awk, e.g.:

awk '/pattern/,0{if (!/pattern/)print}' < list.txt 

or, my favorite use the next perl command:

perl -ne 'print unless 1../pattern/' < list.txt 

deletes the 1.st line when the pattern is on 1st line...

another solution is reverse-delete-reverse

tail -r < list.txt | sed '/pattern/,$d' | tail -r 

if you have the tac command use it instead of tail -r The interesant thing is than the /pattern/,$d' works on the last line but the1,/pattern/d` doesn't on the first.

like image 67
jm666 Avatar answered Oct 12 '22 12:10

jm666


How to ignore all lines before a match occurs in bash?

The question headline and your example don't quite match up.

Print all lines from "R2-01.sql" in sed:

sed -n '/R2-01.sql/,$p' input_file.txt 

Where:

  • -n suppresses printing the pattern space to stdout
  • / starts and ends the pattern to match (regular expression)
  • , separates the start of the range from the end
  • $ addresses the last line in the input
  • p echoes the pattern space in that range to stdout
  • input_file.txt is the input file

Print all lines after "R2-01.sql" in sed:

sed '1,/R2-01.sql/d' input_file.txt 
  • 1 addresses the first line of the input
  • , separates the start of the range from the end
  • / starts and ends the pattern to match (regular expression)
  • $ addresses the last line in the input
  • d deletes the pattern space in that range
  • input_file.txt is the input file
  • Everything not deleted is echoed to stdout.
like image 22
Johnsyweb Avatar answered Oct 12 '22 11:10

Johnsyweb