1

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

$ 
faimer
  • 21
  • just to correct ligne=cat /var/log/svlog | grep "\$day" with this quotes (`) – faimer 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. – faimer 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. – Chris Davies Apr 26 '16 at 16:02
  • Start using $(this syntax) for command substitution instead of backticks; the quoting is MUCH easier. – Wildcard Apr 26 '16 at 23:43
  • https://unix.stackexchange.com/questions/163810/grep-on-a-variable – Ciro Santilli OurBigBook.com Jun 01 '17 at 16:14

1 Answers1

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 ...
Guido
  • 4,114
  • 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 – faimer 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. – faimer Apr 26 '16 at 15:02
  • To process the contents of $ligne with awk, you will need to echo it into a pipe to awk, see answer. – Guido Apr 26 '16 at 15:11
  • It works for me now, is the missing echo on the last line echo $ligne | awk '{split($0,numbers," ")} END {for(n in numbers){ print numbers[n] }}' – faimer Apr 27 '16 at 14:27