how to attach a variable from the 2nd file to the 1st file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to attach a variable from the 2nd file to the 1st file
# 1  
Old 05-31-2008
how to assign the value from the 2nd file to the 1st file, line by line..

Hi all,
I am a newbie in unix shell script world. Say, I have a list of devices in file01 and then also have file02 with the list of the location. I need to telnet to each devices in file01 and set the location according to file02. Example:
#cat file01
ttnpx01
ttnpx02
nncrd01
nncrd02
nkcrs01

#cat file02
liverpool
newtown
stockland
greenfield
newyork

With the for loop I can telnet to each device from file01, but I am not sure how to retrieve the variable from file02 and then attach to device. This is my basic script
#cat myscript
for host in `cat file01`
do
telnet $host
set location ...
done

Below is the result that I try to achieve:
ttnpx01 --> will have the location set to liverpool
ttnpx02 --> will have the location set to newtown
nncrd01 --> will have the location set to stockland
nncrd02 --> will have the location set to greenfield
nkcrs01 --> will have the location set to newyork

Can you please help? Thanks

Last edited by lo tan; 05-31-2008 at 09:22 AM..
# 2  
Old 05-31-2008
One way:

Code:
awk 'NR==FNR{a[NR]=$0;next}{print a[FNR], $0}' file01 file02 |
while read host location
do
   echo $host 
   echo $location
   # do other stuff here
done

Regards
# 3  
Old 05-31-2008
wow, thank you for your prompt respond. Your code looks good, I will check it then let you know the result. Again, thank you.
# 4  
Old 06-01-2008
Quote:
Originally Posted by Franklin52
One way:

Code:
awk 'NR==FNR{a[NR]=$0;next}{print a[FNR], $0}' file01 file02 |
while read host location
do
   echo $host 
   echo $location
   # do other stuff here
done

Regards
Hi Franklin52,
I have tried your code, it works fine with a linux machine. However, when I tried it with a SunOS, it failed due to the awk could not combine 2 files side by side. As a newbie I am still in learning process and have not much experiences with the awk, would you able to lend me a hand please?
Below is the result from the awk and I think it is not what you meant. Thanks
#awk 'NR==FNR{a[NR]=$0;next}{print a[FNR], $0}' file01 file02
ttnpx01
ttnpx02
nncrd01
nncrd02
nkcrs01
liverpool
newtown
stockland
greenfield
newyork
# 5  
Old 06-02-2008
Quote:
Originally Posted by lo tan
Hi Franklin52,
I have tried your code, it works fine with a linux machine. However, when I tried it with a SunOS, it failed due to the awk could not combine 2 files side by side. As a newbie I am still in learning process and have not much experiences with the awk, would you able to lend me a hand please?
Below is the result from the awk and I think it is not what you meant. Thanks
#awk 'NR==FNR{a[NR]=$0;next}{print a[FNR], $0}' file01 file02
ttnpx01
ttnpx02
nncrd01
nncrd02
nkcrs01
liverpool
newtown
stockland
greenfield
newyork
Try it with nawk.

Regards
# 6  
Old 06-03-2008
Quote:
Originally Posted by Franklin52
Try it with nawk.

Regards
Yes, it works with nawk. Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare 1st column from 2 file and if match print line from 1st file and append column 7 from 2nd

hi I have 2 file with more than 10 columns for both 1st file apple,0,0,0...... orange,1,2,3..... mango,2,4,5..... 2nd file apple,2,3,4,5,6,7... orange,2,3,4,5,6,8... watermerlon,2,3,4,5,6,abc... mango,5,6,7,4,6,def.... (1 Reply)
Discussion started by: tententen
1 Replies

2. UNIX for Beginners Questions & Answers

Check Error File attach and email zip file

I need something to say if these two file extensions exist in this directory *err and *rpt zip up these files into one zip file and email them to me. If they don't exist wait 2 hours and check again.... Not sure how to determine if I need to do an if then statement or a while true or a for... (1 Reply)
Discussion started by: xgringo
1 Replies

3. Shell Programming and Scripting

Unable to attach a .txt file or .log file to mail and mailx command

Hi, I am trying to attach a .log file or .txt file to mail command to send an email once my ksh script executed. I am unable to use mutt command as it has been not installed and i am not supposed to install it. I have tried many ways by googling which has not helped me to succeed. Here is my... (5 Replies)
Discussion started by: Samah
5 Replies

4. Shell Programming and Scripting

Appending information from 2nd file into 1st based on intervals

Hi, I am trying to gather information from the second file and append it to the first file. input HWUSI-EAS000_29:1:100:10000:11479#0/1 + chr5 14458050 ATTGGCTGAGGTCCTACTAGTTGTGATGTGTAAGTGT HHHHHHGDGGEDGGGDGCGEDDEFFFAGE 0 second file:... (14 Replies)
Discussion started by: Diya123
14 Replies

5. Shell Programming and Scripting

Grep/Awk on 1st 2 Letters in 2nd Column of File

Hi everyone. I need to change a script (ksh) so that it will grep on the 1st 2 letters in the second column of a 5 column file such as this one: 192.168.1.1 CAXY0_123 10ABFL000001 # Comment 192.168.1.2 CAYZ0_123 10ABTX000002 # Comment 192.168.2.1 FLXY0_123 11ABCA000001 ... (4 Replies)
Discussion started by: TheNovice
4 Replies

6. Shell Programming and Scripting

Append 1st field from a file into 2nd field of another file

Hi, I've internally searched through forums for about 2+ hours. Unfortunately, with no luck. Although I've found some cases close to mine below, but didn't help so much. Actually, I'm in short with time. So I had to post my case. Hoping that you can help. I have 2 files, FILE1 ... (1 Reply)
Discussion started by: amurib
1 Replies

7. Shell Programming and Scripting

Appending 1st field in a file into 2nd field in another file

Hi, I've internally searched through forums for about 2+ hours. Unfortunately, with no luck. Although I've found some cases close to mine below, but didn't help so much. Actually, I'm in short with time. So I had to post my case. Hoping that you can help. I have 2 files, FILE1 ... (0 Replies)
Discussion started by: amurib
0 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

compare two files and make 1st file same as 2nd file

I am trying to compare two file and make changes where ever its different. for example: Contents of file1 IP=192.165.89.11 NM=255.255.0.0 GW=192.165.89.1 Contents of file2 IP=192.165.89.11 NM=255.255.255.255 GW=192.165.89.1 NOTE HERE THAT NM IS DIFFERENT So i want the changes... (6 Replies)
Discussion started by: pradeepreddy
6 Replies

10. Shell Programming and Scripting

How to attach an excel file/ dat file thru unix mails

Hi. I want to attach a .xls or .dat file while sending mail thru unix. I have come across diff attachments sending options, but allthose embeds the content in the mail. I want the attachement to be send as such. Please help me out. regards Diwakar (1 Reply)
Discussion started by: diwakar82
1 Replies
Login or Register to Ask a Question