Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
I'm stuck with separating column using SED. Here are two entries of my CSV file and I need help in separating "Organization Location" column into two columns "Organization City" and "Organization State".
Can someone please help me?
Year Rank Organization ID Organization Name Organization Location Private Income
2004 1 321092 Salvation Army Alexandria, Va. $1,324,089,000
2004 2 321148 American Cancer Society Alexandria, Va. $794,000,000
sed may not be the best language for the task and maybe not AWK either. But if the file has tabs between the fields, you might get by with either. If not, then perl5 with Text::CSV would be the way to parse it.
So, two questions: are their tabs between the fields and can you show us what you have so far written in sed?
Doesn't much look like a CSV - but that dollar field might make a mess of things. Let's see some real data - anonymised would be a good idea.
Agree sed is the wrong tool, but awk should be usable if you know it.
why do you need sed? is this a homework?
In your example state is missing in line 3. How do you handle that case?
Is this really a CSV (comma separated values) file?
Can you show us the data in the file directly, as with cat or less, rather than as it's shown in a spreadsheet program?
The spreadsheet program may be formatting data to display it by adding dollar signs and commas to the numeric data, etc.
And please explain why sed is a requirement. Is this homework?
[simulpost]
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524
Rep:
It sounds like homework. What have you tried so far? You would need to nest awk statements in sed. But if you can't use awk, that won't work. If you are able to use more than one iteration sed might be feasible.
This is an output of cat abc.csv file command. This is actually a homework given by my teacher and he specifically mentioned to split the column using SED.
Year,Rank,Organization ID,Organization Name,Organization Location,Private Income,Total Assets,Service Expense,Fundraising Expense
2004,1,321092,Salvation Army,"Alexandria, Va.","$1,324,089,000","$3,039,922,000","$2,126,200,000","$123,791,000"
2004,2,321148,American Cancer Society,Atlanta,"$794,000,000","$836,295,000","$610,639,000","$188,150,000"
2004,3,321036,Gifts In Kind International,"Alexandria, Va.","$787,192,199","$790,561,819","$792,432,766","$167,242"
it is not trivial, because commas in that file are not always separators, but part of the string (like "$123,791,000").
I would try something like:
1. replace comma within " " with : or ; or something else - but probably not required
2. find the 5th field
3. split it
4. restore the original comma
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524
Rep:
Hi amy441,
The reason your instructor told you to use sed is because he wants you to learn the sed command. It was not because he wanted you to learn only how to do the assignment. Would you like to go to a doctor who only knows the examples given in class? Or would you rather go to a doctor who knows the practice of medicine.
If we simply tell you the answer, the whole purpose of school is lost. You won't learn anything. So, read the sed man page. You will probably need to use more than one command line, so you can write a script.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.