Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 18th, 2006, 12:58 AM   #1
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
Small Database Work

Well, as you probably know I started Unix scripting today. Most of the problem I've ran into today were solved with the 'man command' statement (for the manual). However, now I ran into a problem and I have no idea what command to use. If you guys could point me on what command to use that would be great then I'll look up the rest. I found this useful database to practice with and here's how it goes (in short):

// file1.fd
Quebec:5:QC
Ontario:6:ON
Manitoba:7:MB
Saskatchewan:8:SK

//file2.fd
Quebec City:7560592:1542056:July 1, 1867:5
Toronto:12439755:1076395:July 1, 1867:6
Winnipeg:1170300:647797:July 15, 1870:7
Regina:996194:651036:September 1, 1905:8

I'll show you an example with the first entry so you can see what I'm doing.

First off, both files are sorted based on their ID's (5-8 is what I've shown above). So that would mean 'Toronto' (in file2) would be on the same line number as Ontario (in file1). If you believe me that it's sorted according to the ID, please skim down the page to the '->' to save your time

to acomplish this I used (NOTE: the '-k2' is what section is being sorted 'in this case the ID's):

sort -n -t: -k2 file1.ca > SortedFile1.tmp
sort -n -t: -k5 file2.ca > SortedFile2.tmp

Please don't worry about the ID's because it really doesn't mean anything. All it does is help me match the provinces with the capitals (for later use).

->
Now, that it's sorted, what I'm actually trying to do is take for instance, the first column of file2 'Toronto for instance' then add the corresonding Province Abbreviation (from file1). With ', ' (a comma followed by a space) as opposed to keeping the deliminer ':' (or seperator whatever you want to call it).

So I get:

'Toronto, ON' and so fourth.

Before you go on about looking at the man pages, I found the cut and paste commands. Yes that would normally work by doing:

cut -d: -f1 file1.fd > temp1.fd
cut -d: -f3 file2.fd > temp2.fd
paste -d: temp1.fd temp2.fd > FINAL.fd

this would create:

Quebec City: QC
Toronto: ON
Winnipeg: MB
Regina: SK

But as I said earlier I want to be able to add a string in between the deliminer.

So I can do

Quote:
Quebec City, Qc
..
..
Regina, SK

Any ideas on what command I should be using to do this?

Can the cut and paste command suffice?
__________________
Death smiles at us all. All a man can do is smile back.
Eric the Red is offline   Reply With Quote
Old Nov 18th, 2006, 2:56 AM   #2
free-zombie
Programmer
 
free-zombie's Avatar
 
Join Date: May 2006
Location: Bavaria, Germany
Posts: 50
Rep Power: 0 free-zombie is an unknown quantity at this point
Send a message via ICQ to free-zombie Send a message via MSN to free-zombie Send a message via Yahoo to free-zombie
seams that you simply want to replace the ':' with a ','. This is best accomplished with sed (stream editor, has powerful regex capabilities...), so you make that last line
paste -d: temp1.fd temp2.fd | sed 's/:/,/' > FINAL.fd
's/:/,/' means Substiture : with , (max. once on every line)
free-zombie is offline   Reply With Quote
Old Nov 18th, 2006, 11:00 AM   #3
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
Thanks for you help. One more quick question

If I were to use that sed command to only edit let's say the second or third (Edit**) delimiter ':'. How would I do that?

The online manual doesn't say too much about that:

Sed Addresses

     An address is not required, but if specified must be a number (that
     counts input lines cumulatively across input files)

So I tried doing:

sed 2's/:/,/' file1 file2 > tempit.ca

That means the second line. However, I need the second field.
__________________
Death smiles at us all. All a man can do is smile back.

Last edited by Eric the Red; Nov 18th, 2006 at 11:39 AM.
Eric the Red is offline   Reply With Quote
Old Nov 18th, 2006, 1:38 PM   #4
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 855
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
I can't help you as I don't know bash/shell scripting. However, I found this Shell Scripting Primer in Apple's developer pages. It looks like an excellent resource and if I have time I plan to read through it myself.
titaniumdecoy is offline   Reply With Quote
Old Nov 18th, 2006, 4:20 PM   #5
a thing
Unverified User
 
a thing's Avatar
 
Join Date: Aug 2005
Location: none
Posts: 146
Rep Power: 0 a thing is on a distinguished road
I think what you want is awk. http://en.wikibooks.org/wiki/Programming:AWK
__________________
Warning: My posts may change (dramatically) within the first 15 minutes they're posted.
Got 'Nux?—GNU/Linux and other free software support.
It's GNU/Linux, not just Linux.
a thing is offline   Reply With Quote
Old Nov 18th, 2006, 10:46 PM   #6
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
Thanks titaniumdecoy, great stuff!

Yes, and thanks a thing I'll look into awk.
__________________
Death smiles at us all. All a man can do is smile back.
Eric the Red is offline   Reply With Quote
Old Nov 18th, 2006, 11:09 PM   #7
a thing
Unverified User
 
a thing's Avatar
 
Join Date: Aug 2005
Location: none
Posts: 146
Rep Power: 0 a thing is on a distinguished road
Oh yeah, you might wanna look at http://tldp.org/LDP/abs/html/index.html
__________________
Warning: My posts may change (dramatically) within the first 15 minutes they're posted.
Got 'Nux?—GNU/Linux and other free software support.
It's GNU/Linux, not just Linux.
a thing is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
XML DataBase Help (Won't work in IE :() brownhead JavaScript and Client-Side Browser Scripting 1 Aug 27th, 2006 1:44 PM
searching an access database? designXperts.net Visual Basic .NET 12 Nov 27th, 2005 5:03 PM
how do i automate text data and input into database? lionel84 C# 0 Jun 26th, 2005 9:35 PM
Java - Searching a table in a database - small problem. Vengeance Java 4 Apr 28th, 2005 2:53 PM
Using ODBC to connect to a remote database in a C program bigi C++ 1 Mar 8th, 2005 3:19 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:50 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC