How to get values from one file to other file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get values from one file to other file?
# 1  
Old 02-06-2015
How to get values from one file to other file?

Such as I have one file:
file1
====
Code:
hostname=abcd1234
server name=qwer1234

I need get the value of hostname: abcd1234 and the server name value to input file2, how to write a script do that? Thanks a lot!
file2.sh
======
Code:
#!/bin/sh
var1=$hostname
var2=$server name

echo $var1
echo $var2


Last edited by Scrutinizer; 02-06-2015 at 12:52 PM.. Reason: CODE tags
# 2  
Old 02-06-2015
Is the space in the variable name a requirement? Else you can just source file1 in file2.sh:

Code:
#!/bin/sh

. ./file1

var1=$hostname
var2=$server_name

echo $var1
echo $var2

# 3  
Old 02-06-2015
Did you try source file1 before the var= parts from within file2.sh yet?

hth
# 4  
Old 02-06-2015
Quote:
Originally Posted by Walter Misar
Is the space in the variable name a requirement? Else you can just source file1 in file2.sh:

Code:
#!/bin/sh

. ./file1

var1=$hostname
var2=$server_name

echo $var1
echo $var2

Sorry, maybe I don't write clearly.
my means if the file1 not hostname, just like A=abcd1234, how to get the abcd1234 and input in file2?

File1:

Code:
A=abc1234d
B=qwer1234

File2:
Code:
var1=$A
var2=$B

echo $var1
echo $var2

Thanks a lot!

Last edited by Scrutinizer; 02-06-2015 at 03:46 PM..
# 5  
Old 02-06-2015
What's the difference? Try the solutions proposed...
# 6  
Old 02-06-2015
Maybe we have not been clear. There is obviously a language barrier here.

You cannot have a space character in a shell variable name. So you cannot have a variable named server name.

If you change file1 to contain the text:
Code:
hostname=abcd1234
server_name=qwer1234

then if you change the contents of file2.sh as Walter Misar suggested to:
Code:
#!/bin/sh

. ./file1

var1=$hostname
var2=$server_name

echo $var1
echo $var2

and run your script, it should print:
Code:
abcd1234
qwer1234

If this isn't what you are trying to do, we have completely misunderstood what you are trying to accomplish.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 02-06-2015
Thank you, the . ./file1 works
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to look up values in File 2 from File 1, & printingNth field of File1 based value of File2 $2

I have two files which are the output of a multiple choice vocab test (60 separate questions) from 104 people (there are some missing responses) and the question list. I have the item list in one file (File1) Item,Stimulus,Choice1,Choice2,Choice3,Choice4,Correct... (5 Replies)
Discussion started by: samonl
5 Replies

2. Shell Programming and Scripting

Taking key values from one file and extracting values from another file

Hi, I have two files with values in both. File1: cat 2 3 dog 4 5 elephant 6 7 camel 2 3 File2: ----+--gkf;ajf= ---+---- +----- cat -------=----+ 3 | 4 ----- dog ------++-- 5 | 9 ----++-- elephant | 5 | 7 ---++ camel ------ ++++_---- || 8 | 9 I want the final file as: cat 4... (1 Reply)
Discussion started by: npatwardhan
1 Replies

3. UNIX for Dummies Questions & Answers

Reading Xml file and print the values into the text file in columnwise?

hi guys, i want help... Reding XML file and print the values into the text file using linux shell script file as per below xml file <sequence> <Filename>aldorzum.doc</Filename> <DivisionCode>US</DivisionCode> <ContentType>Template</ContentType> <ProductCode>VIMZIM</ProductCode> </sequence>... (4 Replies)
Discussion started by: sravanreddy
4 Replies

4. UNIX for Dummies Questions & Answers

Reading XML file and print the values in the text file using Linux shell script

hi guys, i want help... Reding XML file and print the values into the text file using linux shell script file as per below xml file <sequence> <Filename>aldorzum.doc</Filename> <DivisionCode>US</DivisionCode> <ContentType>Template</ContentType> <ProductCode>VIMZIM</ProductCode> </sequence>... (1 Reply)
Discussion started by: sravanreddy
1 Replies

5. Shell Programming and Scripting

awk file to read values from Db2 table replacing hard coded values

Hi, I want to replace a chain of if-else statement in an old AWK file with values from Db2 table or CSV file. The part of code is below... if (start_new_rec=="true"){ exclude_user="false"; user=toupper($6); match(user, "XXXXX."); if (RSTART ==2 ) { ... (9 Replies)
Discussion started by: asandy1234
9 Replies

6. 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

7. Shell Programming and Scripting

Compare values in two files. For matching rows print corresponding values from File 1 in File2.

- I have two files (File 1 and File 2) and the contents of the files are mentioned below. - I am trying to compare the values of Column1 of File1 with Column1 of File2. If a match is found, print the corresponding value from Column2 of File1 in Column5 of File2. - I tried to modify and use... (10 Replies)
Discussion started by: Santoshbn
10 Replies

8. Shell Programming and Scripting

remove values of a file one by one from 2nd file and then print the remaining values of 2nd file

Hi all, I have 2 files. One contains only 1 column and other one contains 2 columns, let say 1_col.txt and 2_col.txt respectively. Here, I will try to explain with an example. Input files : 1_col.txt 2_col.txt a a b x a c p ... (5 Replies)
Discussion started by: AshwaniSharma09
5 Replies

9. Shell Programming and Scripting

Replacing values in a file based on values in another file

Hi I have 2 files:- 1. List of files which consists of names of some output files. 2. A delimited file; delimted by "|" I want to replace the value of the $23 (23rd column) in the delimited file with name in the first file. It is always position to position. Meaning first row of the first... (5 Replies)
Discussion started by: pparthiv
5 Replies

10. Shell Programming and Scripting

PERL:How to convert numeric values txt file to PACKED DECIMAL File?

Is there any way to convert numeric values txt file to PACKED DECIMAL File using PERL. Regards, Alok (1 Reply)
Discussion started by: aloktiwary
1 Replies
Login or Register to Ask a Question