Does AWK have similar ability as SED to find line ranges based on text in line rather than line number?
Clash Royale CLAN TAG#URR8PPP
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.
Update:
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.
awk sed command-line macintosh
|
show 1 more comment
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.
Update:
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.
awk sed command-line macintosh
3
What does "not work" mean here? Do you get some form of error message?
– Kusalananda
Dec 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 at 6:54
1
andawk '/rex/,0'
will print all lines from the line matching/rex/
up to end-of-file.
– mosvy
Dec 18 at 7:02
1
maybe your file isn't usingn
/LF
/ascii 10 line terminators? those commands should work, even on very ancientsed
implementations.
– mosvy
Dec 18 at 7:06
1
@mosvy you can post that as an answer.
– muru
Dec 18 at 7:23
|
show 1 more comment
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.
Update:
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.
awk sed command-line macintosh
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.
Update:
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.
awk sed command-line macintosh
awk sed command-line macintosh
edited Dec 18 at 9:57
asked Dec 18 at 6:00
verbatim
184
184
3
What does "not work" mean here? Do you get some form of error message?
– Kusalananda
Dec 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 at 6:54
1
andawk '/rex/,0'
will print all lines from the line matching/rex/
up to end-of-file.
– mosvy
Dec 18 at 7:02
1
maybe your file isn't usingn
/LF
/ascii 10 line terminators? those commands should work, even on very ancientsed
implementations.
– mosvy
Dec 18 at 7:06
1
@mosvy you can post that as an answer.
– muru
Dec 18 at 7:23
|
show 1 more comment
3
What does "not work" mean here? Do you get some form of error message?
– Kusalananda
Dec 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 at 6:54
1
andawk '/rex/,0'
will print all lines from the line matching/rex/
up to end-of-file.
– mosvy
Dec 18 at 7:02
1
maybe your file isn't usingn
/LF
/ascii 10 line terminators? those commands should work, even on very ancientsed
implementations.
– mosvy
Dec 18 at 7:06
1
@mosvy you can post that as an answer.
– muru
Dec 18 at 7:23
3
3
What does "not work" mean here? Do you get some form of error message?
– Kusalananda
Dec 18 at 6:43
What does "not work" mean here? Do you get some form of error message?
– Kusalananda
Dec 18 at 6:43
1
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 at 6:54
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 at 6:54
1
1
and
awk '/rex/,0'
will print all lines from the line matching /rex/
up to end-of-file.– mosvy
Dec 18 at 7:02
and
awk '/rex/,0'
will print all lines from the line matching /rex/
up to end-of-file.– mosvy
Dec 18 at 7:02
1
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 at 7:06
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 at 7:06
1
1
@mosvy you can post that as an answer.
– muru
Dec 18 at 7:23
@mosvy you can post that as an answer.
– muru
Dec 18 at 7:23
|
show 1 more comment
3 Answers
3
active
oldest
votes
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 at 6:28
add a comment |
/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 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 at 7:11
add a comment |
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 thatawk
's one is better thansed
's in that a record can match both ends:sed '1,/foo/'
to match everything up to the first occurrence offoo
only works if that first occurrence is not on the first line (GNUsed
supports a0
virtual address to work around that specific case), whileawk 'NR==1, /foo/'
works OK inawk
.awk
'sFNR
also allows to have line-number-based ranges per file (GNUsed
has a-s
for that).
– Stéphane Chazelas
Dec 18 at 11:14
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f489617%2fdoes-awk-have-similar-ability-as-sed-to-find-line-ranges-based-on-text-in-line-r%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 at 6:28
add a comment |
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 at 6:28
add a comment |
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.
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.
edited Dec 19 at 5:34
answered Dec 19 at 5:20
mosvy
5,9411325
5,9411325
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 at 6:28
add a comment |
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 at 6:28
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 at 6:28
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 at 6:28
add a comment |
/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 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 at 7:11
add a comment |
/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 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 at 7:11
add a comment |
/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).
/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).
answered Dec 18 at 6:19
muru
1
1
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 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 at 7:11
add a comment |
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 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 at 7:11
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 at 7:06
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 at 7:06
1
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 at 7:11
@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 at 7:11
add a comment |
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 thatawk
's one is better thansed
's in that a record can match both ends:sed '1,/foo/'
to match everything up to the first occurrence offoo
only works if that first occurrence is not on the first line (GNUsed
supports a0
virtual address to work around that specific case), whileawk 'NR==1, /foo/'
works OK inawk
.awk
'sFNR
also allows to have line-number-based ranges per file (GNUsed
has a-s
for that).
– Stéphane Chazelas
Dec 18 at 11:14
add a comment |
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 thatawk
's one is better thansed
's in that a record can match both ends:sed '1,/foo/'
to match everything up to the first occurrence offoo
only works if that first occurrence is not on the first line (GNUsed
supports a0
virtual address to work around that specific case), whileawk 'NR==1, /foo/'
works OK inawk
.awk
'sFNR
also allows to have line-number-based ranges per file (GNUsed
has a-s
for that).
– Stéphane Chazelas
Dec 18 at 11:14
add a comment |
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)
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)
edited Dec 18 at 11:00
answered Dec 18 at 10:54
JJoao
7,1041928
7,1041928
2
Would be worth noting thatawk
's one is better thansed
's in that a record can match both ends:sed '1,/foo/'
to match everything up to the first occurrence offoo
only works if that first occurrence is not on the first line (GNUsed
supports a0
virtual address to work around that specific case), whileawk 'NR==1, /foo/'
works OK inawk
.awk
'sFNR
also allows to have line-number-based ranges per file (GNUsed
has a-s
for that).
– Stéphane Chazelas
Dec 18 at 11:14
add a comment |
2
Would be worth noting thatawk
's one is better thansed
's in that a record can match both ends:sed '1,/foo/'
to match everything up to the first occurrence offoo
only works if that first occurrence is not on the first line (GNUsed
supports a0
virtual address to work around that specific case), whileawk 'NR==1, /foo/'
works OK inawk
.awk
'sFNR
also allows to have line-number-based ranges per file (GNUsed
has a-s
for that).
– Stéphane Chazelas
Dec 18 at 11:14
2
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 at 11:14
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 at 11:14
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f489617%2fdoes-awk-have-similar-ability-as-sed-to-find-line-ranges-based-on-text-in-line-r%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
What does "not work" mean here? Do you get some form of error message?
– Kusalananda
Dec 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 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 at 7:02
1
maybe your file isn't using
n
/LF
/ascii 10 line terminators? those commands should work, even on very ancientsed
implementations.– mosvy
Dec 18 at 7:06
1
@mosvy you can post that as an answer.
– muru
Dec 18 at 7:23