0

how to find -l or --log-file string in a file and store absolute path passed with that as a variable ?

Sample file 1:-

$ cat server.log
server_options='-l /tmp/server_log'

Sample file 2:-

$ cat server.log
server_options='--log-file=/tmp/server1_log'

Requirement

if -l or --log-file is found in server_options variable then find the absolute path of the file and store it in a variable

Note:- 1. Don't consider lines starting with "#" 2. if "-l" option is specified between the -l and absolute path there will be a space and if "--log-file" is specified between the "--log-file" and absolute path there will be a equal to sign "="

for example

server_options='-l /tmp/server_log'
server_options='--log-file=/tmp/server1_log'

Desired output of the sample 1 file stored in a variable "LOG_FILE"

$ echo $LOG_FILE
/tmp/server_log

Desired output of the sample 2 file stored in a variable "LOG_FILE"

$ echo $LOG_FILE
/tmp/server1_log

Tried:-

$ grep "^[^#;]" server.log | awk '/\-\l|/{ print $2 }'
/tmp/server_log'

Need more reliable way to achieve this

asokan
  • 189
  • 1
  • 9

3 Answers3

1

Other than awk, there is also a cut command which can have similar result. For example:

grep "-l|--log-file" server.log | cut -d'\'' -f1 | cut -d' ' -f1

cut -d'\'' -f1 = cut by ' character, choose 2nd string

cut -d' ' -f1 = cut by "space" character, choose 2nd string

AlexM
  • 304
  • 1
  • 3
  • 16
0
grep "^[^#;]" server.log |grep -Po '(-l|--log-file) \K.*(?=.)'
asokan
  • 189
  • 1
  • 9
  • You could just combine it as `grep -Po '^[^#;].*(-l|--log-file) \K.*(?=.)' /etc/ddn/ime/ime-server.conf` – Inian Nov 01 '17 at 12:48
  • if "-l" option is specified between the -l and absolute path there will be a space and if "--log-file" is specified between the "--log-file" and absolute path there will be a equal to sign "=" – asokan Nov 02 '17 at 07:41
  • server_options='-l /tmp/server_log' server_options='--log-file=/tmp/server1_log' – asokan Nov 02 '17 at 07:43
  • and this one liner doesn't work with "--log-file" option – asokan Nov 02 '17 at 07:43
0

this is a bit long shot, but understandable

egrep '\-l|\-\-log\-file' server.log | grep -v ^# | gawk -F"'" '{print $2}' | cut -d" " -f2
Potato
  • 3,320
  • 2
  • 14
  • 31