Defined Variable from text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Defined Variable from text file
# 1  
Old 03-30-2012
Bug Defined Variable from text file

Hi Guys,

I have one text file ABC.txt...It have 3 lines

List=/home/klk/dir/ABC.txt

Code:
Leajnk123
KJUHIO1234
IJOKIJ7676

I want use as different variable of each line.Just like

Code:
X=firstline
Y=Secound Line
Z=Third Line


Thanks
# 2  
Old 03-30-2012
This creates an array, then the array elements are assigned to variables - you should learn how to use the array instead of what I gave you. (there are other ways to do this)
Code:
#!/bin/bash
myarray=( `cat $List | tr -s '\n' ' '`)
X=${myarray[0]}
Y=${myarray[1]}
Z=${myarray[2]}

This is what you asked for but it is not very useful, IMO.
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 03-30-2012
This method requires no BASH-specific features.

Code:
#!/bin/sh

exec 5<filename # Open file into FD 5
read X <&5 # Read from FD 5 into variables
read Y <&5
read Z <&5
exec 5<&- # Close FD 5

This User Gave Thanks to Corona688 For This Post:
# 4  
Old 03-30-2012
Also in any POSIX compliant shell:
Code:
for i in X Y Z; do 
  read $i
done < infile


Last edited by Scrutinizer; 03-30-2012 at 07:17 PM.. Reason: Double quotes were superfluous in this situation
These 2 Users Gave Thanks to Scrutinizer For This Post:
# 5  
Old 03-31-2012
@jim mcnamara
The new mapfile builtin makes it possible to load an array with the contents of a text file without using a loop or command substitution.
Code:
[root@node2 ~]# mapfile ary < <(seq 3) 
[root@node2 ~]# for i in {0..2}; do echo ${ary[i]}; done
1
2
3
[root@node2 ~]# seq 3 > file1
[root@node2 ~]# mapfile bry < file1
[root@node2 ~]# for i in {0..2}; do echo ${bry[i]}; done
1
2
3

This User Gave Thanks to complex.invoke For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Awk: use variable defined in begin

Hi there, I'm working with file more than 400K lines, 60 columns. Column count is going to be multiple of 12: 60, 12, 72 or so. NF/12 gives me on how many iterations I've to do to check certain value. For example: 7, 14th if only 24 columns in file. 7th, 14th and 21st if 36 columns in... (6 Replies)
Discussion started by: genome
6 Replies

2. Shell Programming and Scripting

How do I get variable defined in BASH subshell outside?

I'm a BASH shell user (relatively new) I need to get a variable calculated in a subshell, outside the subshell, when it completes. I can do it, by writing the variable into a file, and then reading the file again when outside the subshell. I've tried lots of things from exporting to environmental... (3 Replies)
Discussion started by: goreilly
3 Replies

3. UNIX for Dummies Questions & Answers

[Solved] Variable defined in .bashrc not intializing in script

I have the variable defined in .bashrc BIN_DIR="/usr/local/dw" and in my shell script i am using below. #!/bin/bash echo "Bin Dir: ${BIN_DIR}" . "${BIN_DIR}"/dwh_Loadfuncs.sh Output: Bin Dir: /usr/local/dw/dwh_LoadXMLFileIntoStage.sh: line 7: /dwh_Loadfuncs.sh: No such file or... (3 Replies)
Discussion started by: Ariean
3 Replies

4. UNIX for Dummies Questions & Answers

Finding WHERE an environment variable is defined.

HI. I had an environment variable defined in my .bash_profile file and I deleted it. I opened a new terminal and looked inside .bash_profile and it's not there but when I type printenv MYCONFIG it reutrns what the value used to be in my .bash_profile still!! I did a grep for MYCONFIG and... (6 Replies)
Discussion started by: dissectcode
6 Replies

5. Shell Programming and Scripting

search the pattern in a file and replace with variable already defined previously in csh

I want to replace a certain pattern with the variable already defined. e.g. set path_verilog = /home/priya/bin/verilogfile my file contents are : verilog new verilog is defined here verilog_path_comes I am using the below command sed 's/verilog_path_comes/'$path_verilog'/g' <filename>... (2 Replies)
Discussion started by: nehashine
2 Replies

6. Shell Programming and Scripting

Unable to add user defined variable

Hi, I have a user defined variable _TIME1=xxx I am using awk command for pattern matching. cat $_LOCATION/catalina.txt | awk '/^`$_TIME1`:??:??/' It not taking the value of $_TIME! eg:I am using the command to get all the patter from 12:00:00 to 12:59:59 The user defined variable... (2 Replies)
Discussion started by: ahamed
2 Replies

7. Shell Programming and Scripting

Finding Variable Value defined in a Script

Hi , I had a script in which there is a variable $LOG defined and I want to check where that variable is getting value from, is there a way I can check for that variable and it's value if it is defined in some other script function or script . I checked using command env and also export... (1 Reply)
Discussion started by: somu_june
1 Replies

8. Shell Programming and Scripting

Force Input in User Defined Variable

In a line such as: echo -n "How many days back would you like to check? "; read days How can I ensure that the user has a.) entered a number between 1-30 (not 0 or 31+) and b.) has not just hit enter (ie set it to "") and if it's entered wrong, how do I start the if statement over? I... (10 Replies)
Discussion started by: earnstaf
10 Replies

9. Shell Programming and Scripting

Replace variable with a user defined variable

I have a file that has a list of entries in a column x z z z x y z The column can have any length and any number of any strings. I need to replace each unique string with a user defined number. I can filter the unique entries out using awk '{if (NF==5) print $2}' file | uniq | nl >... (1 Reply)
Discussion started by: ce124
1 Replies

10. Linux

environment variable is not defined

moved to correct thread (0 Replies)
Discussion started by: alien12
0 Replies
Login or Register to Ask a Question