Issue
I have some text file data that I am parsing with SED, AWK and Perl.
product {
name { thing1 }
customers {
mary { }
freddy { }
bob {
spouse betty
}
}
}
From the "customers" section, I am trying to get output similar to:
mary{ }
freddy{ }
bob{spouse betty}
Using: sed -n -e "/customers {/,/}/{/customers {/d;/}/d;p;}" $file'
This is the output:
mary { }
freddy { }
bob {
spouse betty
}
How can I concatenate the "bob" customer to one line and remove the extra spaces? The main reason for this specific output is that I am writing a script to grab the "customer" fields and other fields in the text file, then outputting them to a csv file. Which will look something like this. I know this would probably be easier in another language, but bash is what I know.
output.csv
product,customers,another_column
thing1,mary{ } freddy{ } bob{spouse betty},something_else
Solution
The data happens to have valid tcl list syntax:
set f [open "input.file"]
set data [dict create {*}[read $f]]
close $f
set name [string trim [dict get $data product name]]
dict for {key val} [dict get $data product customers] {
lappend customers [format "%s{%s}" $key [string trim $val]]
}
set f [open "output.csv" w]
puts $f "product,customers,another_column"
puts $f [join [list $name [join $customers] "something_else"] ,]
close $f
creates output.csv with
product,customers,another_column
thing1,mary{} freddy{} bob{spouse betty},something_else
Answered By - glenn jackman Answer Checked By - David Goodson (WPSolving Volunteer)