{"id":2243,"date":"2017-12-08T05:36:00","date_gmt":"2017-12-08T05:36:00","guid":{"rendered":"http:\/\/www.scmgalaxy.com\/tutorials\/?p=2243"},"modified":"2020-01-09T10:01:58","modified_gmt":"2020-01-09T10:01:58","slug":"shell-script-to-read-file-line-by-line","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/shell-script-to-read-file-line-by-line\/","title":{"rendered":"Shell script to read file line by line"},"content":{"rendered":"<p><strong>rajeshkumar created the topic: Shell script to read file line by line<\/strong><\/p>\n<p><code>processLine(){<br \/>\n  line=\"$@\" # get all args<br \/>\n  #  just echo them, but you may need to customize it according to your need<br \/>\n  # for example, F1 will store first field of $line, see readline2 script<br \/>\n  # for more examples<br \/>\n  # F1=$(echo $line | awk '{ print $1 }')<br \/>\n  echo $line<br \/>\n}<\/p>\n<p>### Main script stars here ###<br \/>\n# Store file name<br \/>\nFILE=\"\"<\/p>\n<p># Make sure we get file name as command line argument<br \/>\n# Else read it from standard input device<br \/>\nif [ \"$1\" == \"\" ]; then<br \/>\n   FILE=\"\/dev\/stdin\"<br \/>\nelse<br \/>\n   FILE=\"$1\"<br \/>\n   # make sure file exist and readable<br \/>\n   if [ ! -f $FILE ]; then<br \/>\n        echo \"$FILE : does not exists\"<br \/>\n        exit 1<br \/>\n   elif [ ! -r $FILE ]; then<br \/>\n        echo \"$FILE: can not read\"<br \/>\n        exit 2<br \/>\n   fi<br \/>\nfi<br \/>\n# read $FILE using the file descriptors<\/p>\n<p># Set loop separator to end of line<br \/>\nBAKIFS=$IFS<br \/>\nIFS=$(echo -en \"\\n\\b\")<br \/>\nexec 3<&#038;0\nexec 0<\"$FILE\"\nwhile read -r line\ndo\n        # use $line variable to process line in processLine() function\n        processLine $line\ndone\nexec 0<&#038;3\n \n# restore $IFS which was used to determine what the field separators are\nIFS=$BAKIFS\nexit 0<\/code><\/p>\n<p>Regards,<br \/>\nRajesh Kumar<br \/>\nTwitt me @ <a href=\"http:\/\/twitter.com\/RajeshKumarIn\" target=\"_blank\" rel=\"noopener\">twitter.com\/RajeshKumarIn<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>rajeshkumar created the topic: Shell script to read file line by line processLine(){ line=&#8221;$@&#8221; # get all args # just echo them, but you may need to customize it according to your need # for example, F1 will store first field of $line, see readline2 script # for more examples # F1=$(echo $line | awk&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[454],"tags":[138],"class_list":["post-2243","post","type-post","status-publish","format-standard","hentry","category-shell-script","tag-shell"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/2243","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=2243"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/2243\/revisions"}],"predecessor-version":[{"id":2244,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/2243\/revisions\/2244"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=2243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=2243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=2243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}