Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to exclude the first 2 lines and only number list the rest of the file?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Output of what I would like

Here is what I get:

Output of what I get

  1. 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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading