Grep starting from a fixed text, until the first blank line

Clash Royale CLAN TAG#URR8PPP
up vote
8
down vote
favorite
I have a file prova.txt like this:
Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random2
random3
random4
extra1
extra2
bla
Start to grab from here: 2
fix1
fix2
fix3
fix4
random1546
random2561
extra2
bla
bla
Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random22131
and I need to grep out from "Start to grab here" to the first blank line. The output should be like this:
Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random2
random3
random4
Start to grab from here: 2
fix1
fix2
fix3
fix4
random1546
random2561
Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random22131
As you can see the lines after "Start to grab here" are random, so -A -B grep flag don't work:
cat prova.txt | grep "Start to grab from here" -A 15 | grep -B 15 "^$" > output.txt
Can you help me to find a way that catch the first line that will be grabbed (as "Start to grab from here"), until a blank line. I cannot predict how many random lines I will have after "Start to grab from here".
Any unix compatible solution is appreciate (grep, sed, awk is better than perl or similar).
EDITED: after brilliant response by @john1024, I would like to know if it's possible to:
1° sort the block (according to Start to grab from here: 1 then 1 then 2)
2° remove 4 (alphabetically random) lines fix1,fix2,fix3,fix4 but are always 4
3° eventually remove random dupes, like sort -u command
Final output shoul be like this:
# fix lines removed - match 1 first time
Start to grab from here: 1
random1
random2
random3
random4
#fix lines removed - match 1 second time
Start to grab from here: 1
#random1 removed cause is a dupe
random22131
#fix lines removed - match 2 that comes after 1
Start to grab from here: 2
random1546
random2561
or
# fix lines removed - match 1 first time and the second too
Start to grab from here: 1
random1
random2
random3
random4
#random1 removed cause is a dupe
random22131
#fix lines removed - match 2 that comes after 1
Start to grab from here: 2
random1546
random2561
The second output is better that the first one. Some other unix command magic is needed.
sed awk regular-expression sort
add a comment |
up vote
8
down vote
favorite
I have a file prova.txt like this:
Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random2
random3
random4
extra1
extra2
bla
Start to grab from here: 2
fix1
fix2
fix3
fix4
random1546
random2561
extra2
bla
bla
Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random22131
and I need to grep out from "Start to grab here" to the first blank line. The output should be like this:
Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random2
random3
random4
Start to grab from here: 2
fix1
fix2
fix3
fix4
random1546
random2561
Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random22131
As you can see the lines after "Start to grab here" are random, so -A -B grep flag don't work:
cat prova.txt | grep "Start to grab from here" -A 15 | grep -B 15 "^$" > output.txt
Can you help me to find a way that catch the first line that will be grabbed (as "Start to grab from here"), until a blank line. I cannot predict how many random lines I will have after "Start to grab from here".
Any unix compatible solution is appreciate (grep, sed, awk is better than perl or similar).
EDITED: after brilliant response by @john1024, I would like to know if it's possible to:
1° sort the block (according to Start to grab from here: 1 then 1 then 2)
2° remove 4 (alphabetically random) lines fix1,fix2,fix3,fix4 but are always 4
3° eventually remove random dupes, like sort -u command
Final output shoul be like this:
# fix lines removed - match 1 first time
Start to grab from here: 1
random1
random2
random3
random4
#fix lines removed - match 1 second time
Start to grab from here: 1
#random1 removed cause is a dupe
random22131
#fix lines removed - match 2 that comes after 1
Start to grab from here: 2
random1546
random2561
or
# fix lines removed - match 1 first time and the second too
Start to grab from here: 1
random1
random2
random3
random4
#random1 removed cause is a dupe
random22131
#fix lines removed - match 2 that comes after 1
Start to grab from here: 2
random1546
random2561
The second output is better that the first one. Some other unix command magic is needed.
sed awk regular-expression sort
This is really helpful for grabbing the stack trace for a particular thread from java jstack output. Glad I found this Q&A!
– BenjaminBallard
Sep 14 at 14:00
add a comment |
up vote
8
down vote
favorite
up vote
8
down vote
favorite
I have a file prova.txt like this:
Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random2
random3
random4
extra1
extra2
bla
Start to grab from here: 2
fix1
fix2
fix3
fix4
random1546
random2561
extra2
bla
bla
Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random22131
and I need to grep out from "Start to grab here" to the first blank line. The output should be like this:
Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random2
random3
random4
Start to grab from here: 2
fix1
fix2
fix3
fix4
random1546
random2561
Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random22131
As you can see the lines after "Start to grab here" are random, so -A -B grep flag don't work:
cat prova.txt | grep "Start to grab from here" -A 15 | grep -B 15 "^$" > output.txt
Can you help me to find a way that catch the first line that will be grabbed (as "Start to grab from here"), until a blank line. I cannot predict how many random lines I will have after "Start to grab from here".
Any unix compatible solution is appreciate (grep, sed, awk is better than perl or similar).
EDITED: after brilliant response by @john1024, I would like to know if it's possible to:
1° sort the block (according to Start to grab from here: 1 then 1 then 2)
2° remove 4 (alphabetically random) lines fix1,fix2,fix3,fix4 but are always 4
3° eventually remove random dupes, like sort -u command
Final output shoul be like this:
# fix lines removed - match 1 first time
Start to grab from here: 1
random1
random2
random3
random4
#fix lines removed - match 1 second time
Start to grab from here: 1
#random1 removed cause is a dupe
random22131
#fix lines removed - match 2 that comes after 1
Start to grab from here: 2
random1546
random2561
or
# fix lines removed - match 1 first time and the second too
Start to grab from here: 1
random1
random2
random3
random4
#random1 removed cause is a dupe
random22131
#fix lines removed - match 2 that comes after 1
Start to grab from here: 2
random1546
random2561
The second output is better that the first one. Some other unix command magic is needed.
sed awk regular-expression sort
I have a file prova.txt like this:
Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random2
random3
random4
extra1
extra2
bla
Start to grab from here: 2
fix1
fix2
fix3
fix4
random1546
random2561
extra2
bla
bla
Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random22131
and I need to grep out from "Start to grab here" to the first blank line. The output should be like this:
Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random2
random3
random4
Start to grab from here: 2
fix1
fix2
fix3
fix4
random1546
random2561
Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random22131
As you can see the lines after "Start to grab here" are random, so -A -B grep flag don't work:
cat prova.txt | grep "Start to grab from here" -A 15 | grep -B 15 "^$" > output.txt
Can you help me to find a way that catch the first line that will be grabbed (as "Start to grab from here"), until a blank line. I cannot predict how many random lines I will have after "Start to grab from here".
Any unix compatible solution is appreciate (grep, sed, awk is better than perl or similar).
EDITED: after brilliant response by @john1024, I would like to know if it's possible to:
1° sort the block (according to Start to grab from here: 1 then 1 then 2)
2° remove 4 (alphabetically random) lines fix1,fix2,fix3,fix4 but are always 4
3° eventually remove random dupes, like sort -u command
Final output shoul be like this:
# fix lines removed - match 1 first time
Start to grab from here: 1
random1
random2
random3
random4
#fix lines removed - match 1 second time
Start to grab from here: 1
#random1 removed cause is a dupe
random22131
#fix lines removed - match 2 that comes after 1
Start to grab from here: 2
random1546
random2561
or
# fix lines removed - match 1 first time and the second too
Start to grab from here: 1
random1
random2
random3
random4
#random1 removed cause is a dupe
random22131
#fix lines removed - match 2 that comes after 1
Start to grab from here: 2
random1546
random2561
The second output is better that the first one. Some other unix command magic is needed.
sed awk regular-expression sort
sed awk regular-expression sort
edited Oct 24 '16 at 19:29
asked Oct 24 '16 at 18:40
heisen
6017
6017
This is really helpful for grabbing the stack trace for a particular thread from java jstack output. Glad I found this Q&A!
– BenjaminBallard
Sep 14 at 14:00
add a comment |
This is really helpful for grabbing the stack trace for a particular thread from java jstack output. Glad I found this Q&A!
– BenjaminBallard
Sep 14 at 14:00
This is really helpful for grabbing the stack trace for a particular thread from java jstack output. Glad I found this Q&A!
– BenjaminBallard
Sep 14 at 14:00
This is really helpful for grabbing the stack trace for a particular thread from java jstack output. Glad I found this Q&A!
– BenjaminBallard
Sep 14 at 14:00
add a comment |
2 Answers
2
active
oldest
votes
up vote
10
down vote
Using awk
Try:
$ awk '/Start to grab/,/^$/' prova.txt
Start to grab from here: 1
random1
random2
random3
random4
Start to grab from here: 2
random1546
random2561
Start to grab from here: 3
random45
random22131
/Start to grab/,/^$/ defines a range. It starts with any line that matches Start to grab and ends with the first empty line, ^$, that follows.
Using sed
With very similar logic:
$ sed -n '/Start to grab/,/^$/p' prova.txt
Start to grab from here: 1
random1
random2
random3
random4
Start to grab from here: 2
random1546
random2561
Start to grab from here: 3
random45
random22131
-n tells sed not to print anything unless we explicitly ask it to. /Start to grab/,/^$/p tells it to print any lines in the range defined by /Start to grab/,/^$/.
Your solution is perfect, I have edited my ask to add something. Relly apreciate your help. Thank you
– heisen
Oct 24 '16 at 19:08
add a comment |
up vote
0
down vote
I am posting an alternative solution as it may be useful to some peoples use cases. This solution does not comply exactly to the stated requirements, for the best solution see the answer from @John1024.
You can use awk with the Record Separator set to an empty string, awk will interpret these as blank newlines:
$ awk '/Start/' RS= prova.txt
Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random2
random3
random4
Start to grab from here: 2
fix1
fix2
fix3
fix4
random1546
random2561
Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random22131
This version does not preserve the blank newlines in the output. It will also show context before the match if present. This behaviour can be very useful when grepping for something in a file and you want to see the newline delimited block it is a part of, for example:
$ awk '/random1546/' RS= prova.txt
Start to grab from here: 2
fix1
fix2
fix3
fix4
random1546
random2561
For example I find this useful when grepping for things in ini files.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
10
down vote
Using awk
Try:
$ awk '/Start to grab/,/^$/' prova.txt
Start to grab from here: 1
random1
random2
random3
random4
Start to grab from here: 2
random1546
random2561
Start to grab from here: 3
random45
random22131
/Start to grab/,/^$/ defines a range. It starts with any line that matches Start to grab and ends with the first empty line, ^$, that follows.
Using sed
With very similar logic:
$ sed -n '/Start to grab/,/^$/p' prova.txt
Start to grab from here: 1
random1
random2
random3
random4
Start to grab from here: 2
random1546
random2561
Start to grab from here: 3
random45
random22131
-n tells sed not to print anything unless we explicitly ask it to. /Start to grab/,/^$/p tells it to print any lines in the range defined by /Start to grab/,/^$/.
Your solution is perfect, I have edited my ask to add something. Relly apreciate your help. Thank you
– heisen
Oct 24 '16 at 19:08
add a comment |
up vote
10
down vote
Using awk
Try:
$ awk '/Start to grab/,/^$/' prova.txt
Start to grab from here: 1
random1
random2
random3
random4
Start to grab from here: 2
random1546
random2561
Start to grab from here: 3
random45
random22131
/Start to grab/,/^$/ defines a range. It starts with any line that matches Start to grab and ends with the first empty line, ^$, that follows.
Using sed
With very similar logic:
$ sed -n '/Start to grab/,/^$/p' prova.txt
Start to grab from here: 1
random1
random2
random3
random4
Start to grab from here: 2
random1546
random2561
Start to grab from here: 3
random45
random22131
-n tells sed not to print anything unless we explicitly ask it to. /Start to grab/,/^$/p tells it to print any lines in the range defined by /Start to grab/,/^$/.
Your solution is perfect, I have edited my ask to add something. Relly apreciate your help. Thank you
– heisen
Oct 24 '16 at 19:08
add a comment |
up vote
10
down vote
up vote
10
down vote
Using awk
Try:
$ awk '/Start to grab/,/^$/' prova.txt
Start to grab from here: 1
random1
random2
random3
random4
Start to grab from here: 2
random1546
random2561
Start to grab from here: 3
random45
random22131
/Start to grab/,/^$/ defines a range. It starts with any line that matches Start to grab and ends with the first empty line, ^$, that follows.
Using sed
With very similar logic:
$ sed -n '/Start to grab/,/^$/p' prova.txt
Start to grab from here: 1
random1
random2
random3
random4
Start to grab from here: 2
random1546
random2561
Start to grab from here: 3
random45
random22131
-n tells sed not to print anything unless we explicitly ask it to. /Start to grab/,/^$/p tells it to print any lines in the range defined by /Start to grab/,/^$/.
Using awk
Try:
$ awk '/Start to grab/,/^$/' prova.txt
Start to grab from here: 1
random1
random2
random3
random4
Start to grab from here: 2
random1546
random2561
Start to grab from here: 3
random45
random22131
/Start to grab/,/^$/ defines a range. It starts with any line that matches Start to grab and ends with the first empty line, ^$, that follows.
Using sed
With very similar logic:
$ sed -n '/Start to grab/,/^$/p' prova.txt
Start to grab from here: 1
random1
random2
random3
random4
Start to grab from here: 2
random1546
random2561
Start to grab from here: 3
random45
random22131
-n tells sed not to print anything unless we explicitly ask it to. /Start to grab/,/^$/p tells it to print any lines in the range defined by /Start to grab/,/^$/.
answered Oct 24 '16 at 18:43
John1024
45.1k4101118
45.1k4101118
Your solution is perfect, I have edited my ask to add something. Relly apreciate your help. Thank you
– heisen
Oct 24 '16 at 19:08
add a comment |
Your solution is perfect, I have edited my ask to add something. Relly apreciate your help. Thank you
– heisen
Oct 24 '16 at 19:08
Your solution is perfect, I have edited my ask to add something. Relly apreciate your help. Thank you
– heisen
Oct 24 '16 at 19:08
Your solution is perfect, I have edited my ask to add something. Relly apreciate your help. Thank you
– heisen
Oct 24 '16 at 19:08
add a comment |
up vote
0
down vote
I am posting an alternative solution as it may be useful to some peoples use cases. This solution does not comply exactly to the stated requirements, for the best solution see the answer from @John1024.
You can use awk with the Record Separator set to an empty string, awk will interpret these as blank newlines:
$ awk '/Start/' RS= prova.txt
Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random2
random3
random4
Start to grab from here: 2
fix1
fix2
fix3
fix4
random1546
random2561
Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random22131
This version does not preserve the blank newlines in the output. It will also show context before the match if present. This behaviour can be very useful when grepping for something in a file and you want to see the newline delimited block it is a part of, for example:
$ awk '/random1546/' RS= prova.txt
Start to grab from here: 2
fix1
fix2
fix3
fix4
random1546
random2561
For example I find this useful when grepping for things in ini files.
add a comment |
up vote
0
down vote
I am posting an alternative solution as it may be useful to some peoples use cases. This solution does not comply exactly to the stated requirements, for the best solution see the answer from @John1024.
You can use awk with the Record Separator set to an empty string, awk will interpret these as blank newlines:
$ awk '/Start/' RS= prova.txt
Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random2
random3
random4
Start to grab from here: 2
fix1
fix2
fix3
fix4
random1546
random2561
Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random22131
This version does not preserve the blank newlines in the output. It will also show context before the match if present. This behaviour can be very useful when grepping for something in a file and you want to see the newline delimited block it is a part of, for example:
$ awk '/random1546/' RS= prova.txt
Start to grab from here: 2
fix1
fix2
fix3
fix4
random1546
random2561
For example I find this useful when grepping for things in ini files.
add a comment |
up vote
0
down vote
up vote
0
down vote
I am posting an alternative solution as it may be useful to some peoples use cases. This solution does not comply exactly to the stated requirements, for the best solution see the answer from @John1024.
You can use awk with the Record Separator set to an empty string, awk will interpret these as blank newlines:
$ awk '/Start/' RS= prova.txt
Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random2
random3
random4
Start to grab from here: 2
fix1
fix2
fix3
fix4
random1546
random2561
Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random22131
This version does not preserve the blank newlines in the output. It will also show context before the match if present. This behaviour can be very useful when grepping for something in a file and you want to see the newline delimited block it is a part of, for example:
$ awk '/random1546/' RS= prova.txt
Start to grab from here: 2
fix1
fix2
fix3
fix4
random1546
random2561
For example I find this useful when grepping for things in ini files.
I am posting an alternative solution as it may be useful to some peoples use cases. This solution does not comply exactly to the stated requirements, for the best solution see the answer from @John1024.
You can use awk with the Record Separator set to an empty string, awk will interpret these as blank newlines:
$ awk '/Start/' RS= prova.txt
Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random2
random3
random4
Start to grab from here: 2
fix1
fix2
fix3
fix4
random1546
random2561
Start to grab from here: 1
fix1
fix2
fix3
fix4
random1
random22131
This version does not preserve the blank newlines in the output. It will also show context before the match if present. This behaviour can be very useful when grepping for something in a file and you want to see the newline delimited block it is a part of, for example:
$ awk '/random1546/' RS= prova.txt
Start to grab from here: 2
fix1
fix2
fix3
fix4
random1546
random2561
For example I find this useful when grepping for things in ini files.
answered 21 hours ago
htaccess
1,719145
1,719145
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%2f318595%2fgrep-starting-from-a-fixed-text-until-the-first-blank-line%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
This is really helpful for grabbing the stack trace for a particular thread from java jstack output. Glad I found this Q&A!
– BenjaminBallard
Sep 14 at 14:00