ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
My goal is for the script to accept user input (which will be from column 1) and the script to check column 2 to see what OS type it is and then the script echoes headings dependant on what was found in column 2 followed by the data in columns 3 and 4.
so for example if USR1 was inputted I want the following output:
Code:
Linux User Name: Steven1
Distro: Ubuntu
but if USR4 was inputted I want:
Code:
Windows User Name: Kate4
Windows Version: WinXP
I'm guessing awk or grep must be used somewhere, am I right - I've googled but I just can't seem to find exactly what I need.
Last edited by Inbhir Pheofharain; 05-05-2009 at 04:57 AM.
Reason: Forgot user name Kate4 on code example
ghostdog74's implementation is probably superior, in that one presumes that the Python module (csv) that he specifies would know how to deal with commas that can be embedded in the fields of a CSV file. If you can guarantee that your CSV file does not contain any such condition, then the solution given by colucix should suffice. For what it's worth, there is a comparable Perl module for parsing CSV files, should you prefer Perl as your implementation language.
--- rod.
ghostdog74's implementation is probably superior, in that one presumes that the Python module (csv) that he specifies would know how to deal with commas that can be embedded in the fields of a CSV file.
that's why i use csv module to handle such cases of embedded commas. eg
Code:
# more file
USR1,Linux,"Steven1,one",Ubuntu
USR2,Linux,"George3,two",Fedora
USR3,BSD,"Alan3,three",FreeBSD
USR4,Windows,"Kate4,four",WinXP
# ./test.py
Enter user: usr1
Linux user name: Steven1,one
Distro: Ubuntu
Thanks very much for your help guys, I really do appreciate it.
I must admit I'm not at all familiar with Python or Perl, so I'll give it a shot.
There are no embedded commas in my CSV [and nor will there be].
Just one other thing (sorry should have mentioned it in the beginning) If using the awk method how do I return an error message if no match was found on column 1.
Many thanks again
Last edited by Inbhir Pheofharain; 05-05-2009 at 12:48 PM.
Reason: Added tags
Just one other thing (sorry should have mentioned it in the beginning) If using the awk method how do I return an error message if no match was found on column 1.
Just assign 1 to a variable, then test the value of the variable in the END section of the awk program. Take in mind that a variable not assigned has a value of 0 that is "false":
Code:
#!/bin/bash
read -p "Enter user: " user
awk -F, -v user=$user '
$1 == user {
print $2,"User Name:",$3
if ( $2 == "Linux" ) print "Distro:",$4
if ( $2 == "Windows" ) print "Windows Version:",$4
if ( $2 == "BSD" ) print "BSD Version:",$4
found = 1
}
END{ if ( ! found )
print "error: user", user, "not found"
}' file.csv
maybe troublesome is too strong but i think the awk solution is both faster in implimentation and execution.
when i graduated in ece/ cs, java was kinda' slow (its more resource intensive considering jvm is an emulation layer) i know it is much faster now due to tighter coding in jvm's as well as better processors.
i usually relegate java to gui swing stuff and c to text data manipulation but that's just my personal tastes.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.