Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Need help Parsing a Input in Bash

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. Can someone please help:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading