I can't figure out how to add a line to a file then print that line only with the line number. Here is my current code which prints all lines:
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
CodePudding user response:
You can use the -n
option to grep
to print a matching line along with its line number.
grep -n -F -x "$newname:$phone:$street:$birth:$salary" datafile.txt
CodePudding user response:
Thanks to help from Barmar, I figured out my answer.
cat -n $file | grep "$newname:$phone:$street:$birth:$salary"
Thanks everyone.