0

I want to filter the svlog file by date and split the result by the space, so when i enter the date, it didn't work for me, please refer to the script that I wrote below, the problem was in this command:

ligne=`cat /var/log/svlog | grep  "\$day"`

, it doesn't consider "\$day". i also tried this "^\$day", this "$day" and this "${day}" but the same result.

#!/bin/bash                 

echo SCRIPT-LOG             
echo enter date         
read day                   

ligne=`cat /var/log/svlog | grep  "\$day"`

$ligne >> log1.txt          

awk '{split($ligne,numbers," ")} END {for(n in numbers){ print numbers[n] }}'>lo


"Monit_Sub.sh" 11 lines, 211 characters

$ sudo ./Monit_Sub.sh 
SCRIPT-LOG
enter date
Apr 26
./Monit_Sub.sh: line 8: Apr: command not found

$ 
  • just to correct ligne=` cat /var/log/svlog | grep "\$day" ` with this quotes (`) – LinuxUser Apr 26 '16 at 14:38
  • 1
    This line "$ligne >> log1.txt" is missing an echo so your script is trying to execute a command called Apr. That's the specific error you've got, I've not considered anything else in your script. – EightBitTony Apr 26 '16 at 14:41
  • The problem is not on that line "$ligne >> log1.txt", i have omitted it . but the "ligne" variable is empty there is no result. – LinuxUser Apr 26 '16 at 14:52
  • 3
    Please edit your question and explain what you are actually trying to do. You are showing your attempt but we have no way of knowing what your actual objective is. For example, is $ligne >> log1.txt supposed to execute the command stored in $ligne and append its output into log1.txt (that's what it does) or was it supposed to append the contents of the variable $ligne to the file? Your awk command has no input, what is that supposed to do? Please edit and clarify. – terdon Apr 26 '16 at 14:58
  • 1
    Please provide a sample data file and the desired output using that file. You should edit your question to include this information. – roaima Apr 26 '16 at 16:02
2

Change

ligne=`cat /var/log/svlog | grep  "\$day"`    

to

ligne=$(grep "$day" /var/log/svlog)

To feed the contents of $ligne to awk, use

echo "$ligne" | awk ...
  • 3
    Why would you echo that? Just grep "$day" /var/log/svlog >> log1.txt is enough. – terdon Apr 26 '16 at 14:53
  • @terdon I put in an echo because it's not entirely clear to me what OP intended to do with $ligne; see @EightBitTony 's comment above – Guido Apr 26 '16 at 14:55
  • i dont want echo it. i want to use the result of awk '{split($ligne,numbers," ")} END {for(n in numbers){ print numbers[n] }}'>lo – LinuxUser Apr 26 '16 at 14:56
  • @LinuxUser I see. Updated my answer. – Guido Apr 26 '16 at 14:58
  • I dont want to echo the variable ligne, i have ommitted this line "$ligne >> log1.txt" . I want to split the result of the variable "ligne", but there is no result. – LinuxUser Apr 26 '16 at 15:02

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.