简体   繁体   中英

Regular expression to return part of a string, from within a file, in bash script

Ok I'm stumped!

I have a bash script. First thing I do is run a command that outputs to a log file. The result looks like this:

LINE1: ProgOut: Starting my thing <parm1> in this <parm2> ... 2014-01-22 

LINE2: 10:21:52.750 NOTIFICATION XYZ-000: My thing "<parm1>", inst "123456", sub 1 has started

<parm1> and <parm2> are input parameters and could therefore change. All other text (except LINE1, LINE2 which are there for readability) are all standard output.

I need to read through and assign the value 123456 to a variable.

I have tried with a regular expression:

INST_ID=$(tail -5 "$LOG_FILE" | grep -oP "^ *My thing *\ *"<parm1>", inst \"\K[^\"]+")

but it obviously doesn't work and I'm struggling to find an alternative. I had also thought fgrep / grep -f would be better, so I could read line by line?

Thanks

认为此grep会有所帮助。

grep -o "inst \"[0-9]*\"" | grep -o [0-9]*

If there are only these 2 lines in the log file so the word "inst" comes only before "123456" then cnicutar's answer should work. What do you get when you run

var=` cat <log_file_path> | grep inst | sed 's/.*inst "\\(.*\\)".*/\\1/' cat <log_file_path> | grep inst | sed 's/.*inst "\\(.*\\)".*/\\1/' cat <log_file_path> | grep inst | sed 's/.*inst "\\(.*\\)".*/\\1/' `

echo $var

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM