regex - Add to the end of a line missing a pattern -


i have long, not maintained bash script on centos, many log lines using echo, , of third of them tee-ing log file. modify rest of echo lines tee log file.

here example myscript.sh:

command1 echo "hi1" echo "hi2" | tee -a my.log echo "hi3 tee" command2 

after running on file, contents changed to:

command1 echo "hi1" | tee -a my.log echo "hi2" | tee -a my.log echo "hi3 tee" | tee -a my.log command2 

i thinking need use sed or awk regular expression, logic is, "if line contains 'echo', followed not '| tee', append ' | tee -a my.log' @ end of line".

after lot of searching, best i've come far:

sed --in-place=_backup '/^.*echo\(?!\| tee$\)*/ s/$/ \| tee -a my.log/' myscript.sh 

but appends | tee -a my.log end of each line containing echo.

does have ideas?

this should trick (although feel bunch of corner cases coming):

$ awk '/^echo/&&!/tee -a my.log$/{$0=$0"| tee -a my.log"}1' file command1 echo "hi1"| tee -a my.log echo "hi2" | tee -a my.log echo "hi3 tee"| tee -a my.log command2 

explanation:

/^echo/                  # if line start echo &&                       # logical , !/tee -a my.log$/        # doesn't end tee -a my.log {$0=$0"| tee -a my.log"} # append tee command end of line 1                        # awk idiom print lines in file 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -