Home > Net >  Move rows to column in bash
Move rows to column in bash

Time:08-30

How can I move the below sample data to columns?

"FromPort": 80,
"CidrIp": "10.0.0.0/8",
"CidrIp": "XX.XX.XX.XX/32",
"CidrIp": "XX.XX.XX.XX/32",
"CidrIp": "XX.XX.XX.XX/32",
"FromPort": 443,
"CidrIp": "0.0.0.0/0",
"CidrIpv6": "::/0",

It should look like the below:

"FromPort": 80, "CidrIp": "10.0.0.0/8", "CidrIp": "XX.XX.XX.XX/32", "CidrIp": "XX.XX.XX.XX/32", "CidrIp": "XX.XX.XX.XX/32",

"FromPort": 443, "CidrIp": "0.0.0.0/0", "CidrIpv6": "::/0",

CodePudding user response:

It looks like you want to do two things:

  • Change the newline after a comma to a space
  • Add a newline before "FromPort" if it's not the first one (because you just removed the newline preceding it).

This seems to do it:

$ perl -pe 's/,\R/, /; s/^(?="FromPort)/\n/ if $n  ;' test.txt
"FromPort": 80, "CidrIp": "10.0.0.0/8", "CidrIp": "XX.XX.XX.XX/32", "CidrIp": "XX.XX.XX.XX/32", "CidrIp": "XX.XX.XX.XX/32",
"FromPort": 443, "CidrIp": "0.0.0.0/0", "CidrIpv6": "::/0",

If there's something else, you'll need to be more specific in the question.

  •  Tags:  
  • bash
  • Related