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 so for example if USR1 was inputted I want the following output: Code:
Linux User Name: Steven1 Code:
Windows User Name: Kate4 |
Using awk:
Code:
#!/bin/bash |
here's a Python script, if you can use Python on your system
Code:
#!/usr/bin/env python Code:
# ./test.py |
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. |
Quote:
Code:
# more file |
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 |
Quote:
Code:
#!/bin/bash |
Colucix, that's absolutely perfect.
Have just tested it and it's perfect. Thanks very much again. :D |
If you want a JAVA based application to do this - let me know and I'll mail it across to you.
Debian |
well, here's another implementation, entirely in awk
Code:
awk 'BEGIN{ |
Quote:
|
Quote:
Thanks very much :) |
Quote:
|
Quote:
|
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. |