![]() |
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//file2.fd :
Quebec City:7560592:1542056:July 1, 1867:5I'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.tmpPlease 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.fdthis would create: :
Quebec City: QCBut as I said earlier I want to be able to add a string in between the deliminer. So I can do Quote:
Any ideas on what command I should be using to do this? Can the cut and paste command suffice? |
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 |
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 AddressesSo I tried doing: sed 2's/:/,/' file1 file2 > tempit.ca That means the second line. However, I need the second field. |
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.
|
I think what you want is awk. http://en.wikibooks.org/wiki/Programming:AWK
|
Thanks titaniumdecoy, great stuff!
Yes, and thanks a thing I'll look into awk. |
Oh yeah, you might wanna look at http://tldp.org/LDP/abs/html/index.html
|
| All times are GMT -5. The time now is 2:33 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC