Retrieving variables from contents of files using awk or any other bash tool
ProgrammingThis 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.
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.
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.
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
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.
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.
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...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.