I have an HTML file that is in UTF-8 format and I want to remove the first five lines from it.

I've tried using sed but it doesn't work in this case:

sed  "1,5d" Result.html>small2

It actually works for other files, but not here. I can't use tail because it removes from the end of the file, and the site may be changed later.

this is my file

    HTTP/1.1 200 OK
    Cache-Control: private
    Content-Length: 176073
    Content-Type: text/html; charset=utf-8
    Server: Microsoft-IIS/7.5
    X-AspNet-Version: 4.0.30319
    Set-Cookie: ASP.NET_SessionId=jaq52r5vsd04zvffokbutu1q; path=/; HttpOnly
    X-Powered-By: ASP.NET
    Date: Thu, 29 Nov 2012 06:41:59 GMT
    Connection: close

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 

the file link: 4shared.com/document/U8yRa19I/Result.html here is the od -c Result.html result:

0000000   H   T   T   P   /   1   .   1       2   0   0       O   K  \r
0000020       C   a   c   h   e   -   C   o   n   t   r   o   l   :    
0000040   p   r   i   v   a   t   e  \r       C   o   n   t   e   n   t
0000060   -   L   e   n   g   t   h   :       1   7   6   0   7   3  \r
0000100       C   o   n   t   e   n   t   -   T   y   p   e   :       t
0000120   e   x   t   /   h   t   m   l   ;       c   h   a   r   s   e
0000140   t   =   u   t   f   -   8  \r       S   e   r   v   e   r   :
0000160       M   i   c   r   o   s   o   f   t   -   I   I   S   /   7
0000200   .   5  \r       X   -   A   s   p   N   e   t   -   V   e   r
0000220   s   i   o   n   :       4   .   0   .   3   0   3   1   9  \r
0000240       S   e   t   -   C   o   o   k   i   e   :       A   S   P
0000260   .   N   E   T   _   S   e   s   s   i   o   n   I   d   =   j
0000300   a   q   5   2   r   5   v   s   d   0   4   z   v   f   f   o
0000320   k   b   u   t   u   1   q   ;       p   a   t   h   =   /   ;
0000340       H   t   t   p   O   n   l   y  \r       X   -   P   o   w
0000360   e   r   e   d   -   B   y   :       A   S   P   .   N   E   T
0000400  \r       D   a   t   e   :       T   h   u   ,       2   9    
0000420   N   o   v       2   0   1   2       0   6   :   4   1   :   5
0000440   9       G   M   T  \r       C   o   n   n   e   c   t   i   o
0000460   n   :       c   l   o   s   e  \r      \r
  • I've just need to remove 10 lines that are before <html> tag – Arash Nov 29 '12 at 6:51
  • What do you mean by "doesn't work"? – choroba Nov 29 '12 at 9:04
  • ive test sed in simple file and its great but in uni coded file its not done what i think, its remove lines in 2 byte format but what i want to remove this lines for example this lines:"HTTP/1.1 200 OK"in unicode is "00042 42157.."(for example)and i just want to remove this hedears line 10 line from start of file,tnx – Arash Nov 29 '12 at 9:08
  • 1
    I can't really see what you mean or why it shouldn't work as-is, but you could try a different tool and see if it helps: tail -n +10 Result.html. This tail command skips the first 10 lines, just as you need (it does not "count backwards" from the end in this form). – Daniel Andersson Nov 29 '12 at 9:12
  • tnx ive try it now but nothing change! and i cat it to file but file is empty: tail -n +10 Result.html nothing changed and tail -n +10 Result.html>ams ,ams is empty – Arash Nov 29 '12 at 9:17

I can't access your file so I can't test this, but one of these should work:

gawk 'NR>5' Result.html>small2
perl -ne 'print if $.>5' Result.html>small2

If they don't work, I doubt it is a problem with the encoding, you may have some strange characters screwing things up. try passing your file through od to check:

od -c Result.html | more


I see in the output of od -c that you have mac-style lines that end with a carriage return (\r) and not a line feed (\n). So, try changing these to \n and running sed or one of the other commands again:

perl -ne 's/\r/\n/g; print' Results.html | gawk 'NR>5' > small2

Also, please post your file so we can access it and try it ourselves. It will greatly speed up the process. The service you have linked to requires us to get an account.

| improve this answer | |
  • i cant get answer and both smalls file are empty :( – Arash Nov 29 '12 at 13:45
  • tank you thank you tank uuuu, :-* :) its finally works, – Arash Nov 29 '12 at 14:04
  • i cant vote :( because it need at least 15 reputation,I just have 8 but soon as i get 15 i will thank your favor ;) again thank u, and 1 question what is perl doing ? just changing \r to \n ?? why before that we cant use awk?? tnx – Arash Nov 29 '12 at 14:29
  • 2
    Yes, Perl is just changing \r to \n. You can't use gawk or sed because they use \n to define a line. Since there were no \ns in your file, sed and gawk were treating it all as one line. So, asking it to remove the 1st 5 lines results in an empty file since there is only one line in the original file. – terdon Nov 29 '12 at 15:38

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.