Search code examples
powershellawksedgreptext-parsing

how to parse data in yaml from column to row


My final data after parsing from a yaml file is like this in text file :

        - id: 200
          addr: 10.242.57.129/27
        - id: 210
          addr: 10.242.57.161/25
        - id: 300
          addr: 10.244.26.1/24

I wanted to convert to this and save it as txt with shell scripting ( bash):

200,10.242.57.125,27
210,10.242.57.162,25
300,10.244.26.11,24 

Can anyone help for that?

Need to get the command shell for this (with awk, grep , sed , ...).

I used the following code but it doesn't work:

grep -F -e "id:" | cut -c 14-16 -e "addr:" | cut -c 16-35 ip.txt | paste -d , - -

Solution

  • Using GNU sed

    $ sed -E '/-/{N;s~[^0-9]*([0-9]+)\n[^0-9]*([0-9.]+)/([0-9]+)~\1,\2,\3~}' input_file
    200,10.242.57.129,27
    210,10.242.57.161,25
    300,10.244.26.1,24