Answer the question
In order to leave comments, you need to log in
How to make a selection, with strict GREP on multiple lines?
Good afternoon. It has become such an interesting task, I need to do grep for three phrases, but not just grep and what would he look for throughout the document, these three words, but what would he output them one by one. I'll show you with an example.
I have a file like this:
1 London Paris |I am the Rock|
2 Moscow Kyiv |I am the Cena|
3 Kharkiv Dnepr |I am the CM Punk|
4 Ottava Amsterdam |I am the Rock|
5 Oslo New-Yourk |I am the Rock|
6 Lviv Varshava |I am the Cena|
7 Berlin Rome |I am the CM Punk|
8 Moscow Oslo |I am the Cena|
9 Kharkiv Kyiv |I am the Brock Lesnar|
1 London Paris |I am the Rock|
2 Moscow Kyiv |I am the Cena|
3 Kharkiv Dnepr |I am the CM Punk|
5 Oslo New-Yourk |I am the Rock|
6 Lviv Varshava |I am the Cena|
7 Berlin Rome |I am the CM Punk|
grep "I am the Rock\|I am the Cena\|I am the CM Punk" textfile
1 London Paris |I am the Rock|
2 Moscow Kyiv |I am the Cena|
3 Kharkiv Dnepr |I am the CM Punk|
4 Ottava Amsterdam |I am the Rock|
5 Oslo New-Yourk |I am the Rock|
6 Lviv Varshava |I am the Cena|
7 Berlin Rome |I am the CM Punk|
8 Moscow Oslo |I am the Cena|
4 Ottava Amsterdam |I am the Rock|
8 Moscow Oslo |I am the Cena|
Answer the question
In order to leave comments, you need to log in
Here did the same
How to write a script in Bash?
text="\
1 London Paris |I am the Rock|
2 Moscow Kyiv |I am the Cena|
3 Kharkiv Dnepr |I am the CM Punk|
4 Ottava Amsterdam |I am the Rock|
5 Oslo New-Yourk |I am the Rock|
6 Lviv Varshava |I am the Cena|
7 Berlin Rome |I am the CM Punk|
8 Moscow Oslo |I am the Cena|
9 Kharkiv Kyiv |I am the Brock Lesnar|
"
fsm()
{
awk '{
switch (state) {
case 0:
if (/Rock/) {
out = $0
state = 1
}
break
case 1:
if (/Rock/) {
out = $0;
} else if (/Cena/) {
out = out"\n"$0
state = 2
} else {
out = ""
state = 0
}
break
case 2:
if (/Punk/) {
out = out"\n"$0
print out
out = ""
state = 0
} else {
out = ""
state = 0
}
break
}
}'
}
echo "$text" | fsm
[[email protected] tmp]$ echo "$text" | fsm
1 London Paris |I am the Rock|
2 Moscow Kyiv |I am the Cena|
3 Kharkiv Dnepr |I am the CM Punk|
5 Oslo New-Yourk |I am the Rock|
6 Lviv Varshava |I am the Cena|
7 Berlin Rome |I am the CM Punk|
[[email protected] tmp]$
It ?
https://habrahabr.ru/sandbox/102954/
grep - can only work with a string! do not expect him to work with data arrays
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question