LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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, 04: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 04:57 AM. Reason: Forgot user name Kate4 on code example
 
Old 05-05-2009, 05:36 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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, 05:55 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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, 09:39 AM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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, 09:45 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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, 12: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 12:48 PM. Reason: Added tags
 
Old 05-05-2009, 03:11 PM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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, 04: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, 05: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 08:16 PM.
 
Old 05-07-2009, 07:48 AM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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, 07:51 AM   #11
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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, 03: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, 09:17 AM   #13
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
Quote:
Originally Posted by ghostdog74 View Post
Java? that's just troublesome.
+ 1
 
Old 05-14-2009, 02: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, 09:43 PM   #15
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
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



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 03:33 AM
Calculate average from csv file in shell script khairilthegreat Linux - Newbie 5 11-21-2007 12:57 PM
Filtering a CSV file from web log with shell script? Micro420 Programming 8 08-22-2007 03:13 AM
shell script to read input from csv ip addresses? kr0m3 Programming 3 07-21-2007 08:51 AM
Shell script to read from csv file hendemeg Programming 1 05-11-2004 08:23 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:29 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
Open Source Consulting | Domain Registration