BASH - 将包含行号的新条目打印到文件
BASH - Print new entry to file with line number included
我不知道如何向文件添加一行,然后只打印带有行号的那一行。这是我当前打印所有行的代码:
printf "Would you like to add an entry to the file? Type yes or y? "
read addline
input=$addline
if [ $input = 'yes' ] || [ $input = 'y' ]
then
printf "Please enter a new name: First Last: "
read newname
printf "Please enter a new telephone number: xxx-xxx-xxxx: "
read phone
printf "Please enter a street address only: xxx Example Street: "
read street
printf "Please enter your birthdate: xx-xx-xxxx: "
read birth
printf "Please enter salary: xxxxxxx: "
read salary
echo -e "$newname:$phone:$street:$birth:$salary" >> datafile.txt
else
printf "\nYou did not type yes or y. Run the program again.\n"
exit
fi
printf "\nYour entry has been saved.\n"
sort -k2 $file | nl
您可以使用 -n
选项 grep
打印匹配行及其行号。
grep -n -F -x "$newname:$phone:$street:$birth:$salary" datafile.txt
感谢 Barmar 的帮助,我找到了答案。
cat -n $文件 | grep "$newname:$phone:$street:$birth:$salary"
谢谢大家
我不知道如何向文件添加一行,然后只打印带有行号的那一行。这是我当前打印所有行的代码:
printf "Would you like to add an entry to the file? Type yes or y? "
read addline
input=$addline
if [ $input = 'yes' ] || [ $input = 'y' ]
then
printf "Please enter a new name: First Last: "
read newname
printf "Please enter a new telephone number: xxx-xxx-xxxx: "
read phone
printf "Please enter a street address only: xxx Example Street: "
read street
printf "Please enter your birthdate: xx-xx-xxxx: "
read birth
printf "Please enter salary: xxxxxxx: "
read salary
echo -e "$newname:$phone:$street:$birth:$salary" >> datafile.txt
else
printf "\nYou did not type yes or y. Run the program again.\n"
exit
fi
printf "\nYour entry has been saved.\n"
sort -k2 $file | nl
您可以使用 -n
选项 grep
打印匹配行及其行号。
grep -n -F -x "$newname:$phone:$street:$birth:$salary" datafile.txt
感谢 Barmar 的帮助,我找到了答案。
cat -n $文件 | grep "$newname:$phone:$street:$birth:$salary"
谢谢大家