Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
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.
The die-hard Linux way to accomplish this would be to brute-force write your own script to parse out the file.
The easy way would be to load the file into some GUI program that is CSV aware, like OpenOffice Calc, delete the columns you don't want, then resave the file. This kludge would probably be good enough if you only need to do this operation once, or very infrequently.
The die-hard Linux way to accomplish this would be to brute-force write your own script to parse out the file.
The easy way would be to load the file into some GUI program that is CSV aware, like OpenOffice Calc, delete the columns you don't want, then resave the file. This kludge would probably be good enough if you only need to do this operation once, or very infrequently.
Yeah. I did this for this time. But in future, i am really trying to get some shell script to do the work for me, and to automate the process...
Awk/gawk can be considered to be a programming language in its own right. If you have to do string manipulation and text data file parsing, you must have this in your toolkit. When combined with the power of the scripting language and some other handy-dandy CLI utilities in every Linux distro (sed comes to mind), there is no text data file you can't slice and dice any way you want to. I could try to drop an example or two into this forum, but there's no way I'd do as good a job as the gazillion and one online tutorials already available.
These can look pretty daunting, but for what you're trying to do you really won't have to dive too deep into the tutorial. All you're telling awk to do is:
1) Treat commas as field delimiters
2) Fields are encapsulated by quotes, ignore commas, quotes, and single quotes within a field
3) Open file {x}
4) Extract fields 13 and 99, store in variable {y}
5) Write {y} to file {z}
These can look pretty daunting, but for what you're trying to do you really won't have to dive too deep into the tutorial. All you're telling awk to do is:
1) Treat commas as field delimiters
2) Fields are encapsulated by quotes, ignore commas, quotes, and single quotes within a field
3) Open file {x}
4) Extract fields 13 and 99, store in variable {y}
5) Write {y} to file {z}
Is that about right?
Yes. That's exactly it.
I took a look at the tutorials, and they all seem awfully complicated to me. I'll do my best at trying to archieve what i am trying to do, but if there is somebody out there, who knows how to do it, could you please take the time to lead me onto the right tracks? Please.
Wow!! Cool Steve! I never knew perl could do this much (and it doesn't even take much code to do it)
It would be really great, if you could actually show me how to select certain fields. I really appreciate it.
I am just wondering: what is the best way to learn perl (if you already have knowlege about the loop-structures and other stuff of that level)?
I've started doing some c++, but i never really got all to far. I am now starting java, and i am thinking that perl would be a great file and os-oriented language.
I have always found Shell Scripts and Batch files very helpful, as i like doing stuff that has to do with servers and other command line oriented projects. Perl seems to be a great asset that i would really like to add to my "toolbox".
I've added another line that shows each field individually as an element from the array. The Text::ParseWords modules is part of most standard perl installations - it just populates an array based on each delimited line given to it. In this case, the lines are retrieved from the CSV file identified by the command line argument (which is shifted into $target).
I've used it a fair bit (under Unix and Windows) for my last few employers so I bought the Learning perl book from O'Reilly. I don't know what the current edition is like, but the 2nd edition was so good I got Programming Perl and The Perl Cookbook as well.
The perl man pages are very good, but they take a bit of time... Have a look at man perl, man perlfaq4, etc.
That Perl stuff is great. It reminds me of a mix of the TI graphing calculator syntaxes (yes, odd - but that's the first thing that came to mind, not sure why) compined with c and normal shell / bat scripting.
I've got the O'Reilly Linux in a Nutshell. It's a good book, but i wouldn't suggest it for a noob to linux (it got pretty confusing in the beginning, as i came from a windows environment). It's mainly a command line reference; -- Is the perl one similar?
With this script of yours, i'll have something to start experimenting with and something to add stuff to and to take apart, in addition to the fact that it simply works great for the job it was intended to do.
Steve, you are great!
Can all perl script be run on windows and on linux, without having to change anything except the first line?
I'm glad that helped And yes, the Learning Perl reference was a good one - plenty of examples and explanations, I do recommend it.
And yes again, a lot scripts can be run without change on both platforms. I tend to put platform specific stuff in properties files and just use whichever one I'm running it against. For example, scripts working with files may be the same once you have the file open and processing, but path syntax may be different (drives instead of mount points, etc).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.