LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Shell Script & CSV files (https://www.linuxquestions.org/questions/programming-9/shell-script-and-csv-files-723811/)

Inbhir Pheofharain 05-05-2009 04:56 AM

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.

colucix 05-05-2009 05:36 AM

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.

ghostdog74 05-05-2009 05:55 AM

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


theNbomr 05-05-2009 09:39 AM

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 05-05-2009 09:45 AM

Quote:

Originally Posted by theNbomr (Post 3530844)
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


Inbhir Pheofharain 05-05-2009 12:32 PM

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

colucix 05-05-2009 03:11 PM

Quote:

Originally Posted by Inbhir Pheofharain (Post 3531040)
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


Inbhir Pheofharain 05-05-2009 04:47 PM

Colucix, that's absolutely perfect.

Have just tested it and it's perfect.

Thanks very much again. :D

amysaraantony 05-07-2009 05:51 AM

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

Debian

ghostdog74 05-07-2009 07:48 AM

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


ghostdog74 05-07-2009 07:51 AM

Quote:

Originally Posted by amysaraantony (Post 3533162)
If you want a JAVA based application to do this

Java? that's just troublesome.

Inbhir Pheofharain 05-14-2009 03:32 AM

Quote:

Originally Posted by amysaraantony (Post 3533162)
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 :)

schneidz 05-14-2009 09:17 AM

Quote:

Originally Posted by ghostdog74 (Post 3533298)
Java? that's just troublesome.

+ 1

Inbhir Pheofharain 05-14-2009 02:42 PM

Quote:

Originally Posted by schneidz (Post 3540575)
+ 1

Why is Java troublesome? :scratch:

schneidz 05-14-2009 09:43 PM

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.


All times are GMT -5. The time now is 02:32 AM.