{"id":1574,"date":"2014-11-12T15:28:23","date_gmt":"2014-11-12T15:28:23","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=1574"},"modified":"2014-11-12T15:28:23","modified_gmt":"2014-11-12T15:28:23","slug":"%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96%e6%96%87%e4%bb%b6%e4%b8%ad%e7%9a%84%e6%9f%90%e4%b8%80%e8%a1%8c%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/1574.html","title":{"rendered":"\u5982\u4f55\u83b7\u53d6\u6587\u4ef6\u4e2d\u7684\u67d0\u4e00\u884c\uff1f"},"content":{"rendered":"<p>\u5728Stackoverflow\u4e0a\u770b\u5230\u4e00\u4e2a\u9898\u76ee&#8221;<a href=\"http:\/\/stackoverflow.com\/questions\/1429556\/shell-bash-command-to-get-nth-line-of-stdout\" target=\"_blank\">Shell\/Bash Command to get nth line of STDOUT<\/a>&#8220;\uff0c\u89c9\u5f97\u4e0d\u9519\uff0c\u5c1d\u8bd5\u4e86\u5176\u4e2d\u4ecb\u7ecd\u7684\u65b9\u6cd5\uff0c\u7136\u540e\u7ed3\u5408\u81ea\u5df1\u4e4b\u524d\u7684\u4e00\u4e9b\u5b66\u4e60\uff0c\u603b\u7ed3\/\u6458\u5f55\u81f3\u4e0b\u6587\uff1a<\/p>\n<h6>\u7528Linux\u7684\u547d\u4ee4\u83b7\u53d6\u6587\u4ef6\u4e2d\u7684\u67d0\u4e00\u884c{Shell\/Bash Command to get nth line of STDOUT}<\/h6>\n<p>Is there any bash command that will let you get the nth line of STDOUT?<br \/>\nThat is to say, something that would take this<\/p>\n<pre class=\"lang:default decode:true\">$ ls -l\n-rw-r--r--@ 1 root wheel my.txt\n-rw-r--r--@ 1 root wheel files.txt\n-rw-r--r--@ 1 root wheel here.txt<\/pre>\n<p>and do something like<\/p>\n<pre class=\"lang:default decode:true\">$ ls -l | magic-command 2\n-rw-r--r--@ 1 root wheel files.txt<\/pre>\n<p>I realize this would be bad practice when writing scripts meant to be reused, BUT when working with the shell day to day it&#8217;d be useful to me to be able to filter my STDOUT in such a way.<br \/>\nI also realize this would be semi-trivial command to write (buffer STDOUT, return a specific line), but I want to know if there&#8217;s some <strong>standard<\/strong> shell command to do this that would be available without me dropping a script into place.<\/p>\n<hr \/>\n<p>Using sed, just for variety:<\/p>\n<pre class=\"lang:default decode:true\">ls -l | sed -n 2p<\/pre>\n<p>Using this alternative, which looks more efficient since it stops reading the input when the required line is printed, may generate a SIGPIPE in the feeding process, which may in turn generate an unwanted error message:<\/p>\n<pre class=\"lang:default decode:true\">ls -l | sed -n -e '2{p;q}'<\/pre>\n<p>I&#8217;ve seen that often enough that I usually use the first (which is easier to type, anyway), though ls is not a command that complains when it gets SIGPIPE.<br \/>\nFor a range of lines:<\/p>\n<pre class=\"lang:default decode:true\">ls -l | sed -n 2,4p<\/pre>\n<p>For several ranges of lines:<\/p>\n<pre class=\"lang:default decode:true\">ls -l | sed -n -e 2,4p -e 20,30p\nls -l | sed -n -e '2,4p;20,30p'\nls -l | head -2 | tail -1<\/pre>\n<hr \/>\n<p>Alternative to the nice head \/ tail way:<\/p>\n<pre class=\"lang:default decode:true\">ls -al | awk 'NR==2'<\/pre>\n<p>or<\/p>\n<pre class=\"lang:default decode:true\">ls -al | sed -n '2p'<\/pre>\n<hr \/>\n<p>From <a href=\"http:\/\/sed.sf.net\/sed1line.txt\" target=\"_blank\">sed1line<\/a>:<\/p>\n<pre class=\"lang:default decode:true\"># print line number 52\nsed -n '52p' # method 1\nsed '52!d' # method 2\nsed '52q;d' # method 3, efficient on large files<\/pre>\n<p>From <a href=\"http:\/\/www.pement.org\/awk\/awk1line.txt\" target=\"_blank\">awk1line<\/a>:<\/p>\n<pre class=\"lang:default decode:true\"># print line number 52\nawk 'NR==52'\nawk 'NR==52 {print;exit}' # more efficient on large files<\/pre>\n<hr \/>\n<p>Try this sed version:<\/p>\n<pre class=\"lang:default decode:true\">ls -l | sed '2 ! d'<\/pre>\n<p>It says &#8220;delete all the lines that aren&#8217;t the second one&#8221;.<\/p>\n<hr \/>\n<p>For the sake of completeness \ud83d\ude09<br \/>\nshorter code<\/p>\n<pre class=\"lang:default decode:true\">find \/ | awk NR==3<\/pre>\n<p>shorter life<\/p>\n<pre class=\"lang:default decode:true\">find \/ | awk 'NR==3 {print $0; exit}'<\/pre>\n<hr \/>\n<p>Is Perl easily available to you?<\/p>\n<pre class=\"lang:default decode:true\">$ perl -n -e 'if ($. == 7) { print; exit(0); }'<\/pre>\n<hr \/>\n<p>Obviously substitute whatever number you want for 7.<br \/>\nYou can use awk:<\/p>\n<pre class=\"lang:default decode:true\">ls -l | awk 'NR==2'<\/pre>\n<p>Update<br \/>\nThe above code will not get what we want because of off-by-one error: the ls -l command&#8217;s first line is the total line. For that, the following revised code will work:<\/p>\n<pre class=\"lang:default decode:true\">ls -l | awk 'NR==3'<\/pre>\n<hr \/>\n<p>Another poster suggested<\/p>\n<pre class=\"lang:default decode:true\">ls -l | head -2 | tail -1<\/pre>\n<p>but if you pipe head into tail, it looks like everything up to line N is processed twice.<br \/>\nPiping tail into head<\/p>\n<pre class=\"lang:default decode:true\">ls -l | tail -n +2 | head -n1<\/pre>\n<p>would be more efficient?<\/p>\n<h6>\u539f\u6587\u94fe\u63a5\uff1a<\/h6>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/1429556\/shell-bash-command-to-get-nth-line-of-stdout\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/1429556\/shell-bash-command-to-get-nth-line-of-stdout<\/a><\/p>\n<hr \/>\n<p>&nbsp;<\/p>\n<h5>\u5feb\u901f\u6253\u5370\u51fa\u6587\u4ef6\u4e2d\u95f4\u4f4d\u7f6e\u7684\u884c<\/h5>\n<h6>1.sed\u548cawk<\/h6>\n<p>\u5047\u8bbe\u4f60\u9700\u898120\u81f340\u884c\u7684\u5185\u5bb9\uff1a<\/p>\n<pre class=\"lang:default decode:true\">sed -n '20,40p' file_name\n#\nawk 'FNR&gt;=20 &amp;&amp; FNR&lt;=40' file_name<\/pre>\n<h6>2.sed\u7684\u51e0\u79cd\u65b9\u5f0f\uff08\u6253\u5370\/\u663e\u793a\u6307\u5b9a\u884c\uff09<\/h6>\n<pre class=\"lang:default decode:true\"># print line number 52\nsed -n '52p' # method 1\nsed '52!d' # method 2\nsed '52q;d' # method 3, efficient on large files<\/pre>\n<p><span style=\"color: #0000ff;\"><strong>method 3 efficient on large files<\/strong><\/span><\/p>\n<p>fastest way to display specific lines<\/p>\n<h6>3.\u5148head\u7136\u540etail<\/h6>\n<p>\u5728Python\u4e2d\u53ef\u4ee5\u73b0\u5c06\u6587\u4ef6readlines()\u5230\u4e00\u4e2alist\u4e2d\uff0c\u7136\u540e\u4f7f\u7528list\u5207\u5272\u65b9\u6cd5\u8fdb\u884c\u67e5\u770b\u2014\u2014\u5bf9\u4e8e\u5c0f\u6587\u4ef6\u800c\u8a00\u3002<\/p>\n<h6>4.\u5148\u5207\u5272\uff0c\u7136\u540e\u5206\u522b\u67e5\u770b<\/h6>\n<p>\u5148split\uff0c\u7136\u540egrep\u3001sed\u3001awk\u3001head\u3001tail<\/p>\n<h6><span style=\"color: #ff0000;\"><strong>\u53c2\u8003<\/strong><strong>\uff1a<\/strong><\/span><\/h6>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/191364\/quick-unix-command-to-display-specific-lines-in-the-middle-of-a-file\" target=\"_blank\">Quick unix command to display specific lines in the middle of a file?<\/a><\/li>\n<li><a href=\"http:\/\/www.unix.com\/unix-dummies-questions-answers\/21027-how-do-you-specific-lines-file.html\" target=\"_blank\">http:\/\/www.unix.com\/unix-dummies-questions-answers\/21027-how-do-you-specific-lines-file.html<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Stackoverflow\u4e0a\u770b\u5230\u4e00\u4e2a\u9898\u76ee&#8221;Shell\/Bash Command to get n [&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],"tags":[74,85,78],"class_list":["post-1574","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-linux","tag-awk","tag-perl","tag-sed"],"views":2363,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1574","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=1574"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1574\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=1574"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=1574"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=1574"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}