0

What is the simplest global shell way to remove 3 (or whatever) lines from the top of some output of varying length? I realized that tail will only work if you know how long the document length is.

Example: Say we want to process this but exclude first 2 lines of it's output

$ curl -s http://google.com
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>

for fun here is python solution

$ curl -s http://google.com  | python -c "import sys; [sys.stdout.write(line) for line in list(sys.stdin)[2:]]" | sort
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>
<H1>301 Moved</H1>
The document has moved

Extending you shell this way

$ function from() {
> python -c "import sys; [sys.stdout.write(line) for line in list(sys.stdin)[${1}:]]"
> }

to do this with any number of lines

$ curl -s http://google.com  | from 3 | sort
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>
Peter Moore
  • 1,632
  • 1
  • 17
  • 31
  • 1
    If you want to self-answer your question, you should post the answer separately, not as part of the question. However, this is well-answered duplicate (https://stackoverflow.com/q/339483/3266847), and you should probably just add your answer there, if you feel like it's missing. – Benjamin W. Dec 13 '19 at 00:23
  • For a question that's not specifically about just the first line, but any number of lines: https://stackoverflow.com/q/604864/3266847 (but it's closed as duplicate of the one I posted above) – Benjamin W. Dec 13 '19 at 00:25
  • @Benjamin W. Ahhhh that's it. I just did the python thing for fun. Im glad the answer was not in awk :-) – Peter Moore Dec 13 '19 at 00:34

1 Answers1

2

I think the simplest and most "standard" way is to use tail:

curl ... | tail -n +4

The tail command means to print all lines starting with the 4th line (i.e. skipping lines 1 through 3).

If you want a pure bash one-liner, you could do this:

lineno=1 ; curl ... | while IFS= read -r line ; do [ $lineno -gt 3 ] && echo "$line" ; lineno=$((lineno+1)); done

There's probably a slicker way to do it than the clumsy lineno counter, but it does the job.

Alcamtar
  • 1,478
  • 13
  • 19