Resolution: the files were saved with CR rather than LF line breaks. Mosvy pointed this out, but only posted as a comment, rather than an answer, so I am unable to officially thank him for helping me to find the cause and solve the problem.

Thanks mosvy, if you come back please post as an answer so I can give you a thumbs up.

SED seems to have:

sed '3,10d;/<ACROSS>/,$d' input.txt > output.txt

(delete line 3-10, then delete from line containing "<ACROSS>" to end of file; then write out output.)

Even when I try with only:

sed '3,10d' input.txt > output.txt

but for some reason neither seems to work on my Mac.

Not sure what else to try.

I am hoping there is something very similar with AWK.


when I enter:

sed '3,10d' input.txt > output.txt

it does not delete lines 3 - 10; it just spits back the entire file to output.txt;

when I try:

sed '/<ACROSS>/,$d' input.txt > output.txt

output.txt is blank

Also, I'm on 10.9.4

** Update 2:

Thank you to mosvy!! I wish I could upvote your comment. It was the problem solver.

It turns out the file was saved with CR rather than LF line breaks

When I converted it, that cured everything.

Thanks to everyone who contributed.

  • 3
    What does "not work" mean here? Do you get some form of error message? – Kusalananda Dec 18 '18 at 6:43
  • 1
    your question is unclear, but if you're wondering if you can mix patterns and line numbers in awk ranges: yes, you can. awk 'NR==2,/rex/' will print all lines from the 2nd up to (and including) the one matching /rex/ – mosvy Dec 18 '18 at 6:54
  • 1
    and awk '/rex/,0' will print all lines from the line matching /rex/ up to end-of-file. – mosvy Dec 18 '18 at 7:02
  • 1
    maybe your file isn't using \n/LF/ascii 10 line terminators? those commands should work, even on very ancient sed implementations. – mosvy Dec 18 '18 at 7:06
  • 1
    @mosvy you can post that as an answer. – muru Dec 18 '18 at 7:23

The OP's problem was caused by file file using CR (\r / ascii 13) instead of LF (\n / ascii 10) as line terminators as expected by sed. Using CR was the convention used in classic MacOS; as a non Mac user, the only use of it I've met with in the wild in the last two decades was in PDF files, where it greatly complicates any naive PDF parser written in perl (unlike RS in mawk and gawk, $/ in perl cannot be a regex).

As to the question from the title, yes, awk supports range patterns, and you can freely mix regexps and line number predicates (or any expression) in them. For example:

NR==1,/rex/   # all lines from the 1rst up to (and including)
          # the one matching /rex/

/rex/,0   # from the line matching /rex/ up to the end-of-file.

awk's ranges are different from sed's, because in awk the end predicate could also match the line which started the range. sed's behavior could be emulated with:

s=/start/, !s && /last/ { s = 0; print }

However, ranges in awk are still quite limited because they're not real expression (they cannot be negated, made part of other expressions, used in if(...), etc). Also, there is no magic: if you want to express something like a range with "context" (eg. /start/-4,/end/+4) you'll have to roll your own circular buffer and extra logic.

  • Thank you, sir for helping me solve the problem. It's a problem one doesn't usually think to check first. I was tearing my hair out. I was forced to backtrack and experiment with basic textbook example, and even that was not working before your comment. I kept thinking I must be missing something about the code as I am not an expert using command line, and that it shouldn't be this hard just to do a few basic substitutions. – verbatim Dec 19 '18 at 6:28

/usr/bin/sed '3,10d' works perfectly fine on my Mac (Mojave). Even so, with awk:

awk '(NR >= 3) && (NR <= 10) {next} /<ACROSS>/{exit} 1'

... which skips lines if the line number is between 3 and 10, and simply exits when it reaches a line with <ACROSS> (and prints everything else).

  • Thanks for the input, but this did not seem to work either.: awk '(NR >= 3) && (NR <= 10) {next} /<ACROSS>/{exit} 1' input.txt > output.txt – verbatim Dec 18 '18 at 7:06
  • 1
    @verbatim what Kusalananda said: what does "not work" mean? Have you tested with other files? What does (seq 10; echo '<ACROSS>'; seq 5) | awk '(NR >= 3) && (NR <= 10) {next} /<ACROSS>/{exit} 1' output? What about (seq 10; echo '<ACROSS>'; seq 1 5) | sed '3,10d; /<ACROSS>/,$d'? – muru Dec 18 '18 at 7:11

Your sed works fine. Even so, in Awk there is , -- the "between" operator. Following some examples (default action = print)

Print the lines between "start" and "stop"

awk '/start/,/stop/'

Lines between line 3 and 10

awk 'NR==3, NR==10'

lines between <ACROSS> and end

awk '/<ACROSS>/, 0'

(0 is false, so it never ends)

  • 2
    Would be worth noting that awk's one is better than sed's in that a record can match both ends: sed '1,/foo/' to match everything up to the first occurrence of foo only works if that first occurrence is not on the first line (GNU sed supports a 0 virtual address to work around that specific case), while awk 'NR==1, /foo/' works OK in awk. awk's FNR also allows to have line-number-based ranges per file (GNU sed has a -s for that). – Stéphane Chazelas Dec 18 '18 at 11:14

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.