awk to escape space in name

I am searching a file and not able to escape a space if $i has a space in the name. I tried escaping it with a \ and also trying to add it to allow for spaces in the search. Neither are correct as the first awk only outputs path and the second awk doesn't run. Thank you :).

first awk

awk -F"[]\":{}, ]*" '
        {for (i=1; i<NF; i++)   {if ($i =="path") print $(i+1)
                                 if ($i =="Sample \ Name") print $(i+1)
                                }
        }
' file

second awk

awk -F"[]\s":{}, ]*" '
        {for (i=1; i<NF; i++)   {if ($i =="path") print $(i+1)
                                 if ($i =="Sample \ Name") print $(i+1)
                                }
        }
' file

file

"path": "/results/analysis/output/Home/Auto_user_S5-00580-4-Medexome_65_028/plugin_out/FileExporter_out.52", "Sample Name": "MEV37",

desired output

/results/analysis/output/Home/Auto_user_S5-00580-4-Medexome_65_028/plugin_out/FileExporter_out.52
MEV37

Hello cmccabe,

If your Input_file is same as your sample Input_file then following may help you in same.

awk -F"[\"|,:]" '{for(i=1;i<=NF;i++){if($i == "path"){print $5};if($i == "Sample Name"){print $11}}}'  Input_file

Thanks,
R. Singh

1 Like

I get the desired output by tweaking the FS in your "first awk"...

awk -F"\"[,:]* *\"*" '{
    for (i=1; i<NF; i++) {
        if ($i ~ "path") print $(i+1)
        else if ($i ~ "Sample Name") print $(i+1)
    }
}' file
1 Like

Thank you both :).