Issue
I am new to Bash and have an input file.log with the following contents:
ok: [test_performance=10.50.100.82] => {
"msg": [
[
"DeviceName:78BAY08V2A/10",
"DeviceName:GH7AK1A02097/10"
]]}
ok: [test_1=10.50.101.84] => {
"msg": [
[
"DeviceName:8K251FDD4000D1/13",
"DeviceName:99071JEGR10369/12"
]]}
I want to get output as below:
test_performance,10.50.100.82,78BAY08V2A/10
test_performance,10.50.100.82,GH7AK1A02097/10
test_1,10.50.101.84,8K251FDD4000D1/13
test_1,10.50.101.84,99071JEGR10369/12
I tried below command but it is not working. What can I try next?
grep -C 3 "DeviceName:" file.log | xargs | sed 's/ok/\n/g' | sed 's|[]{}[]||g' | sed 's/msg//g' | sed 's/ =>/,/g' | awk -F ":" '{print $4,$5}' | awk -F "--" '{print $1}' | sed 's/, /,/g' | sed 's/, /-/g' | sed 's/ //g' | grep -E 'arda|kobra|pipeline|performance|integration|feature|kare|yjr' | sed 's/=/,/g' | sed 's/\(.*\)-/\1,/' | sed 's/-/,/g' | tee -a file2.csv
Solution
With GNU awk use "
, :
, =
, ]
and [
as field separators:
awk -F '[":=\\]\\[]' '/^ok/{ok=$3 "," $4} /DeviceName/{print ok "," $3}' file.log
Output:
test_performance,10.50.100.82,78BAY08V2A/10 test_performance,10.50.100.82,GH7AK1A02097/10 test_1,10.50.101.84,8K251FDD4000D1/13 test_1,10.50.101.84,99071JEGR10369/12
Answered By - Cyrus Answer Checked By - Robin (WPSolving Admin)