LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-22-2006, 01:01 AM   #1
jayesha18
LQ Newbie
 
Registered: Jan 2006
Posts: 2

Rep: Reputation: 0
multiple variable from differnet file


Dear all,
I trying to write the shell script , which will take variables from a 3 different files , and display all at once on one line.
Example :--
======
I have following 3 files , which contain data in following order
1) /tmp/name
2) /tmp/age
3) /tmp/country
[root@jayesh ~]# cat /tmp/name
"sachin"
"lara"
"rahul"
[root@jayesh ~]# cat /tmp/age
"35"
"38"
"32"
[root@jayesh ~]# cat /tmp/country
"India"
"Westindies"
"India"

Now I want the output as follows after running the shell script :--

"sachin","India","35"
"lara","Westindies","38"
"raul","India","32"

That means i want to put variable from this 3 file and want to just echo on screen.At present i am only able to get variable from one file only by "for i in " loop
How do i get the above output ? which loop and which command should i use in this case ? plz help and guide me

Thanks & Regards
Jayesh
 
Old 12-22-2006, 06:10 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
I hope this isn't homework.

Easiest way is to use arrays:
Code:
#!/bin/sh -
array0=(`cat  /tmp/name`); array1=(`cat  /tmp/age`); array2=(`cat  /tmp/country`)
for i in `seq 0 $[${#array0[@]}-1]`; do
 echo "${array0[$i]},${array1[$i]},${array2[$i]}"
done
exit 0
Note that 0) this will only work if the position of the data in all files is synced, 1) if you have space separated values in any of the files you must fiddle with the IFS and 2) I assert you are capable of adding the double quotes back in yourself.


Code:
function help() { echo "Bash scripting guides:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html 
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/index.html 
http://www.tldp.org/LDP/abs/html/"; }
Nota bene: do yourself a favour and do not script as root account user.
 
Old 12-22-2006, 10:14 AM   #3
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
The really easiest way (just kidding unSpawn ) is to use the paste command (check the man!).

To acomplish what you want just write:
Code:
paste -d , name country age
And off you go

Cheers!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Javascript multiple form variable update problem Phyrexicaid Programming 4 01-12-2006 01:38 AM
Login from a differnet system carnold Linux - Networking 0 05-10-2005 07:40 AM
How to read variable from one file & update its value in another file minil Programming 1 03-22-2005 12:12 AM
multiline grep result > multiple variable? mikshaw Linux - Software 5 12-25-2003 10:13 PM
need to innitialize string variable with multiple spaces clsonnt Programming 3 08-11-2003 10:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration