0

I am using tail -F to monitor a severe weather alert server log file and awk to filter and execute a command if the conditions for an alert are met. I need to execute the bash command(an ssh command sent to a Pi to trigger an alert LED) in system only once, not one time for every match. Everything is working except the system command is running once for every line matched. Here is what I have so far:

tail -F /home/user/test.txt | stdbuf -oL awk '/ZZZ029/{getline; if (/SV.W/) print system("command")}'

Current output(number of lines could vary):

/O.NEW.KBMX.SV.W.0028.190314T2350Z-190315T0030Z/
/O.CON.KFFC.SV.W.0021.000000T0000Z-190315T0015Z/
/O.NEW.KBMX.SV.W.0028.190314T2350Z-190315T0030Z/
/O.CON.KFFC.SV.W.0021.000000T0000Z-190315T0015Z/
/O.NEW.KBMX.SV.W.0028.190314T2350Z-190315T0030Z/
/O.CON.KFFC.SV.W.0021.000000T0000Z-190315T0015Z/

Expected output:

/O.NEW.KBMX.SV.W.0028.190314T2350Z-190315T0030Z/

I need just one line of output so the system command will run only once. Or just a way to run the command once regardless of number of lines of output.

  • 2
    after you calll system("command") you want the whole pipeline to stop, or you want the pipeline to continue but for awk to never call system again? – Jeff Schaller Mar 23 at 14:08
  • Note that system() runs sh to interpret the command not bash. If you want to start bash to interpret bash specific code, you'll need to construct sh code that runs bash -c bash-specific-code-here where that bash-specific code will have to be quoted as per sh rules. – Stéphane Chazelas Mar 23 at 17:28
  • @JeffSchaller I want the tail -F to continue watching the file but to run the system() command only once for each time matches are found, regardless of how many matches there are. – Jake Mar 24 at 13:56
  • I'm having trouble putting together these two phrases: "run the system() command once" -- and -- "each time matches are found, regardless of how many matches there are". Do you want the command run for the first match, and that first match only? – Jeff Schaller Mar 24 at 14:02
  • Yes, for the first match only. – Jake Mar 24 at 14:27
0
From current output what you provided from my understanding you want to remove duplicates

pipeline_previous_command|awk '{if(!seen[$1]++)print $1}'

/O.NEW.KBMX.SV.W.0028.190314T2350Z-190315T0030Z/
/O.CON.KFFC.SV.W.0021.000000T0000Z-190315T0015Z/

Suppose if you want ouput as output which mentioned above

raveen:~$ pipeline_previous_command |  awk '/W.0028/{if(!seen[$1]++)print $1}'

/O.NEW.KBMX.SV.W.0028.190314T2350Z-190315T0030Z/
  • Yes, remove duplicates of SV.W match. I'm not understanding how to work this into my current script, however. Does it go after the getline; if (/SV.W/) command? – Jake Mar 24 at 16:40
  • tail -F /home/user/test.txt | stdbuf -oL awk '/ZZZ029/{getline; if (/SV.W/) print system("command")}'|awk '{if(!seen[$1]++)print $1}' use this command – Praveen Kumar BS Mar 25 at 6:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.