0

I'll share an example so it makes sense -

[$] cat /boot/config-4.9.0-1-amd64 | tail

  7856  CONFIG_FONT_SUPPORT=y
  7857  # CONFIG_FONTS is not set
  7858  CONFIG_FONT_8x8=y
  7859  CONFIG_FONT_8x16=y
  7860  # CONFIG_SG_SPLIT is not set
  7861  CONFIG_SG_POOL=y
  7862  CONFIG_ARCH_HAS_SG_CHAIN=y
  7863  CONFIG_ARCH_HAS_PMEM_API=y
  7864  CONFIG_ARCH_HAS_MMIO_FLUSH=y
  7865  CONFIG_SBITMAP=y

The output is what I want without using cat , cat here is aliased to -

[$] alias cat

cat='cat -n'

Using tail -n is good only if I want to have more number of lines included in tail.

How do I do a tail listing which shows real-line numbers without resorting to cat , is there a way ?

  • tail or even multitail don't have the inbuilt ability to show line numbers. – steve Jan 29 '17 at 19:50
  • @shirish I suppose answers bellow do not cover your question.... Do you had something different in mind? – George Vasiliou Feb 1 '17 at 7:59
  • not really, the thing is it comes down to the same, the character count remains the same or even more , whether I use nl or sed or awk or whatever. I was looking to make the count lower. – shirish Feb 1 '17 at 15:07
6

cat -n for cat haters:

  • With GNU sed:

    sed = file.txt | sed 'N;s/\n/\t/' | tail
    
  • With awk:

     awk '{ $0 = NR "\t" $0 } 1' file.txt | tail
    
  • With grep and GNU sed:

    grep -n ^ file.txt | sed 's/:/\t/' | tail
    
  • With perl:

    perl -lpe '$_ = qq($.\t$_)' | tail
    

    or

    perl -pe 'print "$.\t"' file.txt | tail
    
  • With bash, paste and seq:

    paste <(seq 1 $(wc -l <file.txt)) file.txt | tail
    
  • With pr:

    pr -n -t -l 1 file.txt | tail
    
  • With plain sh:

    let cnt=0
    while read -r line; do
        let cnt\+\+
        printf '%d\t%s\n' $cnt "$line"
    done <file.txt | tail
    
  • With vim:

    :%s/^/\=line('.')."\t"/ | $-10,$y | new | P
    
1

Have you checked the nl utility (part of coreutils present by default in most distros)? Is this what you are looking for?

$ tail /boot/config-4.9.0-1-amd64 |nl 
# Or even nl <(tail /boot/config-4.9.0-1-amd64)

     1  CONFIG_FONT_SUPPORT=y
     2  # CONFIG_FONTS is not set
     3  CONFIG_FONT_8x8=y
     4  CONFIG_FONT_8x16=y
     5  # CONFIG_SG_SPLIT is not set
     6  CONFIG_SG_POOL=y
     7  CONFIG_ARCH_HAS_SG_CHAIN=y
     8  CONFIG_ARCH_HAS_PMEM_API=y
     9  CONFIG_ARCH_HAS_MMIO_FLUSH=y
    10  CONFIG_SBITMAP=y

nl can provide a lot of interesting numbering options, like starting number, number increase step,etc

To simulate the cat numbering, just use nl /boot/config-4.9.0-1-amd64 |tail

  • 1
    @Sato Katsura Do you think that is it a bit unfair for me to add nl solution to your combo post 5 minutes after my answer? – George Vasiliou Jan 29 '17 at 20:58
  • Actually I added it as I thought of it, without reading your answer. But sure, I'll remove it if you prefer your answer to be the the unique repository of that piece of knowledge. Feel free to downvote my answer for that (unintentional) plagiarism. – Satō Katsura Jan 30 '17 at 7:27
  • @SatoKatsura Thank you for answering. No reason to downvote. I will upvote your combo answer and your responsible attitude in this issue. – George Vasiliou Jan 30 '17 at 7:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.