Match patternA and print it only when patternB is matched including the following line
Clash Royale CLAN TAG#URR8PPP
up vote
4
down vote
favorite
I am looking to get all the lines which have the word 'search_string' + the line after it + the line matching 'mod' before it.
I tried:
grep -n 'mod|search_string' ip | grep --before 1 search_string> inter
grep -n --after 1 search_string ip >> inter
sort -t':' -k1,1n -u inter -o op
Is there a better way?
File:
mod start1
some lines
mod start2
other lines
mod start3
many other lines
search_string yada yada
hello
many other lines
search_string yada yada
bye
mod start4
search_string baba baba
this too
mod start5
Expected output :
mod start3
search_string yada yada
hello
search_string yada yada
bye
mod start4
search_string baba baba
this too
text-processing awk sed
add a comment |Â
up vote
4
down vote
favorite
I am looking to get all the lines which have the word 'search_string' + the line after it + the line matching 'mod' before it.
I tried:
grep -n 'mod|search_string' ip | grep --before 1 search_string> inter
grep -n --after 1 search_string ip >> inter
sort -t':' -k1,1n -u inter -o op
Is there a better way?
File:
mod start1
some lines
mod start2
other lines
mod start3
many other lines
search_string yada yada
hello
many other lines
search_string yada yada
bye
mod start4
search_string baba baba
this too
mod start5
Expected output :
mod start3
search_string yada yada
hello
search_string yada yada
bye
mod start4
search_string baba baba
this too
text-processing awk sed
In your example you havesearch_string
andsearch string
. Are they both valid?
â Kamil Maciorowski
Aug 25 at 19:28
The output of the commands you show do not match the expected output you present. Which is correct ?
â Isaac
Aug 25 at 19:34
It was a typo. Corrected it to show search_string. The final results should match the expected results. I was only trying to show the convoluted steps I was thinking of and not getting proper results.
â romi
Aug 25 at 20:27
add a comment |Â
up vote
4
down vote
favorite
up vote
4
down vote
favorite
I am looking to get all the lines which have the word 'search_string' + the line after it + the line matching 'mod' before it.
I tried:
grep -n 'mod|search_string' ip | grep --before 1 search_string> inter
grep -n --after 1 search_string ip >> inter
sort -t':' -k1,1n -u inter -o op
Is there a better way?
File:
mod start1
some lines
mod start2
other lines
mod start3
many other lines
search_string yada yada
hello
many other lines
search_string yada yada
bye
mod start4
search_string baba baba
this too
mod start5
Expected output :
mod start3
search_string yada yada
hello
search_string yada yada
bye
mod start4
search_string baba baba
this too
text-processing awk sed
I am looking to get all the lines which have the word 'search_string' + the line after it + the line matching 'mod' before it.
I tried:
grep -n 'mod|search_string' ip | grep --before 1 search_string> inter
grep -n --after 1 search_string ip >> inter
sort -t':' -k1,1n -u inter -o op
Is there a better way?
File:
mod start1
some lines
mod start2
other lines
mod start3
many other lines
search_string yada yada
hello
many other lines
search_string yada yada
bye
mod start4
search_string baba baba
this too
mod start5
Expected output :
mod start3
search_string yada yada
hello
search_string yada yada
bye
mod start4
search_string baba baba
this too
text-processing awk sed
edited Aug 26 at 7:52
ñÃÂsýù÷
15.5k92563
15.5k92563
asked Aug 25 at 18:46
romi
235
235
In your example you havesearch_string
andsearch string
. Are they both valid?
â Kamil Maciorowski
Aug 25 at 19:28
The output of the commands you show do not match the expected output you present. Which is correct ?
â Isaac
Aug 25 at 19:34
It was a typo. Corrected it to show search_string. The final results should match the expected results. I was only trying to show the convoluted steps I was thinking of and not getting proper results.
â romi
Aug 25 at 20:27
add a comment |Â
In your example you havesearch_string
andsearch string
. Are they both valid?
â Kamil Maciorowski
Aug 25 at 19:28
The output of the commands you show do not match the expected output you present. Which is correct ?
â Isaac
Aug 25 at 19:34
It was a typo. Corrected it to show search_string. The final results should match the expected results. I was only trying to show the convoluted steps I was thinking of and not getting proper results.
â romi
Aug 25 at 20:27
In your example you have
search_string
and search string
. Are they both valid?â Kamil Maciorowski
Aug 25 at 19:28
In your example you have
search_string
and search string
. Are they both valid?â Kamil Maciorowski
Aug 25 at 19:28
The output of the commands you show do not match the expected output you present. Which is correct ?
â Isaac
Aug 25 at 19:34
The output of the commands you show do not match the expected output you present. Which is correct ?
â Isaac
Aug 25 at 19:34
It was a typo. Corrected it to show search_string. The final results should match the expected results. I was only trying to show the convoluted steps I was thinking of and not getting proper results.
â romi
Aug 25 at 20:27
It was a typo. Corrected it to show search_string. The final results should match the expected results. I was only trying to show the convoluted steps I was thinking of and not getting proper results.
â romi
Aug 25 at 20:27
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
4
down vote
accepted
awk '
$0 ~ /mod/ md=$0
$0 ~ /search_string/ if(md!="") print md ; md="" ; print; getline; print
'
Explanation:
- A line containing
mod
is saved asmd
. - A line containing
search_string
triggers printing the previously savedmd
, the line itself and the next line. if(md!="")
andmd=""
are there to make sure you don't get duplicatedmod
lines when there are manysearch_string
-s under a singlemod
(mod start3
in your example).
Note:
- A line containing both
mod
andsearch_string
will break this logic.
Kamil, Thankyou! This works for me!
â romi
Aug 26 at 7:31
add a comment |Â
up vote
2
down vote
Your file contains "carriage return" characters. It is better to remove them in Unix. To print what the sequence of commands you posted print (with carriage returns removed), try:
awk 'gsub(/r/,"")
/mod/ a = $0
/search_string/ if(a!="")print(a);a=""
print;getline;print
' infile
Or as a one-liner:
$ awk 'gsub(/r/,"")/mod/a=$0/search_string/if(a!="")print(a);a=""print;getline;print' infile
mod start3
search_string yada yada
hello
search_string yada yada
bye
mod start4
search_string baba baba
this too
As it is possible to use a multi character record separator in (GNU) awk we can set the record separator to mod
and print only records that contain search_string
. The printf is required to reconstruct the original record.
To print what you posted as "Expected output" try:
awk '/search_string/printf("mod%s", $0)' RS=mod infile
add a comment |Â
up vote
0
down vote
If you want this in a Python script:
# Read file into memory.
with open('myfile.txt') as f:
lines = [line.rstrip() for line in f]
# Loops through lines backwards, looking for string and optionally mod.
output_lines = list()
find_mod = False
for i, line in enumerate(lines[::-1]):
if 'search_string' in line:
output_lines.append(lines[::-1][i-1])
output_lines.append(lines[::-1][i])
find_mod = True
elif find_mod and 'mod' in line:
output_lines.append(lines[::-1][i])
find_mod=False
print("n".join(output_lines[::-1]))
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
awk '
$0 ~ /mod/ md=$0
$0 ~ /search_string/ if(md!="") print md ; md="" ; print; getline; print
'
Explanation:
- A line containing
mod
is saved asmd
. - A line containing
search_string
triggers printing the previously savedmd
, the line itself and the next line. if(md!="")
andmd=""
are there to make sure you don't get duplicatedmod
lines when there are manysearch_string
-s under a singlemod
(mod start3
in your example).
Note:
- A line containing both
mod
andsearch_string
will break this logic.
Kamil, Thankyou! This works for me!
â romi
Aug 26 at 7:31
add a comment |Â
up vote
4
down vote
accepted
awk '
$0 ~ /mod/ md=$0
$0 ~ /search_string/ if(md!="") print md ; md="" ; print; getline; print
'
Explanation:
- A line containing
mod
is saved asmd
. - A line containing
search_string
triggers printing the previously savedmd
, the line itself and the next line. if(md!="")
andmd=""
are there to make sure you don't get duplicatedmod
lines when there are manysearch_string
-s under a singlemod
(mod start3
in your example).
Note:
- A line containing both
mod
andsearch_string
will break this logic.
Kamil, Thankyou! This works for me!
â romi
Aug 26 at 7:31
add a comment |Â
up vote
4
down vote
accepted
up vote
4
down vote
accepted
awk '
$0 ~ /mod/ md=$0
$0 ~ /search_string/ if(md!="") print md ; md="" ; print; getline; print
'
Explanation:
- A line containing
mod
is saved asmd
. - A line containing
search_string
triggers printing the previously savedmd
, the line itself and the next line. if(md!="")
andmd=""
are there to make sure you don't get duplicatedmod
lines when there are manysearch_string
-s under a singlemod
(mod start3
in your example).
Note:
- A line containing both
mod
andsearch_string
will break this logic.
awk '
$0 ~ /mod/ md=$0
$0 ~ /search_string/ if(md!="") print md ; md="" ; print; getline; print
'
Explanation:
- A line containing
mod
is saved asmd
. - A line containing
search_string
triggers printing the previously savedmd
, the line itself and the next line. if(md!="")
andmd=""
are there to make sure you don't get duplicatedmod
lines when there are manysearch_string
-s under a singlemod
(mod start3
in your example).
Note:
- A line containing both
mod
andsearch_string
will break this logic.
edited Aug 26 at 13:34
answered Aug 25 at 19:27
Kamil Maciorowski
1,0791523
1,0791523
Kamil, Thankyou! This works for me!
â romi
Aug 26 at 7:31
add a comment |Â
Kamil, Thankyou! This works for me!
â romi
Aug 26 at 7:31
Kamil, Thankyou! This works for me!
â romi
Aug 26 at 7:31
Kamil, Thankyou! This works for me!
â romi
Aug 26 at 7:31
add a comment |Â
up vote
2
down vote
Your file contains "carriage return" characters. It is better to remove them in Unix. To print what the sequence of commands you posted print (with carriage returns removed), try:
awk 'gsub(/r/,"")
/mod/ a = $0
/search_string/ if(a!="")print(a);a=""
print;getline;print
' infile
Or as a one-liner:
$ awk 'gsub(/r/,"")/mod/a=$0/search_string/if(a!="")print(a);a=""print;getline;print' infile
mod start3
search_string yada yada
hello
search_string yada yada
bye
mod start4
search_string baba baba
this too
As it is possible to use a multi character record separator in (GNU) awk we can set the record separator to mod
and print only records that contain search_string
. The printf is required to reconstruct the original record.
To print what you posted as "Expected output" try:
awk '/search_string/printf("mod%s", $0)' RS=mod infile
add a comment |Â
up vote
2
down vote
Your file contains "carriage return" characters. It is better to remove them in Unix. To print what the sequence of commands you posted print (with carriage returns removed), try:
awk 'gsub(/r/,"")
/mod/ a = $0
/search_string/ if(a!="")print(a);a=""
print;getline;print
' infile
Or as a one-liner:
$ awk 'gsub(/r/,"")/mod/a=$0/search_string/if(a!="")print(a);a=""print;getline;print' infile
mod start3
search_string yada yada
hello
search_string yada yada
bye
mod start4
search_string baba baba
this too
As it is possible to use a multi character record separator in (GNU) awk we can set the record separator to mod
and print only records that contain search_string
. The printf is required to reconstruct the original record.
To print what you posted as "Expected output" try:
awk '/search_string/printf("mod%s", $0)' RS=mod infile
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Your file contains "carriage return" characters. It is better to remove them in Unix. To print what the sequence of commands you posted print (with carriage returns removed), try:
awk 'gsub(/r/,"")
/mod/ a = $0
/search_string/ if(a!="")print(a);a=""
print;getline;print
' infile
Or as a one-liner:
$ awk 'gsub(/r/,"")/mod/a=$0/search_string/if(a!="")print(a);a=""print;getline;print' infile
mod start3
search_string yada yada
hello
search_string yada yada
bye
mod start4
search_string baba baba
this too
As it is possible to use a multi character record separator in (GNU) awk we can set the record separator to mod
and print only records that contain search_string
. The printf is required to reconstruct the original record.
To print what you posted as "Expected output" try:
awk '/search_string/printf("mod%s", $0)' RS=mod infile
Your file contains "carriage return" characters. It is better to remove them in Unix. To print what the sequence of commands you posted print (with carriage returns removed), try:
awk 'gsub(/r/,"")
/mod/ a = $0
/search_string/ if(a!="")print(a);a=""
print;getline;print
' infile
Or as a one-liner:
$ awk 'gsub(/r/,"")/mod/a=$0/search_string/if(a!="")print(a);a=""print;getline;print' infile
mod start3
search_string yada yada
hello
search_string yada yada
bye
mod start4
search_string baba baba
this too
As it is possible to use a multi character record separator in (GNU) awk we can set the record separator to mod
and print only records that contain search_string
. The printf is required to reconstruct the original record.
To print what you posted as "Expected output" try:
awk '/search_string/printf("mod%s", $0)' RS=mod infile
edited Aug 25 at 20:33
answered Aug 25 at 19:25
Isaac
6,8001834
6,8001834
add a comment |Â
add a comment |Â
up vote
0
down vote
If you want this in a Python script:
# Read file into memory.
with open('myfile.txt') as f:
lines = [line.rstrip() for line in f]
# Loops through lines backwards, looking for string and optionally mod.
output_lines = list()
find_mod = False
for i, line in enumerate(lines[::-1]):
if 'search_string' in line:
output_lines.append(lines[::-1][i-1])
output_lines.append(lines[::-1][i])
find_mod = True
elif find_mod and 'mod' in line:
output_lines.append(lines[::-1][i])
find_mod=False
print("n".join(output_lines[::-1]))
add a comment |Â
up vote
0
down vote
If you want this in a Python script:
# Read file into memory.
with open('myfile.txt') as f:
lines = [line.rstrip() for line in f]
# Loops through lines backwards, looking for string and optionally mod.
output_lines = list()
find_mod = False
for i, line in enumerate(lines[::-1]):
if 'search_string' in line:
output_lines.append(lines[::-1][i-1])
output_lines.append(lines[::-1][i])
find_mod = True
elif find_mod and 'mod' in line:
output_lines.append(lines[::-1][i])
find_mod=False
print("n".join(output_lines[::-1]))
add a comment |Â
up vote
0
down vote
up vote
0
down vote
If you want this in a Python script:
# Read file into memory.
with open('myfile.txt') as f:
lines = [line.rstrip() for line in f]
# Loops through lines backwards, looking for string and optionally mod.
output_lines = list()
find_mod = False
for i, line in enumerate(lines[::-1]):
if 'search_string' in line:
output_lines.append(lines[::-1][i-1])
output_lines.append(lines[::-1][i])
find_mod = True
elif find_mod and 'mod' in line:
output_lines.append(lines[::-1][i])
find_mod=False
print("n".join(output_lines[::-1]))
If you want this in a Python script:
# Read file into memory.
with open('myfile.txt') as f:
lines = [line.rstrip() for line in f]
# Loops through lines backwards, looking for string and optionally mod.
output_lines = list()
find_mod = False
for i, line in enumerate(lines[::-1]):
if 'search_string' in line:
output_lines.append(lines[::-1][i-1])
output_lines.append(lines[::-1][i])
find_mod = True
elif find_mod and 'mod' in line:
output_lines.append(lines[::-1][i])
find_mod=False
print("n".join(output_lines[::-1]))
answered Aug 26 at 2:36
user1717828
1,59611125
1,59611125
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%2f464824%2fmatch-patterna-and-print-it-only-when-patternb-is-matched-including-the-followin%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
In your example you have
search_string
andsearch string
. Are they both valid?â Kamil Maciorowski
Aug 25 at 19:28
The output of the commands you show do not match the expected output you present. Which is correct ?
â Isaac
Aug 25 at 19:34
It was a typo. Corrected it to show search_string. The final results should match the expected results. I was only trying to show the convoluted steps I was thinking of and not getting proper results.
â romi
Aug 25 at 20:27