Search code examples
shellawkcut

Extract string based on pattern in shell script


I am writing a script and stuck at a point where I have to extract a string based on the pattern. I have tried using awk and cut but not be able to get the correct outcome.

These are 3 lines in my file and I am looking to extract 2nd last column

expected output

  1. MxMonitor_Marvel_PI49
  2. alert_manager
  3. MxMonitor_Marvel_PI49

I tried the below one which is the closest I get but it extracts only for few lines not all of them.

awk  -F"," '{print $10}' Filename.txt

File Content

./subsearch_nested_fa89eeb0810630b9_1626351940.6/metadata.csv:2:"read : [ admin ], write : [ admin ]",admin,"MxMonitor_Marvel_PI49",300
./scheduler__nobody_YWxlcnRfbWFuYWdlcg__RMD5922da96313b0bb40_at_1626282000_20762/metadata.csv:2:"read : [ splunk-system-user ], write : [ splunk-system-user ]","splunk-system-user","alert_manager",86400
./subsearch_admin__admin_TXhNb25pdG9yX01hcnZlbF9QSTQ5__search12_1626351937.20757776_1626351938.1/metadata.csv:2:"read : [ admin ], write : [ admin ]",admin,"MxMonitor_Marvel_PI49",300

Solution

  • You may try this shorter awk:

    awk '{gsub(/^.*,"|",.*/, "")} 1' file
    
    MxMonitor_Marvel_PI49
    alert_manager
    MxMonitor_Marvel_PI49
    

    So similar sed:

    sed -E 's/^.*,"|",.*//g' file