- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Read variables from file
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2007 09:08 AM
06-22-2007 09:08 AM
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?
Currently I have this but it doesn't work...
while [ $MYCOUNT -lt $CAIDCOUNT ]
do
CAID=`awk -F '/n' '{print $1}' varlist.txt`
echo "$MYCOUNT $CAID" > out.txt
let MYCOUNT=$MYCOUNT+1
done
Thanks in advance
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2007 09:13 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2007 09:18 AM
06-22-2007 09:18 AM
Re: Read variables from file
~thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2007 01:16 PM - edited 03-08-2015 08:18 PM
06-22-2007 01:16 PM - edited 03-08-2015 08:18 PM
Re: Read variables from file
What are you really trying to do?
If you intended to have your loop above read CAIDCOUNT lines and number them, you should look into "nl -ba".
You could also do it all in awk:
awk '{ print $NR, $0 }' varlist.txt > out.txt
As Tim mentioned, you are reading EVERY line in your file, each time in your loop.
As Tim says you can use a read loop. Assuming you want to read all lines:
(( MYCOUNT = 1 ))
while read LINE; do
echo "$MYCOUNT $LINE"
(( MYCOUNT += 1 ))
done < varlist.txt > out.txt
- Tags:
- awk
- while loop
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2007 04:05 PM
06-22-2007 04:05 PM
Re: Read variables from file
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.
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
Hope that is clear now....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2007 04:43 PM
06-22-2007 04:43 PM
Re: Read variables from file
I believe we have told you why. Though it really should have ALL lines.
>Hope that is clear now.
You need to try my while read loop suggestion or say why it doesn't work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-23-2007 01:26 AM
06-23-2007 01:26 AM
Re: Read variables from file
this works, whether lines contain spaces or not:
OIFS="$IFS"
IFS=
typeset -i i=0
while read a
do print $((i+=1)) $a
done
Depending on your real needs it may be necessary to reset IFS to $oIFS in the loop.
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-23-2007 08:57 AM
06-23-2007 08:57 AM
Re: Read variables from file
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).
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:
#!/usr/bin/sh
typeset -i MYCOUNT=0
while read F1 F2_N
do
(( MYCOUNT=MYCOUNT+1 ))
echo "${MYCOUNT} -> ${F2_N}" >> out.txt
done < varlist.txt
...Note that you need to write your output file (out.txt) in append mode (>>) to retain every iteration of the loop's output.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-24-2007 06:50 PM
06-24-2007 06:50 PM
Re: Read variables from file
an adding to JRF's solution:
You can possibly move the output redirection to the end of the loop, like
OIFS="$IFS"
IFS=
typeset -i i=0
while read a
do print $((i+=1)) $a
done
Advantages:
- outfile needs not be cleared before the loop
- only one 'open' call instead of one per loop count
Additional output in the inner of the loop has to be written NOT to stdout, however.
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2007 11:32 AM
06-25-2007 11:32 AM
Re: Read variables from file
Peter's excellent ideas suggest another approach.
The code below accomodates:
1. The output file is automatically truncated (cleared) at the script's onset.
2. Only one open() call occurs.
3. *ALL* output in the loop is written to the output file regardless of the number of 'echo' or 'print' calls.
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.
# cat reader
#!/usr/bin/sh
typeset MYOUTPUT=${1:-$0.out}
typeset -i MYCOUNT=0
exec 1> ${MYOUTPUT}
while read F1 F2_N
do
(( MYCOUNT=MYCOUNT+1 ))
echo "${MYCOUNT} -> ${F2_N}"
done < varlist.txt
...when run as:
# ./reader
...output will be found in ./reader.out
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-26-2007 06:06 AM
06-26-2007 06:06 AM
Re: Read variables from file
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.
Thanks again for all your help... you guys are just something more than a help, so called Gurus.