ここでは、ファイルからパターンを検索するユーザーから入力を受け取り、パターンが見つかったファイルから必要な行数を表示する小さなスクリプトを作成しました。このコードは、標準的な grep の慣習により、パターン行ごとに検索していますが。パターンが同じ行で 2 回発生した場合、出力を 2 回印刷する必要があります。私が何らかの意味を成すことを願っています。
#!/bin/sh
cat /dev/null>copy.txt
echo "Please enter the sentence you want to search:"
read "inputVar"
echo "Please enter the name of the file in which you want to search:"
read "inputFileName"
echo "Please enter the number of lines you want to copy:"
read "inputLineNumber"
[[-z "$inputLineNumber"]] || inputLineNumber=20
cat /dev/null > copy.txt
for N in `grep -n $inputVar $inputFileName | cut -d ":" -f1`
do
LIMIT=`expr $N + $inputLineNumber`
sed -n $N,${LIMIT}p $inputFileName >> copy.txt
echo "-----------------------" >> copy.txt
done
cat copy.txt