tag:blogger.com,1999:blog-3019054489127059271.post4768979795294516760..comments2024-03-03T10:36:55.383+01:00Comments on YANUB: yet another (nearly) useless blog: Purely shell way to extract a numbered line from a fileVincent Fourmondhttp://www.blogger.com/profile/04388598885608111329noreply@blogger.comBlogger10125tag:blogger.com,1999:blog-3019054489127059271.post-29884222192795657992015-11-21T12:06:08.218+01:002015-11-21T12:06:08.218+01:00Putting a redirection on the far left of a command...Putting a redirection on the far left of a command (<foo bar |...) breaks in some shell contexts (such as inside a while... Etc)Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-3019054489127059271.post-89753444531950127052015-11-17T11:15:54.283+01:002015-11-17T11:15:54.283+01:00Using cat is a preference, there is nothing inhere...Using cat is a preference, there is nothing inherently wrong with either version.. most of the time cat need to be replaced with some other command like gzip/curl/pv or something that doesn't take input from standard in.<br /><br /><br />There are absolutely no good arguments against using cat.Erik Jhttps://www.blogger.com/profile/08336903210387170530noreply@blogger.comtag:blogger.com,1999:blog-3019054489127059271.post-42307229021809550682015-11-17T06:58:40.092+01:002015-11-17T06:58:40.092+01:00Even cat just being there for demonstration purpos...Even cat just being there for demonstration purposes, the more people see it being used this way the more they will use it for their real scripts. You can search for "useless uses of cat" to get some rationales.<br /><br />If you want to read from left to right, then consider these two variations:<br /><br />$ head -n5 file | tail -n1<br /><br />or<br /><br />$ < file head -n5 | tail -n1<br /><br />Both preserve the reading from left to right making use of head taking (one or more) file(s) as arguments and the redirection operator also being allowed in front of the command.joschhttp://mister-muffin.denoreply@blogger.comtag:blogger.com,1999:blog-3019054489127059271.post-24795252730753676742015-11-17T00:06:17.208+01:002015-11-17T00:06:17.208+01:00Now that's an interesting series of command-li...Now that's an interesting series of command-lines, thanks ! @Bruno, the cat is just here as a demonstration, mostly I'm using this to get the nth line of the ouput of some command, and I find it more readable (ie left to right) than the redirection...Vincent Fourmondhttps://www.blogger.com/profile/04388598885608111329noreply@blogger.comtag:blogger.com,1999:blog-3019054489127059271.post-70328925914554200872015-11-16T23:29:15.510+01:002015-11-16T23:29:15.510+01:00If the line to be extract is at the end tail will ...If the line to be extract is at the end tail will read a lot of lines. IMHO using cat is also not a good idea either since it spawns a useless process.<br /><br />Is something like this not more efficient ?<br /><br /> head -5 < file | tail -1<br /><br />Anyhow the sed way is the cleaner to my point of view.Bruno BEAUFILSnoreply@blogger.comtag:blogger.com,1999:blog-3019054489127059271.post-70459995417402068102015-11-16T23:20:49.593+01:002015-11-16T23:20:49.593+01:00I prefer this version
$ cat file | head -n5 | ta...I prefer this version<br /><br />$ cat file | head -n5 | tail -n1Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-3019054489127059271.post-71537616166189479482015-11-16T23:12:13.897+01:002015-11-16T23:12:13.897+01:00sed -ne '5{p;q}'
That will have sed exit ...sed -ne '5{p;q}'<br /><br />That will have sed exit after printing the 5th line.<br /><br />awk 'NR==5 {print; exit}'<br /><br />does the same with awkcamhhttps://www.blogger.com/profile/00860158630335660776noreply@blogger.comtag:blogger.com,1999:blog-3019054489127059271.post-81018299191661483252015-11-16T23:10:52.147+01:002015-11-16T23:10:52.147+01:00Ah, that's a fair point, sed reads the whole i...Ah, that's a fair point, sed reads the whole input file unconditionally.olasdnoreply@blogger.comtag:blogger.com,1999:blog-3019054489127059271.post-5866741309862205322015-11-16T23:04:15.059+01:002015-11-16T23:04:15.059+01:00Mmmm. I knew there was an easy sed way too. Just a...Mmmm. I knew there was an easy sed way too. Just a question, though: does sed close the input after line 5 ? If you are generating a huge amount of data, but only need the beginning, the tail | head trick would kill the original process with SIGPIPE relatively early.Vincent Fourmondhttps://www.blogger.com/profile/04388598885608111329noreply@blogger.comtag:blogger.com,1999:blog-3019054489127059271.post-55621196027294328802015-11-16T22:56:36.325+01:002015-11-16T22:56:36.325+01:00"sed -ne 5p" works pretty well too"sed -ne 5p" works pretty well tooolasdnoreply@blogger.com