MLSC
MLSC

Reputation: 5972

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

Upvotes: 0

Views: 47

Answers (3)

NeronLeVelu
NeronLeVelu

Reputation: 10039

for this specific format

sed -n 's/...//;s/}[^{]*//g;s/{/:/gp' YourFile

Upvotes: 1

choroba
choroba

Reputation: 241908

Perl solution

perl -nwe 'print join ":", /{([^{}]{2,})/g' file

The regular expression extracts groups of 2 or more non-curlies following a curlie, they are then printed separated with colons.

Upvotes: 1

anubhava
anubhava

Reputation: 785256

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

Upvotes: 3

Related Questions