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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
01-18-2005, 04:05 PM
|
#1
|
Member
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396
Rep:
|
Retrieving variables from contents of files using awk or any other bash tool
Hi, this is my first post so go easy...
I have two database files files called customer and vehicle. Basically they look slightly like this
CUSTOMER:
NUMBER 1
NAME John
SURNAME Stevenson
Age 28
VEHICLE:
MAKE Audi
Model TT
NUMBER 1
AGE 4
I need to write a script that searches the vehicle database for a make and model and then retrieves the customer number and searches (then lists) details from the customer file. If possible i'd like details listed on one like like this:
NAME SURNAME AGE
John Stevenson 28
What ive got so far is the customer number in a temp file, I was thinking I could somehow assign the contents as a variable using awk and then search through customer with this variable... Harder than it sounds. I imagine I'll probably need combinations of cat, grep, awk and maybe sed.
Sorry if this is a really basic question, I've searched the web a lot for awk tutorials but 80% of the stuff I've read is EXTREMLEY difficult to understand.
Thankyou SOOOOO much in advance 
|
|
|
01-18-2005, 06:08 PM
|
#2
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.x
Posts: 18,443
|
I'd be curious to see how multiple recs in each file are laid out, but to be honest I'd HIGHLY recommend using a DB eg MySql or it's going to be pretty complicated and slow, and you'd have to write a fair new chunk of code for each new type of 'select' or 'update/insert/delete'.
A possible alternative is to use Perl hashes.
HTH
|
|
|
01-19-2005, 08:00 AM
|
#3
|
Member
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396
Original Poster
Rep:
|
It's skoolwork and were only allowed to use bash... BUT I managed to sort it, I'm just having some problems outputting line numbers to a variable and outputting everything to one line.
I'll probably set up a new thread or something,
If your interested i did this:
File=/media/floppy/temp
{
read CUST
} < $File
cat /media/floppy/customer | grep "$CUST" -A4 > temp
awk '/surname/ {print $2}' temp
Theres lines missing inbetween but essentially these are the crucial lines. As you can probably tell it can only deal with one record  Im tryna make it count lines and then read based on the ammount of lines it counts and assign each one of them a variable.
Thanks anyway, I really appreciate it
P.S
your completelly 100% right it would of been much easier doing this in... well anything but bash basically. I got some help and someone I know managed to do the whole thing in like 5 minutes with perl.
|
|
|
01-19-2005, 08:32 AM
|
#4
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516
|
How about coverting them to flattened one line records and using the join
command?
just a thought.
|
|
|
01-19-2005, 04:19 PM
|
#5
|
Member
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396
Original Poster
Rep:
|
Thanks for the idea but I thought you needed 2 files for join. I've got past the comparison bit of my code I'm just working on displaying it now.
Thanks a lot anyway mate 
|
|
|
01-20-2005, 03:42 AM
|
#6
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516
|
Quote:
Thanks for the idea but I thought you needed 2 files for join. I've got past the comparison bit of my code I'm just working on displaying it now.
|
details, details ....

|
|
|
01-20-2005, 11:24 AM
|
#7
|
Member
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396
Original Poster
Rep:
|
I cant believe we've been asked to do this work, its a joke! The project is so complicated and we cant use switch statements. Worse of all our lecturer doesnt even teach programming, teaches cisco! Here I am writing a 100+ line code... wheres that angry smiley!!!
I'll paste my code (or at least that bit) if your really interested but its my first ever bash script as a result its EXTREMELY complex and I imagine at least 3/4 of it is unnecessary.
Thanks for all your help once again people and the speedy reply. Lemmie know if you sincerely are interested in my incredibly complex code...
|
|
|
01-20-2005, 06:02 PM
|
#8
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.x
Posts: 18,443
|
By the end of this you'll be a very good bash shell guy then ... 
|
|
|
01-21-2005, 03:27 AM
|
#9
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516
|
Don't knock yerself out.
Lecturers never read the bloody
code anyway.
You do a lovely structured bit of work and you'll
get the same grade as some git who copied from his friend!

|
|
|
All times are GMT -5. The time now is 08:35 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|