0

I'd like to retrive a list excluding the first 2 lines

$ pip list
Package                            Version  
---------------------------------- ---------
alabaster                          0.7.10   
anaconda-client                    1.6.9    
anaconda-navigator                 1.7.0    
anaconda-project                   0.8.2    
appnope                            0.1.0    
appscript                          1.0.1    
asn1crypto                         0.24.0   
astroid                            1.6.1    
astropy                            2.0.3    
attrs                              17.4.0   

How to cut off the first two line like:

pip list | cut line=2
  • 1
    awk 'NR>2' file... – jasonwryan May 3 '18 at 7:08
2

Simply with the sed command:

pip list | sed '1,2d'

Explanation: The sed command deletes (d) lines in the range 1 to 2 (and outputs everything else).

Or awk:

pip list | awk 'NR > 2'

Explanation: The awk program outputs everything on line 3 onwards.

awk would be particularly useful if you are planning to do further parsing of that output, such as extracting only the package names:

pip list | awk 'NR > 2 { print $1 }'

The cut command, that you mention in the question (disregarding that the semantics used is wrong), does not cut lines. Well, it does, but it cuts fields out of lines.

  • Actually, with GNU cut, one can do seq 20 | cut -d$'\n' -f3- – Stéphane Chazelas May 3 '18 at 7:47
  • @StéphaneChazelas That's treating the whole file as a \n-delimited set of fields on a single line, so in a sense it's still cutting fields out of a line, but yes. – Kusalananda May 3 '18 at 7:49
  • An advantage of using tail, beside the fact that it's going to be more efficient as it's the simple tool designed to that task is that with most implementations including non-GNU ones, it's going to work even on non-text input (like with very long lines, with NULs or sequences of bytes that don't form valid characters...), while several sed or awk implementations would choke on those. – Stéphane Chazelas May 3 '18 at 7:50
  • @StéphaneChazelas The only reason I did not mention tail was that it is already mentioned in another answer. – Kusalananda May 3 '18 at 8:06
  • 1
    Yes, I don't doubt that. Those comments were just additional notes, not a critique to your answer. – Stéphane Chazelas May 3 '18 at 8:15
6

Simply with tail command:

pip list | tail -n+3 -
  • from tail signature tail [OPTION]... [FILE]... - when FILE is -, read standard input
  • -n, --lines=[+]NUM - output the last NUM lines, instead of the last 10; or use -n +NUM to output starting with line NUM

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.