Read the contents of a file and store them in a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read the contents of a file and store them in a variable
# 8  
Old 11-10-2010
Use Lakris' solution, it will work
# 9  
Old 11-10-2010
Code:
#!/bin/ksh
IFSsav="$IFS"
IFS=,
typeset -i cnt
cat temp.txt | while read line
do
    set $line
    cnt=1
    while [ $cnt -le $# ]
    do
        eval "val=\$$cnt"
        echo $val
        cnt=cnt+1
    done
done
IFS="$IFSsav"

for temp.txt
Code:
kgsc12333,mike anderson
oxftd14442,mc donald
tddff232233,william carlson
vhfdgfkd,jfhjfhsdjklf,jhfhdf,1234
jkfgh djfh, djfh  dfhfh, jkhdj 323

output is
Code:
kgsc12333
mike anderson
oxftd14442
mc donald
tddff232233
william carlson
vhfdgfkd
jfhjfhsdjklf
jhfhdf
1234
jkfgh djfh
 djfh  dfhfh
 jkhdj 323


Last edited by anurag.singh; 11-10-2010 at 06:28 PM..
# 10  
Old 11-11-2010
Hi, something like this?

Code:
#!/bin/ksh
IFS=,
while read id name attr; do
echo $id
echo $name
echo $attr
done < id-name.lst

Best regards,
Lakris
# 11  
Old 11-11-2010
@lakris. Do not forget to save IFS in a variable so it can be restored to its original value afterwards.
There is another possibility:
Code:
while IFS=, read id name attr x
do
  echo "$id"
  echo "$name"
  echo "$attr"
done < temp.txt

With this construct IFS does not need to be restored afterwards. (I included an extra variable (x), because I suspect the OP may have a line with even more fields...)

Last edited by Scrutinizer; 11-11-2010 at 03:42 AM..
# 12  
Old 11-11-2010
Good point, especially if one does other stuff after, that depends on IFS. Embedding it in the while statement is of course the best option.

/L
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to read a value from a file and store in a variable?

Hi, I have a file service.xml which has following content: <?xml version="1.0" encoding="UTF-8"?> <Service Ver="2.31.13"/> I want to read the value of Ver (that is 2.31.13) and assign to a variable which i further use. Please help me in that. (3 Replies)
Discussion started by: laxmikant15
3 Replies

2. Shell Programming and Scripting

Folder contents getting appended as strings while redirecting file contents to a variable

Hi one of the output of the command is as below # sed -n "/CCM-ResourceHealthCheck:/,/---------/{/CCM-ResourceHealthCheck:/d;/---------/d;p;}" Automation.OutputZ$zoneCounter | sed 's/$/<br>/' Resource List : <br> *************************** 1. row ***************************<br> ... (2 Replies)
Discussion started by: vivek d r
2 Replies

3. Shell Programming and Scripting

How to read a file line by line and store it in a variable to execute a program ?

Hello, I am quite new in shell scripting and I would like to write a little scritp to run a program on some parameters files. all my parameters files are in the same directory, so pick them up with ls *.para >>dirafter that I have a dir file like that: param1.para param2.para etc... I... (2 Replies)
Discussion started by: shadok
2 Replies

4. Homework & Coursework Questions

How to read contents of a file into variable :(

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I have to read the contents of each field of a file creating user accounts. The file will be of format : ... (6 Replies)
Discussion started by: dude_me5
6 Replies

5. Shell Programming and Scripting

How to read contents of a file into variable :(

My file is in this format : username : student information : default shell : student ID Eg : joeb:Joe Bennett:/bin/csh:1234 jerryd:Jerry Daniels:/bin/csh:2345 deaverm: Deaver Michelle:/bin/bash:4356 joseyg:Josey Guerra:/bin/bash:8767 michaelh:Michael Hall:/bin/ksh:1547 I have to... (1 Reply)
Discussion started by: dude_me5
1 Replies

6. Shell Programming and Scripting

read contents of a file with serveral lines & assign it to a variable

Hi All I have a file for ex .log file which contain several lines within it. I have to read that file contents & assing that to a variable. (2 Replies)
Discussion started by: satyam.sumit
2 Replies

7. Shell Programming and Scripting

read xml tag attribute and store it in variable

Hi, How to read xml tag attributes and store into variable in shell script? Thanks, Swetha (5 Replies)
Discussion started by: swetha123
5 Replies

8. UNIX for Dummies Questions & Answers

Read a file and store each value in a variable

Hi, How to read a file and put the values in a script. E.g. file1.txt 02/12/2009;t1;t2 The script should read this file and put these values in 3 different variables x1,x2,x3 which can be used further. Thanks Ashu (3 Replies)
Discussion started by: er_ashu
3 Replies

9. UNIX for Dummies Questions & Answers

Read and store each line of a file to a variable

Hi all, I'm quite new to unix and hope that someone can help me on this. I'm using csh. Below is what i intend to do. 1. I stored some data in a file. 2. I intend to read the file line by line and store each line of data into a variable, so that i can used it later. Anyone have any... (4 Replies)
Discussion started by: seijihiko
4 Replies

10. UNIX for Advanced & Expert Users

how to read each letter from file and store it in variable.

Dear friends, i am writing csh script i have one dat file containing following data.like this. 08FD3 03A26 000FA0 FFFF0 BBA0F 00000 00000 from the above file i want to read each letter and store it in one variable. how it is possible. please help (7 Replies)
Discussion started by: rajan_ka1
7 Replies
Login or Register to Ask a Question