Running a command (e.g. begin new line) when reading a specific string within a word

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite
1












When a script is reading text file I would like this code to run specific command when it finds a specific string within a specific variable.



Example:



Let's say that I would like to use the following code to read the output of the command last:



#!/bin/bash
for i in `last`; do
sleep 0.1 | echo -ne "$i "
done


The output of the command last is a table in the form of a list of entries, something like:



username pts/2 1.2.3.4 via Sun Sep 2 06:40 - 06:40 (00:00)
. . . .
. . . .


  1. The variable i in the previous code can be any phrase in the previous table. I would like to apply a specific command e. g. begin new line when the code finds a specific string within the variable i, for example when the variable i contains a closed parenthesis ) I want the code to begin a new line.


  2. When the code finishes reading the output of the command last, I want the code to repeat the for loop once again (multiple times) to read if there are any new updates. How can I direct the code to re-run again? For example, is there such a command goto which will force the code to go to specific line?


Would you please advice?










share|improve this question























  • I want to print new line after specific pattern like (00:00) or (12:05) or ...etc
    – TNT
    2 days ago






  • 3




    1 is relatively straightforward, but even if you repeated the for loop again, then it would print out the previous lines again. You'd have to save the previous output in a buffer somehow, and not show them again. Or use -s and save the current time? Also, the output of last is reverse chronological, so the additional runs would be all out of order.
    – Sparhawk
    2 days ago







  • 2




    The list from the output of the command last will be updated every time someone ssh a computer on network. i want to repeat for loop again and again and again and again and again and again and again and again and again and again and again and again and again and again and again and again and ……. etc. How can I do it? ;-)
    – TNT
    2 days ago











  • Useless use of backticks.
    – David Foerster
    2 days ago














up vote
6
down vote

favorite
1












When a script is reading text file I would like this code to run specific command when it finds a specific string within a specific variable.



Example:



Let's say that I would like to use the following code to read the output of the command last:



#!/bin/bash
for i in `last`; do
sleep 0.1 | echo -ne "$i "
done


The output of the command last is a table in the form of a list of entries, something like:



username pts/2 1.2.3.4 via Sun Sep 2 06:40 - 06:40 (00:00)
. . . .
. . . .


  1. The variable i in the previous code can be any phrase in the previous table. I would like to apply a specific command e. g. begin new line when the code finds a specific string within the variable i, for example when the variable i contains a closed parenthesis ) I want the code to begin a new line.


  2. When the code finishes reading the output of the command last, I want the code to repeat the for loop once again (multiple times) to read if there are any new updates. How can I direct the code to re-run again? For example, is there such a command goto which will force the code to go to specific line?


Would you please advice?










share|improve this question























  • I want to print new line after specific pattern like (00:00) or (12:05) or ...etc
    – TNT
    2 days ago






  • 3




    1 is relatively straightforward, but even if you repeated the for loop again, then it would print out the previous lines again. You'd have to save the previous output in a buffer somehow, and not show them again. Or use -s and save the current time? Also, the output of last is reverse chronological, so the additional runs would be all out of order.
    – Sparhawk
    2 days ago







  • 2




    The list from the output of the command last will be updated every time someone ssh a computer on network. i want to repeat for loop again and again and again and again and again and again and again and again and again and again and again and again and again and again and again and again and ……. etc. How can I do it? ;-)
    – TNT
    2 days ago











  • Useless use of backticks.
    – David Foerster
    2 days ago












up vote
6
down vote

favorite
1









up vote
6
down vote

favorite
1






1





When a script is reading text file I would like this code to run specific command when it finds a specific string within a specific variable.



Example:



Let's say that I would like to use the following code to read the output of the command last:



#!/bin/bash
for i in `last`; do
sleep 0.1 | echo -ne "$i "
done


The output of the command last is a table in the form of a list of entries, something like:



username pts/2 1.2.3.4 via Sun Sep 2 06:40 - 06:40 (00:00)
. . . .
. . . .


  1. The variable i in the previous code can be any phrase in the previous table. I would like to apply a specific command e. g. begin new line when the code finds a specific string within the variable i, for example when the variable i contains a closed parenthesis ) I want the code to begin a new line.


  2. When the code finishes reading the output of the command last, I want the code to repeat the for loop once again (multiple times) to read if there are any new updates. How can I direct the code to re-run again? For example, is there such a command goto which will force the code to go to specific line?


Would you please advice?










share|improve this question















When a script is reading text file I would like this code to run specific command when it finds a specific string within a specific variable.



