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 ...
Related videos on Youtube
Author by
LinuxUser
Updated on September 18, 2022Comments
-
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 about 8 yearsjust to correct ligne=` cat /var/log/svlog | grep "\$day" ` with this quotes (`)
-
EightBitTony about 8 yearsThis 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 about 8 yearsThe problem is not on that line "$ligne >> log1.txt", i have omitted it . but the "ligne" variable is empty there is no result.
-
terdon about 8 yearsPlease 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 intolog1.txt
(that's what it does) or was it supposed to append the contents of the variable$ligne
to the file? Yourawk
command has no input, what is that supposed to do? Please edit and clarify. -
roaima about 8 yearsPlease provide a sample data file and the desired output using that file. You should edit your question to include this information.
-
Wildcard about 8 yearsStart using
$(this syntax)
for command substitution instead of backticks; the quoting is MUCH easier. -
Ciro Santilli Путлер Капут 六四事 almost 7 years
-
-
terdon about 8 yearsWhy would you
echo
that? Justgrep "$day" /var/log/svlog >> log1.txt
is enough. -
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 about 8 yearsi 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 about 8 years@LinuxUser I see. Updated my answer.
-
LinuxUser about 8 yearsI 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 about 8 yearsTo process the contents of
$ligne
with awk, you will need toecho
it into a pipe to awk, see answer. -
LinuxUser about 8 yearsIt 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] }}'