<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Read variables from file in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/read-variables-from-file/m-p/4025247#M93689</link>
    <description>&lt;!--!*#--&gt;Hi (again) Amardeep:&lt;BR /&gt;&lt;BR /&gt;Peter's excellent ideas suggest another approach.&lt;BR /&gt;&lt;BR /&gt;The code below accomodates:&lt;BR /&gt;&lt;BR /&gt;1.  The output file is automatically truncated (cleared) at the script's onset.&lt;BR /&gt;&lt;BR /&gt;2.  Only one open() call occurs.&lt;BR /&gt;&lt;BR /&gt;3.  *ALL* output in the loop is written to the output file regardless of the number of 'echo' or 'print' calls.&lt;BR /&gt;&lt;BR /&gt;4.  One argument can be optionally passed to the script to define the output file name.  If no argument (filename) is provided, output is written to the current directory in a file named the same as the basename of the script with ".out" as a suffix.&lt;BR /&gt;&lt;BR /&gt;# cat reader&lt;BR /&gt;#!/usr/bin/sh&lt;BR /&gt;typeset    MYOUTPUT=${1:-$0.out}&lt;BR /&gt;typeset -i MYCOUNT=0&lt;BR /&gt;exec  1&amp;gt; ${MYOUTPUT}&lt;BR /&gt;while read F1 F2_N&lt;BR /&gt;do&lt;BR /&gt;    (( MYCOUNT=MYCOUNT+1 ))&lt;BR /&gt;    echo "${MYCOUNT} -&amp;gt; ${F2_N}"&lt;BR /&gt;done &amp;lt; varlist.txt&lt;BR /&gt;&lt;BR /&gt;...when run as:&lt;BR /&gt;&lt;BR /&gt;# ./reader&lt;BR /&gt;&lt;BR /&gt;...output will be found in ./reader.out&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
    <pubDate>Mon, 25 Jun 2007 18:32:52 GMT</pubDate>
    <dc:creator>James R. Ferguson</dc:creator>
    <dc:date>2007-06-25T18:32:52Z</dc:date>
    <item>
      <title>Read variables from file</title>
      <link>https://community.hpe.com/t5/operating-system-linux/read-variables-from-file/m-p/4025238#M93680</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;I have a file with thousand rows (not columns) in it. I want to read each line (one by one) and then use it as command syntax in another script. How can I do that? &lt;BR /&gt;&lt;BR /&gt;Currently I have this but it doesn't work... &lt;BR /&gt;&lt;BR /&gt;while [ $MYCOUNT -lt $CAIDCOUNT ]&lt;BR /&gt;do&lt;BR /&gt;CAID=`awk -F '/n' '{print $1}' varlist.txt`&lt;BR /&gt;echo "$MYCOUNT $CAID" &amp;gt; out.txt&lt;BR /&gt;let MYCOUNT=$MYCOUNT+1&lt;BR /&gt;done&lt;BR /&gt;&lt;BR /&gt;Thanks in advance&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Fri, 22 Jun 2007 16:08:28 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/read-variables-from-file/m-p/4025238#M93680</guid>
      <dc:creator>Amar_Joshi</dc:creator>
      <dc:date>2007-06-22T16:08:28Z</dc:date>
    </item>
    <item>
      <title>Re: Read variables from file</title>
      <link>https://community.hpe.com/t5/operating-system-linux/read-variables-from-file/m-p/4025239#M93681</link>
      <description>Awk is reading the entire file line by line.&lt;BR /&gt;To do it this way you would have to keep incrementing NR until done.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;If there are no spaces then a while read X loop would work.&lt;BR /&gt;&lt;BR /&gt;Can you supply an example of your input file.&lt;BR /&gt;</description>
      <pubDate>Fri, 22 Jun 2007 16:13:31 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/read-variables-from-file/m-p/4025239#M93681</guid>
      <dc:creator>Tim Nelson</dc:creator>
      <dc:date>2007-06-22T16:13:31Z</dc:date>
    </item>
    <item>
      <title>Re: Read variables from file</title>
      <link>https://community.hpe.com/t5/operating-system-linux/read-variables-from-file/m-p/4025240#M93682</link>
      <description>Not quite clear what you mean from the code snippet posted here. Could you provide a sample of the input file and also give a clearer and more indepth explanation of what you are trying to accomplish.&lt;BR /&gt;&lt;BR /&gt;~thanks</description>
      <pubDate>Fri, 22 Jun 2007 16:18:43 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/read-variables-from-file/m-p/4025240#M93682</guid>
      <dc:creator>Sandman!</dc:creator>
      <dc:date>2007-06-22T16:18:43Z</dc:date>
    </item>
    <item>
      <title>Re: Read variables from file</title>
      <link>https://community.hpe.com/t5/operating-system-linux/read-variables-from-file/m-p/4025241#M93683</link>
      <description>&lt;!-- !*# --&gt;&lt;P&gt;What are you really trying to do?&lt;BR /&gt;If you intended to have your loop above read CAIDCOUNT lines and number them, you should look into "nl -ba".&lt;BR /&gt;&lt;BR /&gt;You could also do it all in awk:&lt;BR /&gt;awk '{ print $NR, $0 }' varlist.txt &amp;gt; out.txt&lt;BR /&gt;&lt;BR /&gt;As Tim mentioned, you are reading EVERY line in your file, each time in your loop.&lt;BR /&gt;&lt;BR /&gt;As Tim says you can use a read loop. Assuming you want to read all lines:&lt;BR /&gt;(( MYCOUNT = 1 ))&lt;BR /&gt;while read LINE; do&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; echo "$MYCOUNT $LINE"&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; (( MYCOUNT += 1 ))&lt;BR /&gt;done &amp;lt; varlist.txt &amp;gt; out.txt&lt;/P&gt;</description>
      <pubDate>Mon, 09 Mar 2015 03:18:54 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/read-variables-from-file/m-p/4025241#M93683</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2015-03-09T03:18:54Z</dc:date>
    </item>
    <item>
      <title>Re: Read variables from file</title>
      <link>https://community.hpe.com/t5/operating-system-linux/read-variables-from-file/m-p/4025242#M93684</link>
      <description>Thanks for your replies but it doesn't solve my problem.&lt;BR /&gt;&lt;BR /&gt;DON'T look at the echo command, I just used it to illustrate the function, my actual command is 200 characters long and has lot's of other variables. Since the command is from hdvmcli.sh (storage utility) I hide it.&lt;BR /&gt;&lt;BR /&gt;varlist.txt contains lots of lines looks like WWNs and my aim is to use them as one of the variables in line but when I use above mentioned example, $MYCOUNT increases but $CAID remains same (second line from the varlist.txt&lt;BR /&gt;&lt;BR /&gt;Hope that is clear now....</description>
      <pubDate>Fri, 22 Jun 2007 23:05:16 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/read-variables-from-file/m-p/4025242#M93684</guid>
      <dc:creator>Amar_Joshi</dc:creator>
      <dc:date>2007-06-22T23:05:16Z</dc:date>
    </item>
    <item>
      <title>Re: Read variables from file</title>
      <link>https://community.hpe.com/t5/operating-system-linux/read-variables-from-file/m-p/4025243#M93685</link>
      <description>&amp;gt;$MYCOUNT increases but $CAID remains same (second line from the varlist.txt&lt;BR /&gt;&lt;BR /&gt;I believe we have told you why.  Though it really should have ALL lines.&lt;BR /&gt;&lt;BR /&gt;&amp;gt;Hope that is clear now.&lt;BR /&gt;&lt;BR /&gt;You need to try my while read loop suggestion or say why it doesn't work.</description>
      <pubDate>Fri, 22 Jun 2007 23:43:40 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/read-variables-from-file/m-p/4025243#M93685</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2007-06-22T23:43:40Z</dc:date>
    </item>
    <item>
      <title>Re: Read variables from file</title>
      <link>https://community.hpe.com/t5/operating-system-linux/read-variables-from-file/m-p/4025244#M93686</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;this works, whether lines contain spaces or not:&lt;BR /&gt;&lt;BR /&gt;OIFS="$IFS"&lt;BR /&gt;IFS=&lt;BR /&gt;typeset -i i=0&lt;BR /&gt;while read a&lt;BR /&gt;do print $((i+=1)) $a&lt;BR /&gt;done &lt;BR /&gt;Depending on your real needs it may be necessary to reset IFS to $oIFS in the loop.&lt;BR /&gt;&lt;BR /&gt;mfG Peter</description>
      <pubDate>Sat, 23 Jun 2007 08:26:20 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/read-variables-from-file/m-p/4025244#M93686</guid>
      <dc:creator>Peter Nikitka</dc:creator>
      <dc:date>2007-06-23T08:26:20Z</dc:date>
    </item>
    <item>
      <title>Re: Read variables from file</title>
      <link>https://community.hpe.com/t5/operating-system-linux/read-variables-from-file/m-p/4025245#M93687</link>
      <description>&lt;!--!*#--&gt;Hi:&lt;BR /&gt;&lt;BR /&gt;My thinking is the same as Dennis' and Peter.  I would urge you to examine the manpages for 'read' and the 'sh-posix' pages that discuss the shell's IFS (Inter-Field-Seperator).&lt;BR /&gt;&lt;BR /&gt;For example, if you wanted to read a file named 'varlist.txt' with whitespace as field delimiters, capturing in each iteration, the the *second* *through* *last* fields of each line in a variable named 'F2_N', you could do:&lt;BR /&gt;&lt;BR /&gt;#!/usr/bin/sh&lt;BR /&gt;typeset -i MYCOUNT=0&lt;BR /&gt;while read F1 F2_N&lt;BR /&gt;do&lt;BR /&gt;    (( MYCOUNT=MYCOUNT+1 ))&lt;BR /&gt;    echo "${MYCOUNT} -&amp;gt; ${F2_N}" &amp;gt;&amp;gt; out.txt&lt;BR /&gt;done &amp;lt; varlist.txt&lt;BR /&gt;&lt;BR /&gt;...Note that you need to write your output file (out.txt) in append mode (&amp;gt;&amp;gt;) to retain every iteration of the loop's output.&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Sat, 23 Jun 2007 15:57:12 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/read-variables-from-file/m-p/4025245#M93687</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2007-06-23T15:57:12Z</dc:date>
    </item>
    <item>
      <title>Re: Read variables from file</title>
      <link>https://community.hpe.com/t5/operating-system-linux/read-variables-from-file/m-p/4025246#M93688</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;an adding to JRF's solution:&lt;BR /&gt;You can possibly move the output redirection to the end of the loop, like&lt;BR /&gt;OIFS="$IFS"&lt;BR /&gt;IFS=&lt;BR /&gt;typeset -i i=0&lt;BR /&gt;while read a&lt;BR /&gt;do print $((i+=1)) $a&lt;BR /&gt;done &lt;INFILE&gt;outfile&lt;BR /&gt;&lt;BR /&gt;Advantages:&lt;BR /&gt;- outfile needs not be cleared before the loop&lt;BR /&gt;- only one 'open' call instead of one per loop count&lt;BR /&gt;&lt;BR /&gt;Additional output in the inner of the loop has to be written NOT to stdout, however.&lt;BR /&gt;&lt;BR /&gt;mfG Peter&lt;/INFILE&gt;</description>
      <pubDate>Mon, 25 Jun 2007 01:50:20 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/read-variables-from-file/m-p/4025246#M93688</guid>
      <dc:creator>Peter Nikitka</dc:creator>
      <dc:date>2007-06-25T01:50:20Z</dc:date>
    </item>
    <item>
      <title>Re: Read variables from file</title>
      <link>https://community.hpe.com/t5/operating-system-linux/read-variables-from-file/m-p/4025247#M93689</link>
      <description>&lt;!--!*#--&gt;Hi (again) Amardeep:&lt;BR /&gt;&lt;BR /&gt;Peter's excellent ideas suggest another approach.&lt;BR /&gt;&lt;BR /&gt;The code below accomodates:&lt;BR /&gt;&lt;BR /&gt;1.  The output file is automatically truncated (cleared) at the script's onset.&lt;BR /&gt;&lt;BR /&gt;2.  Only one open() call occurs.&lt;BR /&gt;&lt;BR /&gt;3.  *ALL* output in the loop is written to the output file regardless of the number of 'echo' or 'print' calls.&lt;BR /&gt;&lt;BR /&gt;4.  One argument can be optionally passed to the script to define the output file name.  If no argument (filename) is provided, output is written to the current directory in a file named the same as the basename of the script with ".out" as a suffix.&lt;BR /&gt;&lt;BR /&gt;# cat reader&lt;BR /&gt;#!/usr/bin/sh&lt;BR /&gt;typeset    MYOUTPUT=${1:-$0.out}&lt;BR /&gt;typeset -i MYCOUNT=0&lt;BR /&gt;exec  1&amp;gt; ${MYOUTPUT}&lt;BR /&gt;while read F1 F2_N&lt;BR /&gt;do&lt;BR /&gt;    (( MYCOUNT=MYCOUNT+1 ))&lt;BR /&gt;    echo "${MYCOUNT} -&amp;gt; ${F2_N}"&lt;BR /&gt;done &amp;lt; varlist.txt&lt;BR /&gt;&lt;BR /&gt;...when run as:&lt;BR /&gt;&lt;BR /&gt;# ./reader&lt;BR /&gt;&lt;BR /&gt;...output will be found in ./reader.out&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Mon, 25 Jun 2007 18:32:52 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/read-variables-from-file/m-p/4025247#M93689</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2007-06-25T18:32:52Z</dc:date>
    </item>
    <item>
      <title>Re: Read variables from file</title>
      <link>https://community.hpe.com/t5/operating-system-linux/read-variables-from-file/m-p/4025248#M93690</link>
      <description>Hi James, Peeter, Dennis.&lt;BR /&gt;&lt;BR /&gt;Thanks for your suggestions. I finally got it working with Dennis' first suggestion of using "read LINE" option. While I tried others found this option is more easy to explain and easy to use.&lt;BR /&gt;&lt;BR /&gt;Thanks again for all your help... you guys are just something more than a help, so called Gurus.</description>
      <pubDate>Tue, 26 Jun 2007 13:06:24 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/read-variables-from-file/m-p/4025248#M93690</guid>
      <dc:creator>Amar_Joshi</dc:creator>
      <dc:date>2007-06-26T13:06:24Z</dc:date>
    </item>
  </channel>
</rss>