Example:



Let's say that I would like to use the following code to read the output of the command last:



#!/bin/bash
for i in `last`; do
sleep 0.1 | echo -ne "$i "
done


The output of the command last is a table in the form of a list of entries, something like:



username pts/2 1.2.3.4 via Sun Sep 2 06:40 - 06:40 (00:00)
. . . .
. . . .


  1. The variable i in the previous code can be any phrase in the previous table. I would like to apply a specific command e. g. begin new line when the code finds a specific string within the variable i, for example when the variable i contains a closed parenthesis ) I want the code to begin a new line.


  2. When the code finishes reading the output of the command last, I want the code to repeat the for loop once again (multiple times) to read if there are any new updates. How can I direct the code to re-run again? For example, is there such a command goto which will force the code to go to specific line?


Would you please advice?







bash text-processing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago









David Foerster

917616




917616










asked 2 days ago









TNT

308111




308111











  • I want to print new line after specific pattern like (00:00) or (12:05) or ...etc
    – TNT
    2 days ago






  • 3




    1 is relatively straightforward, but even if you repeated the for loop again, then it would print out the previous lines again. You'd have to save the previous output in a buffer somehow, and not show them again. Or use -s and save the current time? Also, the output of last is reverse chronological, so the additional runs would be all out of order.
    – Sparhawk
    2 days ago







  • 2




    The list from the output of the command last will be updated every time someone ssh a computer on network. i want to repeat for loop again and again and again and again and again and again and again and again and again and again and again and again and again and again and again and again and ……. etc. How can I do it? ;-)
    – TNT
    2 days ago











  • Useless use of backticks.
    – David Foerster
    2 days ago
















  • I want to print new line after specific pattern like (00:00) or (12:05) or ...etc
    – TNT
    2 days ago






  • 3




    1 is relatively straightforward, but even if you repeated the for loop again, then it would print out the previous lines again. You'd have to save the previous output in a buffer somehow, and not show them again. Or use -s and save the current time? Also, the output of last is reverse chronological, so the additional runs would be all out of order.
    – Sparhawk
    2 days ago







  • 2




    The list from the output of the command last will be updated every time someone ssh a computer on network. i want to repeat for loop again and again and again and again and again and again and again and again and again and again and again and again and again and again and again and again and ……. etc. How can I do it? ;-)
    – TNT
    2 days ago











  • Useless use of backticks.
    – David Foerster
    2 days ago















I want to print new line after specific pattern like (00:00) or (12:05) or ...etc
– TNT
2 days ago




I want to print new line after specific pattern like (00:00) or (12:05) or ...etc
– TNT
2 days ago




3




3




1 is relatively straightforward, but even if you repeated the for loop again, then it would print out the previous lines again. You'd have to save the previous output in a buffer somehow, and not show them again. Or use -s and save the current time? Also, the output of last is reverse chronological, so the additional runs would be all out of order.
– Sparhawk
2 days ago





1 is relatively straightforward, but even if you repeated the for loop again, then it would print out the previous lines again. You'd have to save the previous output in a buffer somehow, and not show them again. Or use -s and save the current time? Also, the output of last is reverse chronological, so the additional runs would be all out of order.
– Sparhawk
2 days ago





2




2




The list from the output of the command last will be updated every time someone ssh a computer on network. i want to repeat for loop again and again and again and again and again and again and again and again and again and again and again and again and again and again and again and again and ……. etc. How can I do it? ;-)
– TNT
2 days ago





The list from the output of the command last will be updated every time someone ssh a computer on network. i want to repeat for loop again and again and again and again and again and again and again and again and again and again and again and again and again and again and again and again and ……. etc. How can I do it? ;-)
– TNT
2 days ago













Useless use of backticks.
– David Foerster
2 days ago




Useless use of backticks.
– David Foerster
2 days ago










3 Answers
3






active

oldest

votes

















up vote
10
down vote



accepted










If the output from your last is like this:



username pts/2 1.2.3.4 via Sun Sep 2 06:40 - 06:40 (00:00)


and you want to add the newlines where they originally were, then I would suggest changing the logic a bit. Read the input line-by-line to begin with, and split the line into words only after that. That way, there's no need to explicitly look for the parenthesis. And you can repeat the whole loop by wrapping it inside while true; do ... done. So we have something like this:



#!/bin/bash
set -f
while true; do
last | while IFS= read -r line; do
for word in $line; do
sleep .1
printf "%s " "$word"
done
echo # prints a newline
done
done


