Help copy variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help copy variable
# 1  
Old 08-24-2014
Help copy variable

Dear All,

I want to copy a value from a file as follows:
file contains line
Code:
xVal = 10
yVal = 20
zVal = 30

now if I use grep command as follows
Code:
grep -r -n "xVal =" fileName

it will output
Code:
##:xVal = 10

I want to store that value(10) in some local shell variable.

Can somebody help me.

Thanks & Regards,
linuxUser_
# 2  
Old 08-24-2014
Any attempts from your side?

---------- Post updated at 15:30 ---------- Previous update was at 15:25 ----------

However, try (untested):
Code:
while read XV E VAL; do if [ "$XV" = "xVal" ] && break; done < file

VAL now should contain 10.
# 3  
Old 08-24-2014
Trying something like this.
Code:
#!/bin/sh
awk '/^xVal\=\         {VAL=$2}' OFS="\t" fileName
sed -i s/"xVal = 5"/"xVal = $VAL"/g otherFileName

no success yet

---------- Post updated at 07:07 PM ---------- Previous update was at 07:00 PM ----------

Code:
#!/bin/sh

while read XV E VAL; do if [ "$XV" = "xVal" ] && break; done < file1;
sed -i s@"xVal = 60.0"@"xVal = $VAL"@g file2;

it giving error says
Code:
Syntax error: "done"

# 4  
Old 08-24-2014
Longhand using OSX 10.7.5, default bash terminal...
Code:
#!/bin/bash
# getvar.sh
echo 'xVal = 1234567890
yVal = 20
zVal = 30' > /tmp/var
# Working code start...
while read text
do
	if [ "${text:0:7}" == "xVal = " ]
	then
		value=${text:7}
		break
	fi
done < /tmp/var
# Working code end...
# The value and number of characters ensuring no leading whitespaces...
echo "$value, ${#value}"

Results:-
Code:
Last login: Sun Aug 24 15:12:24 on ttys000
AMIGA:barrywalker~> chmod 755 getvar.sh
AMIGA:barrywalker~> ./getvar.sh
1234567890, 10
AMIGA:barrywalker~> _

# 5  
Old 08-24-2014
RudiC probably meant to leave out if. However this approach is a bit flawed in the sense that if xVal is not present in the file then VAL will probably still pick up a vaiue.

Try this modification:

Code:
somevar=
while read label dummy val
do
  if [ "$label" = xVal ]; then 
    somevar=$val
    break
  fi
done < file

This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 08-24-2014
Dear Scrutinizer,

Your code working fine. But I want to learn how it is working exactly.
Can you explain something?
also I want to fine xVal based on its occurrence.
I mean if file containg
Code:
//*********************************\\
Build   : xxxxxxxxxxxx
Date    : Aug 24 2014
Host    : linuxUser
PID      : 3004 
xVal     : 34.0
yVal     : 101.92
:
:
xVal = 10
yVal = 20
....

Your code reading 1st xVal.
I taught "xVal =" will read exact the value i want. But how can I specify it in while loop?

---------- Post updated at 08:21 PM ---------- Previous update was at 08:19 PM ----------

To be more specific I want to read a xVal where the line starts with xVal && followed by =, then i want to read value as shown below
Code:
xVal = VALUE

---------- Post updated at 08:23 PM ---------- Previous update was at 08:21 PM ----------

Dear wisecracker,
I really didnt understand where you are reading file.
where is your file to the code?
Quote:
Originally Posted by wisecracker
Longhand using OSX 10.7.5, default bash terminal...
Code:
#!/bin/bash
# getvar.sh
echo 'xVal = 1234567890
yVal = 20
zVal = 30' > /tmp/var
# Working code start...
while read text
do
    if [ "${text:0:7}" == "xVal = " ]
    then
        value=${text:7}
        break
    fi
done < /tmp/var
# Working code end...
# The value and number of characters ensuring no leading whitespaces...
echo "$value, ${#value}"

