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
# 1  
Old 11-09-2010
Read the contents of a file and store them in a variable

Hi Gurus,

I am trying for a scenario where in I want to read the contents of a file line by line and then store them in variables. Below is the script:

Code:
#!/bin/ksh

while read line

   do

    id=`echo $line | cut -f1 -d |`
    name=`echo $line | cut -f2 -d |`
     
      echo $id
      echo $name
   done < temp.txt

The contents of the temp file is
Code:
kgsc12333,mike anderson
oxftd14442,mc donald
tddff232233,william carlson

I want to strip the word before and after the "," and store them in 2 variables id and name. But the script is throwing some error.

Please help me on this.

Regards,
Sam
# 2  
Old 11-09-2010
try this:

Code:
#!/bin/ksh
while read line
do
  id=`echo $line | cut -d, -f1`
  name=`echo $line | cut -d, -f2`
  echo $id
  echo $name
done<1.txt


Last edited by Franklin52; 11-09-2010 at 06:22 PM.. Reason: Please indent your code and use code tags, thank you
# 3  
Old 11-09-2010
There's no need to use external commands:
Code:
#!/bin/ksh

while read line
do
  id=${line%,*}
  name=${line#*,}
  echo $id
  echo $name
done < temp.txt

These 2 Users Gave Thanks to Franklin52 For This Post:
# 4  
Old 11-09-2010
Thanks Anurag and Franklin for your quick response. I tried Franklin's approach, its working.

Last edited by svajhala; 11-09-2010 at 06:38 PM..
# 5  
Old 11-09-2010
Franklin's approach looks better and working well for me.
There might be some problem in your script. Please post your script printing each line twice.

Last edited by anurag.singh; 11-09-2010 at 06:47 PM..
# 6  
Old 11-09-2010
Hi all,
You could try this , by letting the IFS (input field separator) handle the logics for You:

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

If You know Your input is well formatted.

Best regards,
Lakris

Last edited by Lakris; 11-09-2010 at 07:11 PM.. Reason: typo
# 7  
Old 11-10-2010
Thanks everyone. I tried Franklin's approach and it is working fine. Now my question is how to get the value from 3rd column. I suppose Franklin's script will only work if there 2 columns. But I also need to get the value from 3rd column. Please help me on this.

Code:
kgsc12333,mike anderson,rwx
oxftd14442,mc donald,rx
tddff232233,william carlson,r

Thanks in advance
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

Featured Tech Videos