Issue
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",
Solution
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",
You can also do this with the "flip flop" operator, but it's a bit longer. In scalar context, the ..
starts in a false state and only evaluates its left hand side. With just a number, that's really testing the line number ($. == 2
). Once that becomes true, the ..
stays true until the right hand is true (eof()
). It's a bit weird if you aren't used to it which is why I did the simple thing before. It bugged me that I didn't mention this, so here it is:
$ perl -pe 's/,\R/, /; s/^(?="FromPort)/\n/ if 2 .. eof();' input.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.
Answered By - brian d foy Answer Checked By - David Goodson (WPSolving Volunteer)