Results:-
Code:
Last login: Sun Aug 24 15:12:24 on ttys000
AMIGA:barrywalker~> chmod 755 getvar.sh
AMIGA:barrywalker~> ./getvar.sh
1234567890, 10
AMIGA:barrywalker~> _

# 7  
Old 08-24-2014
It uses space as s separator between words. So when the read statement is executed on that particular line, label will contain xVal , dummy will contain = and val will contain 10 .

So if we use this line instead, then it should work..

Code:
  if [ "$label" = xVal ] && [ "$dummy" = '=' ]; then

You will probably want to use a different name than "dummy" since now it has a function..

Last edited by Scrutinizer; 08-24-2014 at 12:53 PM..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to copy script output to a variable using same script?

I'm trying to copy script output and use it in this same script as a variable, and call the variable when script is compiled. The script is below. #!/bin/bash output=$(script) while read line; do if ]; then grep "$line" logfile.txt # Create text file echo "From: IT ... (4 Replies)
Discussion started by: SysAdminRialto
4 Replies

2. Shell Programming and Scripting

how to copy the directory but not copy certain file

Hi experts cp bin root src /mnt but not copy bin/bigfile any help? ( I post this thread in the "redhat" forum wrongly, I don't know how to withdraw that question in that wrong forum) Thanks (6 Replies)
Discussion started by: yanglei_fage
6 Replies

3. Shell Programming and Scripting

Shell Script to Search for a particular String and copy the timestamp to a variable

Hi, We Perfrom Loads to the database through a Perl script which generates a statistics file. I need to read the statistics. the Statistics file looks something like below: Process Beginning - 08-26-2010-23.41.47 DB2 CONNECTION SUCCESSFUL! Ready to process and load file: FILENAME # of... (2 Replies)
Discussion started by: Praveenkulkarni
2 Replies

4. Shell Programming and Scripting

Making script show command (e.g. copy) being executed and variable substitution?

When script is running you only see when some of the commands are not successfull. Is there a way to see which command are executed and to show the substitution of variables as every line is executed ? (3 Replies)
Discussion started by: gr0124
3 Replies

5. Shell Programming and Scripting

Parse file and copy value to variable in sh script.

Hi, I need to parse a simple text file like below and store the word that starts with BR* to a variable say $BRno. I need to do this in sh script. NOTE: the length of the numbers following BR is not constant (eg: it could be BR1234 or BR22233). And there is only 1 BRxxxxx in a file at a given... (6 Replies)
Discussion started by: script2010
6 Replies

6. Shell Programming and Scripting

How to define a variable with variable definition is stored in a variable?

Hi all, I have a variable say var1 (output from somewhere, which I can't change)which store something like this: echo $var1 name=fred age=25 address="123 abc" password=pass1234 how can I make the variable $name, $age, $address and $password contain the info? I mean do this in a... (1 Reply)
Discussion started by: freddy1228
1 Replies

7. Shell Programming and Scripting

Copy part of a variable

Hi, i was using a input file to get the last line of the file.But now i have stored the values from the file to a variable and want the last line from the variable . Slightly confused on how to extract that data from the variable. previous code, cat input.txt <TIME>00:15:48</TIME>... (2 Replies)
Discussion started by: Shellslave
2 Replies

8. UNIX for Dummies Questions & Answers

What is in-core copy and disk-copy of i-node table?

I have found a question from the exercises of my study mat. The question is "Why are there a in-core copy and a disk-copy of i-node block and super block?" If any one know the proper answer then please send me..... (1 Reply)
Discussion started by: dearanik
1 Replies

9. Shell Programming and Scripting

Representing dir path for copy through env variable

Hello , i am on linux, and im trying to figure out why my cp command cant copy one file. I am trying to use variable in which i stored path to location where is file i wish to copy. $ echo $ORA_ALERT_LOG /u01/app/oracle/diag/rdbms/ring11/ring11/trace $ $ $ pwd /home/oracle $ cp -p... (2 Replies)
Discussion started by: tonijel
2 Replies
Login or Register to Ask a Question