Se file.log
contiver a string MB
, atribua o texto MB
à variável v_name
:
grep -q MB file.log && v_name=MB
Ref: página man para o grep
Estamos tendo um script de shell. No script de shell, queremos descobrir se o arquivo file.log
tem palavra como MB
. Em caso afirmativo, armazene-o na variável v_name
no shell script, se não houver essa palavra, então v_name
deve estar vazio.
Observação: O file.log
conterá no máximo uma palavra MB
.
Se file.log
contiver a string MB
, atribua o texto MB
à variável v_name
:
grep -q MB file.log && v_name=MB
Ref: página man para o grep
Um comando unix para encontrar uma palavra nos arquivos é
grep
$ man grep | grep -A 5 DESCRIPTION
DESCRIPTION
grep searches the named input FILEs (or standard input if no files are named, or if a single hyphen-minus (-) is given as file name) for lines containing a
match to the given PATTERN. By default, grep prints the matching lines.
In addition, three variant programs egrep, fgrep and rgrep are available. egrep is the same as grep -E. fgrep is the same as grep -F. rgrep is the same
as grep -r. Direct invocation as either egrep or fgrep is deprecated, but is provided to allow historical applications that rely on them to run unmodified.
v_name=$(grep -P "\d+\.\d+ MB")
dá a sua variável, e. "92,29 MB"
#!/bin/bash
# $1 = pattern
# $2 = file name to search in
# did not write or test script to handle wild cards in $2
# -i option in grep is case insensitive, use just -l if you care about case
vname='grep -li $1 $2'
if [ -z "$vname" ]; then
echo "vname is empty"
else
echo "vname is " $vname
# set vname to pattern searched for
vname=$1
echo "vname is " $vname
fi
Tags grep