{"id":1562,"date":"2016-09-28T20:04:12","date_gmt":"2016-09-29T01:04:12","guid":{"rendered":"http:\/\/osric.com\/chris\/accidental-developer\/?p=1562"},"modified":"2016-09-28T20:18:00","modified_gmt":"2016-09-29T01:18:00","slug":"ways-to-iterate-over-lines-of-a-file-in-linux","status":"publish","type":"post","link":"https:\/\/osric.com\/chris\/accidental-developer\/2016\/09\/ways-to-iterate-over-lines-of-a-file-in-linux\/","title":{"rendered":"3 ways to iterate over lines of a file in Linux"},"content":{"rendered":"<p>Frequently I need to run a process for each item in a list, stored in a text file one item per line: usernames, filenames, e-mail addresses, etc. Obviously there are more than 3 ways to do this, but here are 3 I have found useful:<\/p>\n<p><strong>Bash<\/strong><br \/>\n<code>sh prog1.sh list.txt<\/code><\/p>\n<p><strong>Source: prog1.sh<\/strong><\/p>\n<pre><code>while read line\r\ndo\r\n    echo $line\r\ndone &lt; $1<\/code><\/pre>\n<p>4 lines. Not bad.<\/p>\n<p><strong>Perl<\/strong><br \/>\n<code>perl prog2.pl list.txt<\/code><\/p>\n<p><strong>Source: prog2.pl<\/strong><\/p>\n<pre><code>while(&lt;&gt;) {\r\n    print `echo $_`;\r\n}<\/code><\/pre>\n<p>3 lines. Pretty good.<\/p>\n<p><strong>Perl -n<\/strong><br \/>\n<code>perl -n prog3.pl list.txt<\/code><\/p>\n<p><strong>Source: prog3.pl<\/strong><\/p>\n<pre><code>print `echo $_`;<\/code><\/pre>\n<p>1 line! The <code>-n<\/code> switch basically wraps your Perl code in a loop that processes each line of the input file. I just discovered this while flipping through my 17-year-old copy of <em><a href=\"http:\/\/shop.oreilly.com\/product\/9780596004927.do\">Programming Perl<\/a><\/em> (link is to a newer edition).<\/p>\n<p>I really like this method because you can write a script that processes a single input that could easily be reused by another script, but can also easily be used to process an entire list by adding just the <code>-n<\/code> switch. (There&#8217;s also a similar <code>-p<\/code> switch that does the same thing, but additionally prints out each line.)<\/p>\n<p>I should note that in the examples above, I am using <code>echo<\/code> as a substitute for any command external to the script itself. In the Perl examples, there would be no need to call <code>echo<\/code> to merely print the contents of the line, but it&#8217;s a convenient stand-in for a generic command.<\/p>\n<p>As suggested by a comment on a previous post, I have made these examples available in a git repository: <a href=\"https:\/\/github.com\/cherdt\/iterate-over-lines\">iterate over lines<\/a>. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Frequently I need to run a process for each item in a list, stored in a text file one item per line: usernames, filenames, e-mail addresses, etc. Obviously there are more than 3 ways to do this, but here are 3 I have found useful: Bash sh prog1.sh list.txt Source: prog1.sh while read line do &hellip; <a href=\"https:\/\/osric.com\/chris\/accidental-developer\/2016\/09\/ways-to-iterate-over-lines-of-a-file-in-linux\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">3 ways to iterate over lines of a file in Linux<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[92,232],"tags":[197,360],"class_list":["post-1562","post","type-post","status-publish","format-standard","hentry","category-perl","category-tips-tricks","tag-bash","tag-perl"],"_links":{"self":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/1562","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/comments?post=1562"}],"version-history":[{"count":12,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/1562\/revisions"}],"predecessor-version":[{"id":1575,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/1562\/revisions\/1575"}],"wp:attachment":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/media?parent=1562"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/categories?post=1562"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/tags?post=1562"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}