• VMware

    Learn about VMware virtualization for its products like vsphere ESX and ESXi, vCenter Server, VMware View, VMware P2V and many more

  • Linux

    Step by step configuration tutorials for many of the Linux services like DNS, DHCP, FTP, Samba4 etc including many tips and tricks in Red Hat Linux.

  • Database

    Learn installation and configuration of databases like Oracle, My SQL, Postgresql, etc including many other related tutorials in Linux.

  • How to add line number at the beginning of each line

    There are many ways to do this, I will try to show some

    Sample file '/tmp/file'
    Apple
    Apricot
    Avocado
    Banana
    Bilberry
    Blackberry
    Blackcurrant


    Method 1

    Using 'nl'
    # nl /tmp/file
         1  Apple
         2  Apricot
         3  Avocado
         4  Banana
         5  Bilberry
         6  Blackberry
         7  Blackcurrant

    Add a symbol after the numbers





    # nl -s ". " /tmp/file
         1. Apple
         2. Apricot
         3. Avocado
         4. Banana
         5. Bilberry
         6. Blackberry
         7. Blackcurrant

    Save the output to a file
    # nl -s ". " /tmp/file  > /tmp/newfile

    Method 2

    Using 'perl'
    #  perl -pe 'printf "%u. ", $.' /tmp/file
    1. Apple
    2. Apricot
    3. Avocado
    4. Banana
    5. Bilberry
    6. Blackberry
    7. Blackcurrant


    Method 3

    Using 'awk'
    # awk '{printf "%d. %s\n", NR, $0}' /tmp/file
    1. Apple
    2. Apricot
    3. Avocado
    4. Banana
    5. Bilberry
    6. Blackberry
    7. Blackcurrant


    # awk '{print NR".",$0}' /tmp/file
    1. Apple
    2. Apricot
    3. Avocado
    4. Banana
    5. Bilberry
    6. Blackberry
    7. Blackcurrant


    Method 4

    Using 'sed'
    # sed '=' /tmp/file | sed '{N;s/\n/. /}'
    1. Apple
    2. Apricot
    3. Avocado
    4. Banana
    5. Bilberry
    6. Blackberry
    7. Blackcurrant


    Follow the below links for more tutorials

    How to find the path of any command in Linux
    How to configure a Clustered Samba share using ctdb in Red Hat Cluster
    How to delete an iscsi-target from openfiler and Linux
    How to perform a local ssh port forwarding in Linux
    Hot swapping a broken HDD with SOFTWARE (RAID 1) on HP blades
    Collect Virtual Connect Support Dump of HP c-Class Blade Enclosures
    How to collect/generate "Show All" report for HP c-Class Blade Enclosures
    How to save %pre installation log file after installation in Red Hat 7
    How to perform interactive kickstart installation on Red Hat
    How to forcefully stop and kill a process in Linux
    How to create customized bootable ISO image in RHEL/CentOS 7
    How to disable or enable ipv6 in Linux
    How to blacklist or disable USB module in Red Hat 7

    How to add line number at the beginning of each line How to add line number at the beginning of each line Reviewed by Deepak Prasad on Saturday, June 24, 2017 Rating: 5

    No comments:

    Powered by Blogger.