Extract a line from a file with sed
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
How do I write a sed
script that scans the input file for "start" and finds the line containing "next" and displays the following line? Something like this:
[user]$ cat test.txt
start
next
This line should print
Ignore this
[user]$ display.sed test.txt
This line should print
[user]$ cat test1.txt
Ignore this
next
Ignore this
start
Ignore this
next
This line should print
Ignore this
next
Too late so ignore this too
start
Ignore this too
[user]$ display.sed test1.txt
This line should print
text-processing sed
add a comment |Â
up vote
3
down vote
favorite
How do I write a sed
script that scans the input file for "start" and finds the line containing "next" and displays the following line? Something like this:
[user]$ cat test.txt
start
next
This line should print
Ignore this
[user]$ display.sed test.txt
This line should print
[user]$ cat test1.txt
Ignore this
next
Ignore this
start
Ignore this
next
This line should print
Ignore this
next
Too late so ignore this too
start
Ignore this too
[user]$ display.sed test1.txt
This line should print
text-processing sed
What if you havenext
and thensomething
after the secondstart
, should the script printsomething
too or should it print only the 1st occurrence ?
â don_crissti
Oct 29 '17 at 23:11
It should print only the 1st occurrence after start+next.
â Hazem Ahmed
Oct 29 '17 at 23:12
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
How do I write a sed
script that scans the input file for "start" and finds the line containing "next" and displays the following line? Something like this:
[user]$ cat test.txt
start
next
This line should print
Ignore this
[user]$ display.sed test.txt
This line should print
[user]$ cat test1.txt
Ignore this
next
Ignore this
start
Ignore this
next
This line should print
Ignore this
next
Too late so ignore this too
start
Ignore this too
[user]$ display.sed test1.txt
This line should print
text-processing sed
How do I write a sed
script that scans the input file for "start" and finds the line containing "next" and displays the following line? Something like this:
[user]$ cat test.txt
start
next
This line should print
Ignore this
[user]$ display.sed test.txt
This line should print
[user]$ cat test1.txt
Ignore this
next
Ignore this
start
Ignore this
next
This line should print
Ignore this
next
Too late so ignore this too
start
Ignore this too
[user]$ display.sed test1.txt
This line should print
text-processing sed
edited Oct 29 '17 at 23:15
don_crissti
46.9k15124154
46.9k15124154
asked Oct 29 '17 at 22:58
Hazem Ahmed
161
161
What if you havenext
and thensomething
after the secondstart
, should the script printsomething
too or should it print only the 1st occurrence ?
â don_crissti
Oct 29 '17 at 23:11
It should print only the 1st occurrence after start+next.
â Hazem Ahmed
Oct 29 '17 at 23:12
add a comment |Â
What if you havenext
and thensomething
after the secondstart
, should the script printsomething
too or should it print only the 1st occurrence ?
â don_crissti
Oct 29 '17 at 23:11
It should print only the 1st occurrence after start+next.
â Hazem Ahmed
Oct 29 '17 at 23:12
What if you have
next
and then something
after the second start
, should the script print something
too or should it print only the 1st occurrence ?â don_crissti
Oct 29 '17 at 23:11
What if you have
next
and then something
after the second start
, should the script print something
too or should it print only the 1st occurrence ?â don_crissti
Oct 29 '17 at 23:11
It should print only the 1st occurrence after start+next.
â Hazem Ahmed
Oct 29 '17 at 23:12
It should print only the 1st occurrence after start+next.
â Hazem Ahmed
Oct 29 '17 at 23:12
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
3
down vote
You could use a range (from 1st occurrence of start
to end of file) and d
elete all lines in that range that don't match next
. If a line matches, read in the n
ext line, p
rint it then q
uit:
sed -n '/start/,$
/next/!d;n;p;q
' infile
I guess what you actually want is a file display.sed
with the following content:
#!/bin/sed -nf
/start/,$
/next/!d;n;p;q
it gives me an unterminated 's' command. Am i doing something wrong?
â Hazem Ahmed
Oct 29 '17 at 23:20
I wrote the following: #!/bin/sed -f then your code that you just posted.
â Hazem Ahmed
Oct 29 '17 at 23:23
if the file name is passed as an argument beside the file, how do i access it from the script?
â Hazem Ahmed
Oct 29 '17 at 23:29
recommendation?
â Hazem Ahmed
Oct 29 '17 at 23:38
@HazemAhmed - see my edit; btw, is this homework ?
â don_crissti
Oct 29 '17 at 23:45
add a comment |Â
up vote
2
down vote
Using awk
would be more appropriate for such case cause it should work even on non-GNU awk implementations:
awk '/^start/ f=1 f && /^next/ && getline nl>0 print nl; exit ' test.txt
/^start/ f=1
- set active flagf=1
on encountering linestart
f && /^next/ && getline nl>0
- on encounteringnext
line (with previously matchedstart
line - ensured by activef
flag) - check if the next needed line exists withgetline nl>0
nl
("needed line") - contains the line following linenext
The output (for your current input content):
This line should print
add a comment |Â
up vote
1
down vote
What about grep
? I will add explanation, if this command does, what you want.
grep -Pzo '(?s)start.*?nextnK.*?n' input.txt
Input (your both examples merged)
start
next
This line should print
Ignore this
Ignore this
next
Ignore this
start
Ignore this
next
This line should print
Ignore this
next
Too late so ignore this too
start
Ignore this too
Output
This line should print
This line should print
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
You could use a range (from 1st occurrence of start
to end of file) and d
elete all lines in that range that don't match next
. If a line matches, read in the n
ext line, p
rint it then q
uit:
sed -n '/start/,$
/next/!d;n;p;q
' infile
I guess what you actually want is a file display.sed
with the following content:
#!/bin/sed -nf
/start/,$
/next/!d;n;p;q
it gives me an unterminated 's' command. Am i doing something wrong?
â Hazem Ahmed
Oct 29 '17 at 23:20
I wrote the following: #!/bin/sed -f then your code that you just posted.
â Hazem Ahmed
Oct 29 '17 at 23:23
if the file name is passed as an argument beside the file, how do i access it from the script?
â Hazem Ahmed
Oct 29 '17 at 23:29
recommendation?
â Hazem Ahmed
Oct 29 '17 at 23:38
@HazemAhmed - see my edit; btw, is this homework ?
â don_crissti
Oct 29 '17 at 23:45
add a comment |Â
up vote
3
down vote
You could use a range (from 1st occurrence of start
to end of file) and d
elete all lines in that range that don't match next
. If a line matches, read in the n
ext line, p
rint it then q
uit:
sed -n '/start/,$
/next/!d;n;p;q
' infile
I guess what you actually want is a file display.sed
with the following content:
#!/bin/sed -nf
/start/,$
/next/!d;n;p;q
it gives me an unterminated 's' command. Am i doing something wrong?
â Hazem Ahmed
Oct 29 '17 at 23:20
I wrote the following: #!/bin/sed -f then your code that you just posted.
â Hazem Ahmed
Oct 29 '17 at 23:23
if the file name is passed as an argument beside the file, how do i access it from the script?
â Hazem Ahmed
Oct 29 '17 at 23:29
recommendation?
â Hazem Ahmed
Oct 29 '17 at 23:38
@HazemAhmed - see my edit; btw, is this homework ?
â don_crissti
Oct 29 '17 at 23:45
add a comment |Â
up vote
3
down vote
up vote
3
down vote
You could use a range (from 1st occurrence of start
to end of file) and d
elete all lines in that range that don't match next
. If a line matches, read in the n
ext line, p
rint it then q
uit:
sed -n '/start/,$
/next/!d;n;p;q
' infile
I guess what you actually want is a file display.sed
with the following content:
#!/bin/sed -nf
/start/,$
/next/!d;n;p;q
You could use a range (from 1st occurrence of start
to end of file) and d
elete all lines in that range that don't match next
. If a line matches, read in the n
ext line, p
rint it then q
uit:
sed -n '/start/,$
/next/!d;n;p;q
' infile
I guess what you actually want is a file display.sed
with the following content:
#!/bin/sed -nf
/start/,$
/next/!d;n;p;q
edited Oct 29 '17 at 23:45
answered Oct 29 '17 at 23:14
don_crissti
46.9k15124154
46.9k15124154
it gives me an unterminated 's' command. Am i doing something wrong?
â Hazem Ahmed
Oct 29 '17 at 23:20
I wrote the following: #!/bin/sed -f then your code that you just posted.
â Hazem Ahmed
Oct 29 '17 at 23:23
if the file name is passed as an argument beside the file, how do i access it from the script?
â Hazem Ahmed
Oct 29 '17 at 23:29
recommendation?
â Hazem Ahmed
Oct 29 '17 at 23:38
@HazemAhmed - see my edit; btw, is this homework ?
â don_crissti
Oct 29 '17 at 23:45
add a comment |Â
it gives me an unterminated 's' command. Am i doing something wrong?
â Hazem Ahmed
Oct 29 '17 at 23:20
I wrote the following: #!/bin/sed -f then your code that you just posted.
â Hazem Ahmed
Oct 29 '17 at 23:23
if the file name is passed as an argument beside the file, how do i access it from the script?
â Hazem Ahmed
Oct 29 '17 at 23:29
recommendation?
â Hazem Ahmed
Oct 29 '17 at 23:38
@HazemAhmed - see my edit; btw, is this homework ?
â don_crissti
Oct 29 '17 at 23:45
it gives me an unterminated 's' command. Am i doing something wrong?
â Hazem Ahmed
Oct 29 '17 at 23:20
it gives me an unterminated 's' command. Am i doing something wrong?
â Hazem Ahmed
Oct 29 '17 at 23:20
I wrote the following: #!/bin/sed -f then your code that you just posted.
â Hazem Ahmed
Oct 29 '17 at 23:23
I wrote the following: #!/bin/sed -f then your code that you just posted.
â Hazem Ahmed
Oct 29 '17 at 23:23
if the file name is passed as an argument beside the file, how do i access it from the script?
â Hazem Ahmed
Oct 29 '17 at 23:29
if the file name is passed as an argument beside the file, how do i access it from the script?
â Hazem Ahmed
Oct 29 '17 at 23:29
recommendation?
â Hazem Ahmed
Oct 29 '17 at 23:38
recommendation?
â Hazem Ahmed
Oct 29 '17 at 23:38
@HazemAhmed - see my edit; btw, is this homework ?
â don_crissti
Oct 29 '17 at 23:45
@HazemAhmed - see my edit; btw, is this homework ?
â don_crissti
Oct 29 '17 at 23:45
add a comment |Â
up vote
2
down vote
Using awk
would be more appropriate for such case cause it should work even on non-GNU awk implementations:
awk '/^start/ f=1 f && /^next/ && getline nl>0 print nl; exit ' test.txt
/^start/ f=1
- set active flagf=1
on encountering linestart
f && /^next/ && getline nl>0
- on encounteringnext
line (with previously matchedstart
line - ensured by activef
flag) - check if the next needed line exists withgetline nl>0
nl
("needed line") - contains the line following linenext
The output (for your current input content):
This line should print
add a comment |Â
up vote
2
down vote
Using awk
would be more appropriate for such case cause it should work even on non-GNU awk implementations:
awk '/^start/ f=1 f && /^next/ && getline nl>0 print nl; exit ' test.txt
/^start/ f=1
- set active flagf=1
on encountering linestart
f && /^next/ && getline nl>0
- on encounteringnext
line (with previously matchedstart
line - ensured by activef
flag) - check if the next needed line exists withgetline nl>0
nl
("needed line") - contains the line following linenext
The output (for your current input content):
This line should print
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Using awk
would be more appropriate for such case cause it should work even on non-GNU awk implementations:
awk '/^start/ f=1 f && /^next/ && getline nl>0 print nl; exit ' test.txt
/^start/ f=1
- set active flagf=1
on encountering linestart
f && /^next/ && getline nl>0
- on encounteringnext
line (with previously matchedstart
line - ensured by activef
flag) - check if the next needed line exists withgetline nl>0
nl
("needed line") - contains the line following linenext
The output (for your current input content):
This line should print
Using awk
would be more appropriate for such case cause it should work even on non-GNU awk implementations:
awk '/^start/ f=1 f && /^next/ && getline nl>0 print nl; exit ' test.txt
/^start/ f=1
- set active flagf=1
on encountering linestart
f && /^next/ && getline nl>0
- on encounteringnext
line (with previously matchedstart
line - ensured by activef
flag) - check if the next needed line exists withgetline nl>0
nl
("needed line") - contains the line following linenext
The output (for your current input content):
This line should print
edited Oct 30 '17 at 12:01
answered Oct 29 '17 at 23:15
RomanPerekhrest
22.5k12145
22.5k12145
add a comment |Â
add a comment |Â
up vote
1
down vote
What about grep
? I will add explanation, if this command does, what you want.
grep -Pzo '(?s)start.*?nextnK.*?n' input.txt
Input (your both examples merged)
start
next
This line should print
Ignore this
Ignore this
next
Ignore this
start
Ignore this
next
This line should print
Ignore this
next
Too late so ignore this too
start
Ignore this too
Output
This line should print
This line should print
add a comment |Â
up vote
1
down vote
What about grep
? I will add explanation, if this command does, what you want.
grep -Pzo '(?s)start.*?nextnK.*?n' input.txt
Input (your both examples merged)
start
next
This line should print
Ignore this
Ignore this
next
Ignore this
start
Ignore this
next
This line should print
Ignore this
next
Too late so ignore this too
start
Ignore this too
Output
This line should print
This line should print
add a comment |Â
up vote
1
down vote
up vote
1
down vote
What about grep
? I will add explanation, if this command does, what you want.
grep -Pzo '(?s)start.*?nextnK.*?n' input.txt
Input (your both examples merged)
start
next
This line should print
Ignore this
Ignore this
next
Ignore this
start
Ignore this
next
This line should print
Ignore this
next
Too late so ignore this too
start
Ignore this too
Output
This line should print
This line should print
What about grep
? I will add explanation, if this command does, what you want.
grep -Pzo '(?s)start.*?nextnK.*?n' input.txt
Input (your both examples merged)
start
next
This line should print
Ignore this
Ignore this
next
Ignore this
start
Ignore this
next
This line should print
Ignore this
next
Too late so ignore this too
start
Ignore this too
Output
This line should print
This line should print
edited Oct 30 '17 at 0:03
answered Oct 29 '17 at 23:53
MiniMax
2,706719
2,706719
add a comment |Â
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f401307%2fextract-a-line-from-a-file-with-sed%23new-answer', 'question_page');
);
Post as a guest
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
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
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
What if you have
next
and thensomething
after the secondstart
, should the script printsomething
too or should it print only the 1st occurrence ?â don_crissti
Oct 29 '17 at 23:11
It should print only the 1st occurrence after start+next.
â Hazem Ahmed
Oct 29 '17 at 23:12