Read variables contain spaces from text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read variables contain spaces from text file
# 1  
Old 10-20-2008
Read variables contain spaces from text file

Dears,

I developed a shell script to read varibales from text file as the following:

cat /dev/null > /rename-OUT.txt
while read line
do
set -- `echo $line`
snmpset -c dslmibs $1 sysName.0 octetstring $2
after=$(snmpget -c dslmibs $1 sysName.0 | cut -d: -f3)
echo "$1,$2,$after" >> /rename-OUT.txt
done < /list.txt


It worked fine when variables don't have spaces but when the variable has spaces however I put it between double quotation " ", it didn't work.

"list.txt" example:
172.16.31.242 "HELWAN-R01C-C-EG L1 1GE"

It consider only the part "HELWAN-R01C-C-EG is the second variable but the desired is HELWAN-R01C-C-EG L1 1GE

Please help me.
# 2  
Old 10-20-2008
The echo statment is losing your variables for you.
Code:
> /rename-OUT.txt
while read line
do
  set -- $line
  snmpset -c dslmibs $1 sysName.0 octetstring $2
  after=$(snmpget -c dslmibs $1 sysName.0 | cut -d: -f3)
  echo "$1,$2,$after" 
done < /list.txt  > /rename-OUT.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read variables from a text file for use in csh script

Hello, I have a text file (say, declarevars.txt) that contains multiple lines that are essentially meant to be variable declarations: set arr1 = (var1a var1b var1c) set arr2 = (var2a var2b var2c) . . . I want to be able to read this text file within a csh (sorry) script and have that... (2 Replies)
Discussion started by: arjaydj
2 Replies

2. Shell Programming and Scripting

Read record from the text file contain multiple separated values & assign those values to variables

I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables. I need to read this file which is an input to my script Config.txt file name, first path, second... (7 Replies)
Discussion started by: ketanraut
7 Replies

3. Shell Programming and Scripting

Read variables from a file and then export it.

Hi I want to read variables from one file and then set it as environment variable; The text file is test.txt which contains SPEED:1000 IP:172.26.126.11 My code is: while read line; do var1=`echo $line | awk 'BEGIN {FS=":"} { print $1 }'` echo $var1 var2=`echo $line | awk 'BEGIN {FS=":"}... (8 Replies)
Discussion started by: SSM
8 Replies

4. Shell Programming and Scripting

Read record from the text file & assign those values to variables in the script

For eg: I have sample.txt file with 4 rows of record like: user1|password1 user2|password2 user3|password3 user4|password4 The username and password is sepsrated by '|' I want to get the 1st row value from the file and assign it to two different variables(username and password) in my... (1 Reply)
Discussion started by: priya001
1 Replies

5. Shell Programming and Scripting

Environment Variables in text file and read command

I cannot get the following substitution ($ORACLE_SID) to work: The variable ORACLE_SID is set to wardin my environment. It has been exported. I have a text file called test.dat: /u07/oradata/${ORACLE_SID}/extab/finmart/summit/ps_voucher_line_crnt_ex.dbf... (2 Replies)
Discussion started by: bradyd
2 Replies

6. Shell Programming and Scripting

Read variables and their values from file

Hi, I want to read the variables and the values from the txt file and compare these values with the ones computed by script. for ex: say var.txt contains the variable names and their values: one 1 two 2 three 3 The value of variables "one" "two" and "three" will be computed in the script... (3 Replies)
Discussion started by: bhushana
3 Replies

7. Shell Programming and Scripting

Read file line spaces

I have a script which read a file it does while read -r line, then i echo line out echo "$line" Problem is the echo does not echo space and tabs at the end of each line. How do i get the end of line space as well (6 Replies)
Discussion started by: kelseyh
6 Replies

8. Shell Programming and Scripting

how can i read text file and assign its values to variables using shell

Hello, I have a cat.dat file, i would like shell to read each 3 lines and set this 3 lines to 3 different variables. my cat.dat is: 11 12 +380486461001 12 13 +380486461002 13 14 +380486461003 i want shell to make a loop and assign 1st line to student_id, 2nd line to... (4 Replies)
Discussion started by: rosalinda
4 Replies

9. Shell Programming and Scripting

Adding specific text and spaces to each line in a text file

Hi, I wanted to add specific text to each row in a text file containing three rows. Example: 0 8 7 6 5 5 7 8 9 0 7 9 7 8 9 0 1 2 And I want to add a 21 at the beginning of the first row, and blank spaces at the beginning of the second two rows. To get this: 21 0 8 7 6 5 5 7 8... (4 Replies)
Discussion started by: hertingm
4 Replies

10. Shell Programming and Scripting

ksh - read file with leading spaces

Hi, Could someone has any suggestions on this? When read a line from a file, I need to check the first char in the line, it could be a space or any char. But the leading spaces are removed by read. Thanks. (2 Replies)
Discussion started by: momi
2 Replies
Login or Register to Ask a Question