{"id":2215,"date":"2015-06-08T00:12:17","date_gmt":"2015-06-07T16:12:17","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=2215"},"modified":"2015-06-08T00:12:17","modified_gmt":"2015-06-07T16:12:17","slug":"linux%e7%9a%84tac%e5%91%bd%e4%bb%a4%e7%9a%84%e5%a4%9a%e7%a7%8d%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/2215.html","title":{"rendered":"Linux\u7684tac\u547d\u4ee4\u7684\u591a\u79cd\u5b9e\u73b0"},"content":{"rendered":"<h6>\u641c\u7d22\u5173\u952e\u5b57\uff1a<\/h6>\n<ul>\n<li>use awk to read a file in reverse<\/li>\n<\/ul>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/www.tek-tips.com\/viewthread.cfm?qid=965087\">http:\/\/www.tek-tips.com\/viewthread.cfm?qid=965087<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/742466\/how-can-i-reverse-the-order-of-lines-in-a-file\">http:\/\/stackoverflow.com\/questions\/742466\/how-can-i-reverse-the-order-of-lines-in-a-file<\/a><\/li>\n<li><a href=\"http:\/\/unix.stackexchange.com\/questions\/9356\/how-can-i-print-lines-from-file-backwards-without-using-tac\">http:\/\/unix.stackexchange.com\/questions\/9356\/how-can-i-print-lines-from-file-backwards-without-using-tac<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/14258616\/using-tac-to-read-a-file-in-reverse\">http:\/\/stackoverflow.com\/questions\/14258616\/using-tac-to-read-a-file-in-reverse<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/2301789\/read-a-file-in-reverse-order-using-python\">http:\/\/stackoverflow.com\/questions\/2301789\/read-a-file-in-reverse-order-using-python<\/a><\/li>\n<li>=<\/li>\n<li><a href=\"http:\/\/www.bashoneliners.com\/oneliners\/oneliner\/popular\/\">http:\/\/www.bashoneliners.com\/oneliners\/oneliner\/popular\/<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/janosgyerik\/bashoneliners\">https:\/\/github.com\/janosgyerik\/bashoneliners<\/a><\/li>\n<\/ul>\n<h6>\u53c2\u8003\u89e3\u7b54\uff1a<\/h6>\n<pre class=\"lang:default decode:true\">$ awk '{a[++n]=$0}END{for(i=n;i&gt;0;--i)print a[i]}' \/path\/to\/input\n$ awk '{a[n++]=$0}END{while(n--)print a[n]}' \/path\/to\/input\n$ sed '1!G;h;$!d' \/path\/to\/input\n$ sed -n '1!G;h;$p' \/path\/to\/input\n$ cat \/path\/to\/input | perl -e 'print reverse &lt;&gt;'<\/pre>\n<pre class=\"lang:default decode:true\"># cat \/path\/to\/input | python -c \"import sys\n&gt; for line in reversed(sys.stdin.readlines()): print line.rstrip()\"<\/pre>\n<p>\u6ce8\u610f\uff0c\u4e0a\u9762\u8fd9\u4e2aPython\u7248\u672c\u7684\u65e0\u6cd5\u5199\u5728\u4e00\u884c\u4e0a\u9762\uff0c\u5426\u5219\u4f1a\u62a5\u9519\uff1a<\/p>\n<pre class=\"lang:default decode:true \">$ cat \/path\/to\/input | python -c \"import sys;for line in reversed(sys.stdin.readlines()): print line.rstrip()\"\n  File \"&lt;string&gt;\", line 1\n    import sys;for line in reversed(sys.stdin.readlines()): print line.rstrip()\n                 ^\nSyntaxError: invalid syntax<\/pre>\n<p>\u6216<\/p>\n<pre class=\"lang:default decode:true\">$ cat \/path\/to\/input | python -c \"import sys\\nfor line in reversed(sys.stdin.readlines()): print line.rstrip()\"\n  File \"&lt;string&gt;\", line 1\n    import sys\\nfor line in reversed(sys.stdin.readlines()): print line.rstrip()\n                                                                               ^\nSyntaxError: unexpected character after line continuation character<\/pre>\n<h6>\u641c\u7d22\u5173\u952e\u5b57\uff1a<\/h6>\n<ul>\n<li>python one line for loop error<\/li>\n<\/ul>\n<h6>\u89e3\u91ca\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/2043453\/executing-python-multi-line-statements-in-the-one-line-command-line\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/2043453\/executing-python-multi-line-statements-in-the-one-line-command-line<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/18602119\/python-command-line-argument-semicolon-loop-error\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/18602119\/python-command-line-argument-semicolon-loop-error<\/a><\/li>\n<\/ul>\n<p>\u6839\u636ePython\u7684\u8bed\u6cd5\u89c4\u5219\uff0cwhile\u548cfor\u8bed\u53e5\u524d\u9762\u662f\u4e0d\u80fd\u6709\u5206\u53f7\u7684\uff08\u590d\u5408\u8bed\u53e5\u90fd\u4e0d\u80fd\u5168\u5199\u5728\u4e00\u884c\uff09\uff1a<\/p>\n<p><strong><span style=\"color: #ff0000;\"><code>while<\/code>,<\/span> <code>for<\/code> can&#8217;t have semicolon before<\/strong>, they need to be on one line. If you looked at <a href=\"http:\/\/docs.python.org\/2\/reference\/grammar.html\">Python grammar<\/a>:<\/p>\n<pre class=\"lang:default decode:true\">compound_stmt ::=  if_stmt\n                   | while_stmt\n                   | for_stmt\n                   | try_stmt\n                   | with_stmt\n                   | funcdef\n                   | classdef\n                   | decorated\nsuite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT\nstatement     ::=  stmt_list NEWLINE | compound_stmt\nstmt_list     ::=  simple_stmt (\";\" simple_stmt)* [\";\"]<\/pre>\n<p>you will see that the statements that are part of <strong><a href=\"http:\/\/docs.python.org\/2\/reference\/compound_stmts.html\" target=\"_blank\"><code>compound_stmt<\/code><\/a><\/strong> need to be one one line alone. The only statements that can be separated by semicolon are <strong><a href=\"http:\/\/docs.python.org\/2\/reference\/simple_stmts.html#grammar-token-simple_stmt\" target=\"_blank\"><code>simple_stmt<\/code><\/a><\/strong> group:<\/p>\n<pre class=\"lang:default decode:true\">simple_stmt ::=  expression_stmt\n                 | assert_stmt\n                 | assignment_stmt\n                 | augmented_assignment_stmt\n                 | pass_stmt\n                 | del_stmt\n                 | print_stmt\n                 | return_stmt\n                 | yield_stmt\n                 | raise_stmt\n                 | break_stmt\n                 | continue_stmt\n                 | import_stmt\n                 | global_stmt\n                 | exec_stmt<\/pre>\n<p>=EOF=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u641c\u7d22\u5173\u952e\u5b57\uff1a use awk to read a file in reverse \u53c2\u8003\u94fe\u63a5\uff1a http:\/\/w [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,11,6,12],"tags":[74,85,8,78,293],"class_list":["post-2215","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-linux","category-other","category-tools","tag-awk","tag-perl","tag-python","tag-sed","tag-tac"],"views":3416,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2215","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=2215"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2215\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=2215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=2215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=2215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}