7

I have a large text file and I only want to look at some of the lines. The first line I want matches a regex R, and when the line matches the regex S, I don't care about that line, or any following lines. Lines in the middle will not match R. Is there a way to do this on the command line in a bash command so I can pipe the output somewhere after?

  • 2
    Please give input and output examples, and what means a line in the middle? Not the first and not the last line? – chaos Apr 28 '15 at 8:59
  • For GNU sed '/R/,/S/!d;/S/Q' large.text.file – Costas Apr 28 '15 at 9:01
  • More portable sed --posix -n '/R/{:1;p;n;/S/!b1;};/S/q' – Costas Apr 28 '15 at 9:24
  • Your POSIX one is not POSIX (; is a valid character in the name of a label, you need ; before } (and can't have anything after) – Stéphane Chazelas Apr 28 '15 at 9:26
  • 2
    @chaos, though an example might help, the question is clearly specified without any ambiguity (the only possible one being whether a line may match both R and S) which is rare enough here to be praised. It certainly doesn't warrant closing as unclear. – Stéphane Chazelas Apr 28 '15 at 10:38
5

With sed:

sed -n '/R/,$!d; /S/q; p'

Example:

$ seq 20 | sed -n '/6/,$!d; /1/q; p'
6
7
8
9
2

with perl:

# perl -ne 'print if ( /R/ .. /S/ ); last if /S/'

It does print the S-Matching regexp, tho'

2

With awk

awk 'x&&/S/{exit};x+=/R/' file

Example

seq 20 | awk 'x&&/1/{exit};x+=/6/'
6
7
8
9
  • It could be optimised by not looking for subsequent Rs like awk '/R/,0{if (/S/) exit; print}' – Stéphane Chazelas Apr 28 '15 at 9:40
  • @StéphaneChazelas Cool,don't edit my answer to add unnecessary semi colons please :) – user78605 Apr 28 '15 at 9:42
  • the semi-colon is required by POSIX. – Stéphane Chazelas Apr 28 '15 at 9:43
  • @StéphaneChazelas No they aren't ? Atleast not when using the --posix or --compat argument ? Also for your optimisation, i doubt that would make even a seconds difference on a terabyte file. – user78605 Apr 28 '15 at 9:45
  • Actually, I even once filed a request to the Austin group so that restriction be relaxed as I couldn't find any implementation that enforced it but it was rejected by the maintainer of GNU awk. (please also note that my comments are never intended as being confrontational, only at adding information). – Stéphane Chazelas Apr 28 '15 at 9:52
-1

I have written a programme to solve this fromto

You can use it like this:

cat file | fromto -f R -T S

Where R is the regex you want to grab from, and S is the regex you want to grab to. It will print out all lines between those two. -f means "include the 'from' line", -F would mean "don't include the 'from' line". Likewise with -t/-T. The question was to exclude the 'to' line, hence -T is the argument for that requirement. If you used -t it would print the 'to' line (but not lines afterwards)

  • Plese explain about your application. – PersianGulf Apr 28 '15 at 14:45
  • I have expanded this to include details. – Rory Apr 28 '15 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.