在这里,我制作了一个小脚本,该脚本接受用户从文件中搜索某些模式的输入,并在找到该模式的文件中显示所需的行数。尽管由于标准 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