Display line number in head and tail command like `cat -n`

86

What about head and tail command? Is there any option to view the line number in head and tail without using cat -n and pipe the output to head or tail?

No; the current versions of head and tail do not have any way to add line numbers to the output themselves.

Share:
86
asker
Author by

asker

Updated on September 18, 2022

Comments

  • asker
    asker almost 2 years

    I'm trying to get the key of these json objects in order to create a new object with extra filed to create table headers in a React app. JSON data:

    let example = [
      {
        id: 1,
        city: 'New York',
      },
     {
        id: 2,
        city: 'Paris',
      },
    ]
    

    The function:

    getKeys() {
        return example.map((key) => {
          return {
            cityName: key, // gets the whole array
            capital: false,
          };
        });
      }
    

    I tries Object.keys( example);, it returns integers; 0, 1. How can I get the keys in this case? Thanks.

    • Kusalananda
      Kusalananda about 5 years
      -n is the option for selecting the number of lines that you'd want head or tail to return.
  • Kusalananda
    Kusalananda about 5 years
    There's nothing stopping the user from using head file | cat -n though.
  • Jeff Schaller
    Jeff Schaller about 5 years
    Yes, I was afraid I was being too brief; I was responding directly to the "can head or tail number lines directly?"