LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 05-05-2009, 05:56 AM   #1
Inbhir Pheofharain
LQ Newbie
 
Registered: May 2009
Location: Edinburgh, Scotland
Distribution: Ubuntu
Posts: 15

Rep: Reputation: 0
Talking Shell Script & CSV files


Hello,

Can anyone help me with a problem I have?

Here is a sample from my CSV file:

Code:
USR1,Linux,Steven1,Ubuntu
USR2,Linux,George3,Fedora
USR3,BSD,Alan3,FreeBSD
USR4,Windows,Kate4,WinXP
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 05:57 AM. Reason: Forgot user name Kate4 on code example
 
Old 05-05-2009, 06:36 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Using awk:
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
 }' file.csv
or you can use the switch statement in awk, if enabled.
 
Old 05-05-2009, 06:55 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
here's a Python script, if you can use Python on your system
Code:
#!/usr/bin/env python
import csv
filename = "file"
reader = csv.reader(open(filename))
user=raw_input("Enter user: ")
for row in reader:    
    if row[0].lower() == user.lower():
        print "%s user name: %s" %(row[1],row[2])
        print "Distro: ",row[-1]
output:
Code:
# ./test.py
Enter user: usr1
Linux user name: Steven1
Distro:  Ubuntu

# ./test.py
Enter user: usr2
Linux user name: George3
Distro:  Fedora
 
Old 05-05-2009, 10:39 AM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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.
 
Old 05-05-2009, 10:45 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by theNbomr View Post
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
 
Old 05-05-2009, 01:32 PM   #6
Inbhir Pheofharain
LQ Newbie
 
Registered: May 2009
Location: Edinburgh, Scotland
Distribution: Ubuntu
Posts: 15

Original Poster
Rep: Reputation: 0
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 01:48 PM. Reason: Added tags
 
Old 05-05-2009, 04:11 PM   #7
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by Inbhir Pheofharain View Post
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
 
Old 05-05-2009, 05:47 PM   #8
Inbhir Pheofharain
LQ Newbie
 
Registered: May 2009
Location: Edinburgh, Scotland
Distribution: Ubuntu
Posts: 15

Original Poster
Rep: Reputation: 0
Colucix, that's absolutely perfect.

Have just tested it and it's perfect.

Thanks very much again.
 
Old 05-07-2009, 06:51 AM   #9
amysaraantony
Member
 
Registered: Apr 2009
Posts: 42

Rep: Reputation: 16
If you want a JAVA based application to do this - let me know and I'll mail it across to you.

Debian

Last edited by amysaraantony; 05-15-2009 at 09:16 PM.
 
Old 05-07-2009, 08:48 AM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
well, here's another implementation, entirely in awk
Code:
awk 'BEGIN{
 FS=","
 printf "Enter user:"
 getline user < "-" 
}
$1 ~ user{
 print $2" user name: " $3
 print "Distro: " $NF
}$1 !~ user {print "Error"; exit}' file
 
Old 05-07-2009, 08:51 AM   #11
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by amysaraantony View Post
If you want a JAVA based application to do this
Java? that's just troublesome.
 
Old 05-14-2009, 04:32 AM   #12
Inbhir Pheofharain
LQ Newbie
 
Registered: May 2009
Location: Edinburgh, Scotland
Distribution: Ubuntu
Posts: 15

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by amysaraantony View Post
If you want a JAVA based application to do this - let me know and I'll mail it across to you.
That would be superb! Could you do that please?

Thanks very much
 
Old 05-14-2009, 10:17 AM   #13
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,217

Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
Quote:
Originally Posted by ghostdog74 View Post
Java? that's just troublesome.
+ 1
 
Old 05-14-2009, 03:42 PM   #14
Inbhir Pheofharain
LQ Newbie
 
Registered: May 2009
Location: Edinburgh, Scotland
Distribution: Ubuntu
Posts: 15

Original Poster
Rep: Reputation: 0
Lightbulb

Quote:
Originally Posted by schneidz View Post
+ 1
Why is Java troublesome?
 
Old 05-14-2009, 10:43 PM   #15
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,217

Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
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.
 
  


Reply

Tags
awk, csv, file, script, shell


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script for CSV file comparision aravind_balan Programming 1 02-17-2009 04:33 AM
Calculate average from csv file in shell script khairilthegreat Linux - Newbie 5 11-21-2007 01:57 PM
Filtering a CSV file from web log with shell script? Micro420 Programming 8 08-22-2007 04:13 AM
shell script to read input from csv ip addresses? kr0m3 Programming 3 07-21-2007 09:51 AM
Shell script to read from csv file hendemeg Programming 1 05-11-2004 09:23 PM


All times are GMT -5. The time now is 05:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration