Skip to content
Advertisement

How to replace newlines between brackets

I have log file similar to this format

test {
seq-cont {
                        0,
                        67,
                        266
                        },
                grp-id 505
        }
}
test{
        test1{
                val
        }
}

Here is the echo command to produce that output

$ echo -e "test {nseq-cont {nttt0,nttt67,nttt266nttt},nttgrp-id 505nt}n}ntest{nttest1{nttvalnt}n}n"

Question is how to remove all whitespace between seq-cont { and the next } that may be multiple in the file.

I want the output to be like this. Preferably use sed to produce the output.

test{seq-cont{0,67,266},
                       grp-id 505
        }
}
test{
        test1{
                val
        }
}

Efforts by OP: Here is the one somewhat worked but not exactly what I wanted:

sed ':a;N;/{/s/[[:space:]]+//;/}/s/}/}/;ta;P;D' logfile

Advertisement

Answer

It can be done using gnu-awk with a custom RS regex that matches { and closing }:

awk -v RS='{[^}]+}' 'NR==1 {gsub(/[[:space:]]+/, "", RT)} {ORS=RT} 1' file

test {seq-cont{0,67,266},
                grp-id 505
        }
}
test{
        test1{
                val
        }
}

Here:

  • NR==1 {gsub(/[[:space:]]+/, "", RT)}: For the first record replace all whitespaces (including line breaks) with empty string.
  • {ORS=RT}: Set ORS to whatever text we captured in RS

PS: Remove NR==1 if you want to do this for entire file.

2 People found this is helpful
Advertisement