-3

i have a logfile, a script checks this log-file every hour it shall pack the 3rd word of the last matching line into a variable but i didn't manage it to work yet ..

logfile:

2018-11-18 22:27:10 1542576430 c2- restarted=true
2018-11-19 00:14:34 1542582874 c2- restarted=true

search for last line with c2- and pack this line int variable "lastline"

lastline=$(tac $logfile | grep -m1 c2-)

now put the 3 string of this line into a variable:

last=$(awk '{print $3}' $lastline)

But that does not work ..

especially it does not work wenn i try:

lastline=$(tac $(awk '{print $3}' $lastline) | grep -m1 c2-)

So can anyone help me ??? at the moment i have absolutely no clue .. the output is always:

awk: cannot open 2018-11-19 (No such file or directory)
nyma3
  • 1
  • 2

1 Answers1

0

This doesn't do what you think:

awk '{print $3}' $lastline

The second parameter should be the file name, not the string to process. In bash, you can send a string to the standard input of a comment using the here-string:

awk '{print $3}' <<< "$lastline"

So, without the intermediate variable:

last=$(awk '{print $3}' <<< "$(tac "$logfile" | grep -m1 c2-)")
choroba
  • 374
  • 1
  • 7