在這裏,我製作了一個小腳本,用於從用戶搜索文件中的某些模式並顯示找到該模式的文件中所需的行數。儘管由於標準的grep練習,此代碼正在尋找模式線。我的意思是如果模式在同一行出現兩次,我希望輸出打印兩次。希望我有所幫助。使用grep計算同一行上的單詞的多次出現
#!/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