I’m trying to number list a certain section of the file but I’m not managing.
I want the IP section to be numbered only
Here is what I want to have:
Here is what I get:
- echo "Number Server"
echo "——– ——–"
sed -n ‘1,2s/^/ /’ server.list.txt
tail -n +3 server.list.txt | nl
echo -n "Enter the server number to delete: " select
read select
if [[ $select -eq 3 ]]
then
sed -i ‘3d’ server.list.txt #deletes the 3rd line
elif [[ $select -eq 4 ]]
then
sed -i ‘4d’ server.list.txt #deletes the 4th line
elif [[ $select -eq 5 ]]
then
sed -i ‘5d’ server.list.txt #deletes the 5th line
else
echo ""
echo "ERROR: The number entered is invalid !!!"
echo ""
fi;;
The output shown below happens when the user enters the number ‘3’ on the menu
Edit: With some help, I was able to list the last 2 lines with numbers. Now I am trying to produce this output:
Number Server IP
1 127.0.0.1
2 192.168.40.30
>Solution :
Use sed to print the first two lines with spaces at the beginning.
Then use tail to pipe the rest of the file to nl.
sed -n '1,2s/^/ /' server.list.txt
tail -n +3 server.list.txt | nl