1

i've been searching for a while but i dont find how to make it:

i have a file that contains a lot of things + the following informations:

pool_1: &pool_prod
        - ip: 10.1.60.5
          weight: 1
        - ip: 10.1.60.6
          weight: 1

    pool_prod_nl: &pool_prod_nl
        - ip: 10.1.60.47
          weight: 1

    pool_prod_it: &pool_prod_it
        - ip: 10.1.60.65
          weight: 1
        - ip: 10.1.60.83
          weight: 1

    pool_prod_it: &pool_prod_dek
        - ip: 10.1.60.68
          weight: 1

I'd like to keep the result:

pool_1: &pool_prod
    - ip: 10.1.60.5
      weight: 1
    - ip: 10.1.60.6
      weight: 1

pool_prod_it: &pool_prod_it
        - ip: 10.1.60.65
      weight: 1
    - ip: 10.1.60.83
      weight: 1

When i do

sed -n '/pool_/,/^$/{p; /^$/q}'

i can extract some informations that i listed on the first code but i dont succes to filter them more because they have the same patterns and infos between, just the number of lines or the number of occurence inside can make a difference, but i dont find how to make it.

I'll appreciate any help thanks in advance

lotusnoir
  • 47
  • 5

3 Answers3

1

awk to the rescue!

$ awk -F'\n' -v RS= -v ORS='\n\n' '/pool/ && NF==5' file

pool_1: &pool_prod
        - ip: 10.1.60.5
          weight: 1
        - ip: 10.1.60.6
          weight: 1

    pool_prod_it: &pool_prod_it
        - ip: 10.1.60.65
          weight: 1
        - ip: 10.1.60.83
          weight: 1

or change NF==5 with NF>4

karakfa
  • 66,216
  • 7
  • 41
  • 56
0

Assuming the sed expression you wrote is what you want then this will print out expressions that are greater than 5 lines and ignore those that aren't. You can adjust that number in if statement to whatever you'd like. This will however delete the contents of your input file (input.txt in my example) so you will have to play with that if you want it some other way.

#!/bin/bash

while true; do
    output_of_sed=$(sed -n '/pool_/,/^$/{p; /^$/q}' input.txt)
    output_number_of_lines=$(sed -n '/pool_/,/^$/{p; /^$/q}' input.txt | wc -l)

    sed -i "1, ${output_number_of_lines}d" input.txt

    if [[ ${output_number_of_lines} -gt 5 ]]; then
        echo "${output_of_sed}"
    fi

    if [[ ${output_number_of_lines} -le 1 ]]; then
        break;
    fi
done
Jacek Trociński
  • 882
  • 1
  • 8
  • 23
0

In awk:

$ awk '/^pool/{if(c>4) print b; c=b=""}{c++; b=b (b?ORS:"") $0}' file
pool_1: &pool_prod
        - ip: 10.1.60.5
          weight: 1
        - ip: 10.1.60.6
          weight: 1

pool_prod_it: &pool_prod_it
    - ip: 10.1.60.65
      weight: 1
    - ip: 10.1.60.83
      weight: 1

Explained:

/^pool/ {              # if record starts with pool
    if(c>4)            # if (buffer record) count > 4
        print b        # print buffer
    b=c=""             # reset buffer and counter
} {
    c++                # iterate counter
    b=b (b?ORS:"") $0  # build buffer with current record and ORS when needed
}

If the pool strings in fact are preceeded by space, change /^pool/ to for example /^ *pool.

James Brown
  • 36,089
  • 7
  • 43
  • 59