Category: awk

  • Awk: Remove everything after the first word in each line

     

    Another awk question. How to remove everything after the first word in each line? E.g., if we wanted to remove everything but the names in this input (FILENAME.txt):

     

    Anna 123 09123 Main Street

    Bob 109 09800 Smith Street

    Joe 0981 123123 King Street

     

    We can use awk like so:

     

    awk ‘{ print $1 }’

     

    e.g.:

     

    cat FILENAME.txt | awk ‘{ print $1 }’

     

    which will print:

     

    Anna

    Bob

    Joe

     

    Short and sweet.

  • Awk: Remove first line of file

     

    Short and sweet – when piping data through awk, how do we remove the first line of the input?

     

    awk ‘{if (NR!=1) {print}}’

     

    e.g.

     

    cat FILENAME.txt | awk ‘{if (NR!=1) {print}}’

     

    Done!