LinuxQuestions.org
Register a domain and help support LQ
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 01-18-2005, 04:05 PM   #1
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Rep: Reputation: 31
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
 
Old 01-18-2005, 06:08 PM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,289

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
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
 
Old 01-19-2005, 08:00 AM   #3
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Original Poster
Rep: Reputation: 31
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.
 
Old 01-19-2005, 08:32 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,290

Rep: Reputation: 174Reputation: 174
How about coverting them to flattened one line records and using the join
command?

just a thought.
 
Old 01-19-2005, 04:19 PM   #5
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Original Poster
Rep: Reputation: 31
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
 
Old 01-20-2005, 03:42 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,290

Rep: Reputation: 174Reputation: 174
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 ....
 
Old 01-20-2005, 11:24 AM   #7
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Original Poster
Rep: Reputation: 31
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...
 
Old 01-20-2005, 06:02 PM   #8
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,289

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
By the end of this you'll be a very good bash shell guy then ...
 
Old 01-21-2005, 03:27 AM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,290

Rep: Reputation: 174Reputation: 174
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!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
To find Tool in QT designer which displays HTML Contents kiranbud Linux - Software 0 11-24-2005 11:35 PM
moving files that have spaces in variables -bash scripting bhar0761 Programming 10 09-22-2005 07:30 AM
[Bash] Concatenate string using awk senorsnor Programming 7 05-05-2005 12:38 AM
awk: /matching/ variables passed with -v aunquarra Linux - General 2 02-17-2005 06:47 PM
Retrieving lost files after botched mv buggi22 Linux - Newbie 1 09-12-2004 09:25 PM


All times are GMT -5. The time now is 02:38 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration