grep with variable in a variable

12,577

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 ...
Share:
12,577

Related videos on Youtube

LinuxUser
Author by

LinuxUser

Updated on September 18, 2022

Comments

  • LinuxUser
    LinuxUser over 1 year

    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
    
    $ 
    
    • LinuxUser
      LinuxUser about 8 years
      just to correct ligne=` cat /var/log/svlog | grep "\$day" ` with this quotes (`)
    • EightBitTony
      EightBitTony about 8 years
      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.
    • LinuxUser
      LinuxUser about 8 years
      The problem is not on that line "$ligne >> log1.txt", i have omitted it . but the "ligne" variable is empty there is no result.
    • terdon
      terdon about 8 years
      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.
    • roaima
      roaima about 8 years
      Please provide a sample data file and the desired output using that file. You should edit your question to include this information.
    • Wildcard
      Wildcard about 8 years
      Start using $(this syntax) for command substitution instead of backticks; the quoting is MUCH easier.
    • Ciro Santilli Путлер Капут 六四事
      Ciro Santilli Путлер Капут 六四事 almost 7 years
  • terdon
    terdon about 8 years
    Why would you echo that? Just grep "$day" /var/log/svlog >> log1.txt is enough.
  • Guido
    Guido about 8 years
    @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
  • LinuxUser
    LinuxUser about 8 years
    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
  • Guido
    Guido about 8 years
    @LinuxUser I see. Updated my answer.
  • LinuxUser
    LinuxUser about 8 years
    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.
  • Guido
    Guido about 8 years
    To process the contents of $ligne with awk, you will need to echo it into a pipe to awk, see answer.
  • LinuxUser
    LinuxUser about 8 years
    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] }}'