シェルスクリプトがあります。シェルスクリプトでは、file.log
asという単語がファイルにあるかどうかを確認しますMB
。その場合はこれをシェルスクリプトの変数に保存しv_name
、単語がなければ空でv_name
なければなりません。
メモ:遺言file.log
には最大1つのMB
単語が含まれています。
答え1
答え2
ファイル内の単語を検索するUnixコマンドは次のとおりです。
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.
答え3
v_name=$(grep -P "\d+\.\d+ MB")
「92.29MB」などの変数を指定してください。
答え4
#!/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