0

I have text file with this content:

4

:
      ID: 5
      table: 4
      numplan: no_change
      accs-cat: no_change
      cld-rule: $
      cld-type: no_change
      cld-numplan: no_change
      clg-rule: '----------+79975532211'
--
    17:
      ID: 19
      table: 7
      numplan: no_change
      accs-cat: no_change
      cld-rule: $
      cld-type: no_change
      cld-numplan: no_change
      clg-rule: '----------+79975532211'
--
    18:
      ID: 20
      table: 3
      numplan: no_change
      accs-cat: no_change
      cld-rule: $
      cld-type: no_change
      cld-numplan: no_change
      clg-rule: '----------+79975532211'
--
    19:
      ID: 21
      table: 5
      numplan: no_change
      accs-cat: no_change
      cld-rule: $
      cld-type: no_change
      cld-numplan: no_change
      clg-rule: '----------+79975532211'

How I can grep results like this:

4:
      ID: 5
      table: 4
      numplan: no_change
      accs-cat: no_change
      cld-rule: $
      cld-type: no_change
      cld-numplan: no_change
      clg-rule: '----------+79975532211'

Then I grep this result :

17:
      ID: 19
      table: 7
      numplan: no_change
      accs-cat: no_change
      cld-rule: $
      cld-type: no_change
      cld-numplan: no_change
      clg-rule: '----------+79975532211'

Then I have this result:

18:
      ID: 20
      table: 3
      numplan: no_change
      accs-cat: no_change
      cld-rule: $
      cld-type: no_change
      cld-numplan: no_change
      clg-rule: '----------+79975532211'

Then I have this result:

19:
      ID: 21
      table: 5
      numplan: no_change
      accs-cat: no_change
      cld-rule: $
      cld-type: no_change
      cld-numplan: no_change
      clg-rule: '----------+79975532211'

I need grep this results in one Bash script. How to do it?

I need get first result, and then next results in my text file

IBam
  • 5,483
  • 1
  • 21
  • 35
Darkwind
  • 197
  • 5
  • 17

2 Answers2

1

This will give you record 4..

grep -A8 '[[:space:]]*4:' file

and so on.. The command takes in to account, that the record will be 8 lines long, -A8 will output the matching line and the 8 lines following it.

You can create a little loop to execute the command for every record of interest and redirect the output of the loop to a text.file:

for i in 4 17 18 19 ; do
    grep -E -A8 "[[:space:]]*$i:" input.txt
done > output.txt

Alternatively you could use awk using a record separator RS="\n *--\n":

awk -F$'\n' '$1 ~ /\y(4|17|18|19):/' RS="\n *--\n" input.txt > output.txt
hek2mgl
  • 133,888
  • 21
  • 210
  • 235
0

Thank you! It's work!

I use this:

for i in 4 17 18 19 ; do
    grep -E -A8 "[[:space:]]*$i:" input.txt
done > output.txt
Darkwind
  • 197
  • 5
  • 17