set -f disables filename expansion, which would otherwise possibly happen at the unquoted expansion of $line. Also, I'd use printf instead of echo to print the words, for a number of reasons.



If you do explicitly want to look for the closing parenthesis, then you can use the [[ .. ]] test: it allows for pattern matching with glob-like patterns, or with regexes. ([[ $word =~ $re ]] would be the regex match)



#!/bin/bash
set -f
while true; do
for word in $(last); do
sleep .1
printf "%s " "$word"
[[ $word = *')'* ]] && echo
done
done


Though this one, of course, doesn't add a newline on lines where the final login duration is replaced by something like still logged in.



The for word in $whatever construct has the downside that it treats multiple spaces exactly like single spaces, so the output from the script will not have the columns aligned as neatly as in the original.






share|improve this answer






















  • Let us continue this discussion in chat.
    – David Foerster
    2 days ago

















up vote
5
down vote













You can use if .. then statement something like:



#!/bin/bash
for i in `last`; do
sleep 0.1 | echo -ne "$i "
if [[ $i = *')'* ]] ; then
echo "" ;
fi
done


If you would like to repeat for loop in a bash to read the output of a command (in your example, the command last), then you have to schedule the script in crontab. More clarifications are here.






share|improve this answer





























    up vote
    0
    down vote













    You can have different approaches and we need you to describe the NEED you have (identify a specific person's login? identify who was loggued as a specific time? etc)



    With just what you said (adding a newline when something shows up in last), you already have different approaches:



    #finding if a 'last' line contains a "specific_thing" (which is itself a regular expression...) 
    # and can print all lines or not depending on showall value
    while true ; do
    last | awk -v looking_for="specific_thing" -v showall=0 '
    ( showall == 1 ) print $0
    ( $0 ~ looking_for ) action="print line? and add something to it?"
    '
    done

    #watch for new things appearing in last and not the whole "last" output every time
    watch last | awk -v looking_for="specific_thing" -v showall=0 '
    ( showall == 1 ) print $0
    ( $0 ~ looking_for ) action="print the line? with added infos?"
    action="execute : system(some command) ?"
    action="all of that?"
    '


    etc.



    You could also look for apparitions in log files



    You could also insert some command in /etc/profile (beware as it can have lots of security implications, and also may create problems at boot, at init launch, etc. It would be an ugly hack, could wreak several (or all) user's login, and could anyway be bypassed by fast pressing of ctrl-C by the one logging in...)



    Etc, etc. Many ways to go, and the best one will be the one fixing what you need to do, and now necessarily sticking to "last" and "adding a newline when I see something in it"



    For better answers, please give us really what you NEED, and WHY you need it, and not HOW you think you need to do this or that action (add a newline when you see an output of last? Why do you think you need to do that? what is the underlying need?)






    share|improve this answer






















      Your Answer







      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "106"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f467934%2frunning-a-command-e-g-begin-new-line-when-reading-a-specific-string-within-a%23new-answer', 'question_page');

      );

      Post as a guest






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      10
      down vote



      accepted










      If the output from your last is like this:



      username pts/2 1.2.3.4 via Sun Sep 2 06:40 - 06:40 (00:00)


      and you want to add the newlines where they originally were, then I would suggest changing the logic a bit. Read the input line-by-line to begin with, and split the line into words only after that. That way, there's no need to explicitly look for the parenthesis. And you can repeat the whole loop by wrapping it inside while true; do ... done. So we have something like this:



      #!/bin/bash
      set -f
      while true; do
      last | while IFS= read -r line; do
      for word in $line; do
      sleep .1
      printf "%s " "$word"
      done
      echo # prints a newline
      done
      done


      set -f disables filename expansion, which would otherwise possibly happen at the unquoted expansion of $line. Also, I'd use printf instead of echo to print the words, for a number of reasons.



      If you do explicitly want to look for the closing parenthesis, then you can use the [[ .. ]] test: it allows for pattern matching with glob-like patterns, or with regexes. ([[ $word =~ $re ]] would be the regex match)



      #!/bin/bash
      set -f
      while true; do
      for word in $(last); do
      sleep .1
      printf "%s " "$word"
      [[ $word = *')'* ]] && echo
      done
      done


      Though this one, of course, doesn't add a newline on lines where the final login duration is replaced by something like still logged in.



      The for word in $whatever construct has the downside that it treats multiple spaces exactly like single spaces, so the output from the script will not have the columns aligned as neatly as in the original.






      share|improve this answer






















      • Let us continue this discussion in chat.
        – David Foerster
        2 days ago














      up vote
      10
      down vote



      accepted










      If the output from your last is like this:



      username pts/2 1.2.3.4 via Sun Sep 2 06:40 - 06:40 (00:00)


      and you want to add the newlines where they originally were, then I would suggest changing the logic a bit. Read the input line-by-line to begin with, and split the line into words only after that. That way, there's no need to explicitly look for the parenthesis. And you can repeat the whole loop by wrapping it inside while true; do ... done. So we have something like this:



      #!/bin/bash
      set -f
      while true; do
      last | while IFS= read -r line; do
      for word in $line; do
      sleep .1
      printf "%s " "$word"
      done
      echo # prints a newline
      done
      done


      set -f disables filename expansion, which would otherwise possibly happen at the unquoted expansion of $line. Also, I'd use printf instead of echo to print the words, for a number of reasons.



      If you do explicitly want to look for the closing parenthesis, then you can use the [[ .. ]] test: it allows for pattern matching with glob-like patterns, or with regexes. ([[ $word =~ $re ]] would be the regex match)



      #!/bin/bash
      set -f
      while true; do
      for word in $(last); do
      sleep .1
      printf "%s " "$word"
      [[ $word = *')'* ]] && echo
      done
      done


      Though this one, of course, doesn't add a newline on lines where the final login duration is replaced by something like still logged in.



      The for word in $whatever construct has the downside that it treats multiple spaces exactly like single spaces, so the output from the script will not have the columns aligned as neatly as in the original.






      share|improve this answer






















      • Let us continue this discussion in chat.
        – David Foerster
        2 days ago












      up vote
      10
      down vote



      accepted







      up vote
      10
      down vote



      accepted






      If the output from your last is like this:



      username pts/2 1.2.3.4 via Sun Sep 2 06:40 - 06:40 (00:00)


      and you want to add the newlines where they originally were, then I would suggest changing the logic a bit. Read the input line-by-line to begin with, and split the line into words only after that. That way, there's no need to explicitly look for the parenthesis. And you can repeat the whole loop by wrapping it inside while true; do ... done. So we have something like this:



      #!/bin/bash
      set -f
      while true; do
      last | while IFS= read -r line; do
      for word in $line; do
      sleep .1
      printf "%s " "$word"
      done
      echo # prints a newline
      done
      done


      set -f disables filename expansion, which would otherwise possibly happen at the unquoted expansion of $line. Also, I'd use printf instead of echo to print the words, for a number of reasons.



      If you do explicitly want to look for the closing parenthesis, then you can use the [[ .. ]] test: it allows for pattern matching with glob-like patterns, or with regexes. ([[ $word =~ $re ]] would be the regex match)



      #!/bin/bash
      set -f
      while true; do
      for word in $(last); do
      sleep .1
      printf "%s " "$word"
      [[ $word = *')'* ]] && echo
      done
      done


      Though this one, of course, doesn't add a newline on lines where the final login duration is replaced by something like still logged in.



      The for word in $whatever construct has the downside that it treats multiple spaces exactly like single spaces, so the output from the script will not have the columns aligned as neatly as in the original.






      share|improve this answer














      If the output from your last is like this:



      username pts/2 1.2.3.4 via Sun Sep 2 06:40 - 06:40 (00:00)


      and you want to add the newlines where they originally were, then I would suggest changing the logic a bit. Read the input line-by-line to begin with, and split the line into words only after that. That way, there's no need to explicitly look for the parenthesis. And you can repeat the whole loop by wrapping it inside while true; do ... done. So we have something like this:



      #!/bin/bash
      set -f
      while true; do
      last | while IFS= read -r line; do
      for word in $line; do
      sleep .1
      printf "%s " "$word"
      done
      echo # prints a newline
      done
      done


      set -f disables filename expansion, which would otherwise possibly happen at the unquoted expansion of $line. Also, I'd use printf instead of echo to print the words, for a number of reasons.



      If you do explicitly want to look for the closing parenthesis, then you can use the [[ .. ]] test: it allows for pattern matching with glob-like patterns, or with regexes. ([[ $word =~ $re ]] would be the regex match)



      #!/bin/bash
      set -f
      while true; do
      for word in $(last); do
      sleep .1
      printf "%s " "$word"
      [[ $word = *')'* ]] && echo
      done
      done


      Though this one, of course, doesn't add a newline on lines where the final login duration is replaced by something like still logged in.



      The for word in $whatever construct has the downside that it treats multiple spaces exactly like single spaces, so the output from the script will not have the columns aligned as neatly as in the original.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited 2 days ago

























      answered 2 days ago









      ilkkachu

      50.8k678140




      50.8k678140











      • Let us continue this discussion in chat.
        – David Foerster
        2 days ago
















      • Let us continue this discussion in chat.
        – David Foerster
        2 days ago















      Let us continue this discussion in chat.
      – David Foerster
      2 days ago




      Let us continue this discussion in chat.
      – David Foerster
      2 days ago












      up vote
      5
      down vote













      You can use if .. then statement something like:



      #!/bin/bash
      for i in `last`; do
      sleep 0.1 | echo -ne "$i "
      if [[ $i = *')'* ]] ; then
      echo "" ;
      fi
      done


      If you would like to repeat for loop in a bash to read the output of a command (in your example, the command last), then you have to schedule the script in crontab. More clarifications are here.






      share|improve this answer


























        up vote
        5
        down vote













        You can use if .. then statement something like:



        #!/bin/bash
        for i in `last`; do
        sleep 0.1 | echo -ne "$i "
        if [[ $i = *')'* ]] ; then
        echo "" ;
        fi
        done


        If you would like to repeat for loop in a bash to read the output of a command (in your example, the command last), then you have to schedule the script in crontab. More clarifications are here.






        share|improve this answer
























          up vote
          5
          down vote










          up vote
          5
          down vote









          You can use if .. then statement something like:



          #!/bin/bash
          for i in `last`; do
          sleep 0.1 | echo -ne "$i "
          if [[ $i = *')'* ]] ; then
          echo "" ;
          fi
          done


          If you would like to repeat for loop in a bash to read the output of a command (in your example, the command last), then you have to schedule the script in crontab. More clarifications are here.






          share|improve this answer














          You can use if .. then statement something like:



          #!/bin/bash
          for i in `last`; do
          sleep 0.1 | echo -ne "$i "
          if [[ $i = *')'* ]] ; then
          echo "" ;
          fi
          done


          If you would like to repeat for loop in a bash to read the output of a command (in your example, the command last), then you have to schedule the script in crontab. More clarifications are here.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 2 days ago

























          answered 2 days ago









          Goro

          1,49641643




          1,49641643




















              up vote
              0
              down vote













              You can have different approaches and we need you to describe the NEED you have (identify a specific person's login? identify who was loggued as a specific time? etc)



              With just what you said (adding a newline when something shows up in last), you already have different approaches:



              #finding if a 'last' line contains a "specific_thing" (which is itself a regular expression...) 
              # and can print all lines or not depending on showall value
              while true ; do
              last | awk -v looking_for="specific_thing" -v showall=0 '
              ( showall == 1 ) print $0
              ( $0 ~ looking_for ) action="print line? and add something to it?"
              '
              done

              #watch for new things appearing in last and not the whole "last" output every time
              watch last | awk -v looking_for="specific_thing" -v showall=0 '
              ( showall == 1 ) print $0
              ( $0 ~ looking_for ) action="print the line? with added infos?"
              action="execute : system(some command) ?"
              action="all of that?"
              '


              etc.



              You could also look for apparitions in log files



              You could also insert some command in /etc/profile (beware as it can have lots of security implications, and also may create problems at boot, at init launch, etc. It would be an ugly hack, could wreak several (or all) user's login, and could anyway be bypassed by fast pressing of ctrl-C by the one logging in...)



              Etc, etc. Many ways to go, and the best one will be the one fixing what you need to do, and now necessarily sticking to "last" and "adding a newline when I see something in it"



              For better answers, please give us really what you NEED, and WHY you need it, and not HOW you think you need to do this or that action (add a newline when you see an output of last? Why do you think you need to do that? what is the underlying need?)






              share|improve this answer


























                up vote
                0
                down vote













                You can have different approaches and we need you to describe the NEED you have (identify a specific person's login? identify who was loggued as a specific time? etc)



                With just what you said (adding a newline when something shows up in last), you already have different approaches:



                #finding if a 'last' line contains a "specific_thing" (which is itself a regular expression...) 
                # and can print all lines or not depending on showall value
                while true ; do
                last | awk -v looking_for="specific_thing" -v showall=0 '
                ( showall == 1 ) print $0
                ( $0 ~ looking_for ) action="print line? and add something to it?"
                '
                done

                #watch for new things appearing in last and not the whole "last" output every time
                watch last | awk -v looking_for="specific_thing" -v showall=0 '
                ( showall == 1 ) print $0
                ( $0 ~ looking_for ) action="print the line? with added infos?"
                action="execute : system(some command) ?"
                action="all of that?"
                '


                etc.



                You could also look for apparitions in log files



                You could also insert some command in /etc/profile (beware as it can have lots of security implications, and also may create problems at boot, at init launch, etc. It would be an ugly hack, could wreak several (or all) user's login, and could anyway be bypassed by fast pressing of ctrl-C by the one logging in...)



                Etc, etc. Many ways to go, and the best one will be the one fixing what you need to do, and now necessarily sticking to "last" and "adding a newline when I see something in it"



                For better answers, please give us really what you NEED, and WHY you need it, and not HOW you think you need to do this or that action (add a newline when you see an output of last? Why do you think you need to do that? what is the underlying need?)






                share|improve this answer
























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  You can have different approaches and we need you to describe the NEED you have (identify a specific person's login? identify who was loggued as a specific time? etc)



                  With just what you said (adding a newline when something shows up in last), you already have different approaches:



                  #finding if a 'last' line contains a "specific_thing" (which is itself a regular expression...) 
                  # and can print all lines or not depending on showall value
                  while true ; do
                  last | awk -v looking_for="specific_thing" -v showall=0 '
                  ( showall == 1 ) print $0
                  ( $0 ~ looking_for ) action="print line? and add something to it?"
                  '
                  done

                  #watch for new things appearing in last and not the whole "last" output every time
                  watch last | awk -v looking_for="specific_thing" -v showall=0 '
                  ( showall == 1 ) print $0
                  ( $0 ~ looking_for ) action="print the line? with added infos?"
                  action="execute : system(some command) ?"
                  action="all of that?"
                  '


                  etc.



                  You could also look for apparitions in log files



                  You could also insert some command in /etc/profile (beware as it can have lots of security implications, and also may create problems at boot, at init launch, etc. It would be an ugly hack, could wreak several (or all) user's login, and could anyway be bypassed by fast pressing of ctrl-C by the one logging in...)



                  Etc, etc. Many ways to go, and the best one will be the one fixing what you need to do, and now necessarily sticking to "last" and "adding a newline when I see something in it"



                  For better answers, please give us really what you NEED, and WHY you need it, and not HOW you think you need to do this or that action (add a newline when you see an output of last? Why do you think you need to do that? what is the underlying need?)






                  share|improve this answer














                  You can have different approaches and we need you to describe the NEED you have (identify a specific person's login? identify who was loggued as a specific time? etc)



                  With just what you said (adding a newline when something shows up in last), you already have different approaches:



                  #finding if a 'last' line contains a "specific_thing" (which is itself a regular expression...) 
                  # and can print all lines or not depending on showall value
                  while true ; do
                  last | awk -v looking_for="specific_thing" -v showall=0 '
                  ( showall == 1 ) print $0
                  ( $0 ~ looking_for ) action="print line? and add something to it?"
                  '
                  done

                  #watch for new things appearing in last and not the whole "last" output every time
                  watch last | awk -v looking_for="specific_thing" -v showall=0 '
                  ( showall == 1 ) print $0
                  ( $0 ~ looking_for ) action="print the line? with added infos?"
                  action="execute : system(some command) ?"
                  action="all of that?"
                  '


                  etc.



                  You could also look for apparitions in log files



                  You could also insert some command in /etc/profile (beware as it can have lots of security implications, and also may create problems at boot, at init launch, etc. It would be an ugly hack, could wreak several (or all) user's login, and could anyway be bypassed by fast pressing of ctrl-C by the one logging in...)



                  Etc, etc. Many ways to go, and the best one will be the one fixing what you need to do, and now necessarily sticking to "last" and "adding a newline when I see something in it"



                  For better answers, please give us really what you NEED, and WHY you need it, and not HOW you think you need to do this or that action (add a newline when you see an output of last? Why do you think you need to do that? what is the underlying need?)







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited yesterday









                  Rui F Ribeiro

                  36.3k1271116




                  36.3k1271116










                  answered 2 days ago









                  Olivier Dulac

                  3,7751224




                  3,7751224



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f467934%2frunning-a-command-e-g-begin-new-line-when-reading-a-specific-string-within-a%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Comments

                      Popular posts from this blog

                      What does second last employer means? [closed]

                      List of Gilmore Girls characters

                      Confectionery