V
V
Valeriu1472017-02-10 09:49:40
bash
Valeriu147, 2017-02-10 09:49:40

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|

Actually, I should output, one by one, the lines containing, "I am the Rock" "I am the Cena" "I am the CM Punk". Strictly in this sequence, one line after one, which contain only these phrases, it should turn out like this:
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|

It seems that everything is simple, I try this command:
grep "I am the Rock\|I am the Cena\|I am the CM Punk" textfile

In the output I get:
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|

As you can see I have duplicate values ​​with index 4 and 8
4 Ottava Amsterdam |I am the Rock|

8 Moscow Oslo |I am the Cena|

I can't figure it out, I've tried sed, awk, egrep in a similar way. But something doesn't work, what could I have done wrong? Give me an idea please.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
abcd0x00, 2017-02-11
@Valeriu147

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

Conclusion
[[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]$

V
Viktor Taran, 2017-02-10
@shambler81

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 question

Ask a Question

731 491 924 answers to any question