awk - How to print the number of characters for the first n lines in a file?

8,632

Solution 1

Tell awk to quit when enough lines have been read:

awk '$0 = length; NR==3 { exit }' /etc/passwd

Note that this solution ignores empty lines, although not for the line count.

Solution 2

A direct Awk version (not so efficient as @Thor's), but slightly more clear:

awk 'NR <= 3 {print length}' /etc/passwd

Solution 3

You can execute it with awk only command, as nicely described by @Thor, and @JJoao (+1 from me)

You can combine awk and head with parameter -n follows by the number of lines as described below:

Thanks for @Maerlyn suggestion to execute in this order: head | awk

e.g. You will get the first 3 lines using:

head -n3 /etc/passwd | awk '{ print length($0); }' 

head man

-n, --lines=[-]K
    print the first K lines instead of the first 10; with the leading '-', print all but the last K lines of each file 
Share:
8,632

Related videos on Youtube

bambosze_babuni
Author by

bambosze_babuni

Updated on September 18, 2022

Comments

  • bambosze_babuni
    bambosze_babuni over 1 year

    I have a command:

    $ awk '{ print length($0); }' /etc/passwd
    

    It prints number of characters of every line in a passwd file:

    52
    52
    61
    48
    81
    58
    etc.
    

    How can I print the number of characters for only the first n lines?

    For example - for the first 3 lines it would give something like:

    52
    52
    61
    
    • njzk2
      njzk2 about 7 years
      @ilkkachu true.
    • VIPIN KUMAR
      VIPIN KUMAR about 7 years
      If you want to ignore blank like then use below - awk '(NR<=line) && length($0) {print length($0)}' line=3 file
  • Yaron
    Yaron about 7 years
    @msjavx86 - great :) please accept the answer, when you'll be able to do it
  • bambosze_babuni
    bambosze_babuni about 7 years
    of course I will - in about 10 minutes ;) thanks again!
  • Admin
    Admin about 7 years
    tricky and cool! (+1)
  • Maerlyn
    Maerlyn about 7 years
    Maybe even reverse, head -n3 /etc/passwd | awk so awk doesn't have to process the whole file.
  • ilkkachu
    ilkkachu about 7 years
    If any or the lines is empty, the assignment evaluates to zero (a falsy value), and the length will not be printed.
  • Dennis Williamson
    Dennis Williamson about 7 years
    If this was awk '{ print length } NR>=3 { exit }' /etc/passwd, I'd upvote it.
  • Dennis Williamson
    Dennis Williamson about 7 years
    AWK can count lines, no need for head.
  • Admin
    Admin about 7 years
    @DennisWilliamson, thank you. That is a very good suggestion, I will not include it in my answer because it is already implicit in Thor's proposal.
  • Thor
    Thor about 7 years
    @ilkkachu: Depending on the situation, it would be reasonable to ignore empty lines. I have added a note about this.
  • Yaron
    Yaron about 7 years
    @DennisWilliamson - thanks for the comment, using head was the first working solution which came to my mind. I've upvoted @Thor, @JJoao answer which uses awk only solution.