Search code examples
bashawksedgrepcut

Splitting content of file and make it in order


I have a file like so:

{A{AAA} B{BBB} test {CCC CCC
}}
{E{EEE} F{FFF} test {GGG GGG
}}
{H{HHH} I{III} test {JJJ -JJJ
}}
{K{KKK} L{LLL} test {MMM 
}}

Updated

I want to use linux commands in order to have the following output:

AAA:BBB:CCC CCC
EEE:FFF:GGG GGG
HHH:III:JJJ -JJJ
KKK:LLL:MMM

Solution

  • Using gnu-awk you can do this:

    awk -v RS='}}' -v FPAT='{[^{}]+(}|\n)' -v OFS=':' '{for (i=1; i<=NF; i++) {
                  gsub(/[{}]|\n/, "", $i); printf "%s%s", $i, (i<NF)?OFS:ORS}}' file
    AAA:BBB:CCC CCC
    EEE:FFF:GGG GGG
    HHH:III:JJJ -JJJ
    KKK:LLL:MMM
    
    • -v RS='}}' will break each record using }} text
    • -v FPAT='{[^{}]+(}|\n)' will split field using given regex. Regex matches each field that starts with { and matches anything but { and } followed by } or a newline.
    • -v OFS=':' sets output field separator as :
    • gsub(/[{}]|\n/, "", $i) removes { or } or newline from each field

    Shorter command (thanks to JoseRicardo):

    awk -v RS='}}' -v FPAT='{[^{}]+(}|\n)' -v OFS=':' '{$1=$1} gsub(/[{}]|\n/, "")' file
    

    or even this:

    awk -v FPAT='{[^{}]{2,}' -v OFS=':' '{$1=$1} gsub(/[{}]/, "")' file