1

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

4 Answers 4

5
$ awk -F'"' '{print $(NF-1)}' file
MxMonitor_Marvel_PI49
alert_manager
MxMonitor_Marvel_PI49
Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

3

With your shown samples, please try following code. Set field separator as , and remove starting, ending " from 2nd last field and finally print it.

awk -F, '{gsub(/^"|"$/,"",$(NF-1));print $(NF-1)}' Input_file

Comments

1

If the field separator is a comma and the values can be optionally wrapped in double quotes, for the example data you might set the field separator to a comma between optional double quotes using "?,"?

awk -F '"?,"?' '{print $(NF-1)}' file

Output

MxMonitor_Marvel_PI49
alert_manager
MxMonitor_Marvel_PI49

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.