Read CSV column value based on column name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read CSV column value based on column name
# 1  
Old 11-15-2009
Read CSV column value based on column name

Hi All,

I am newbie to Unix I ve got assignment to work in unix

can you please help me in this regard

There is a sample CSV file
"Username", "Password"
"John1", "Scot1"
"John2", "Scot2"
"John3", "Scot3"
"John4", "Scot4"

If i give the column name as Password and row number as 4 the result I should get '

'Scot3'

can you please help me in writing shell script for the above

Thanks
John
# 2  
Old 11-15-2009
What have you tried so far?
# 3  
Old 11-15-2009
I'm assuming this is homework?

First, do you understand how to pass arguments to your script? Do you know what $1, $2 are inside your script?

Next, you need to know how to extract a specific line by number. I realize that the man pages of "sed" are confusing, so to get you going, try
Code:
sed -n '4p'

Finally, you need to know how to extract the specific column you want, How will you do that? There are at least two general ways to approach that:

You could build a table that matches column names to a number. Your script would have something like

Code:
if [ $1 = "Username" ]
then
Column=1
elif  [ $1 = "Password" ]
then
Column=2
fi
echo $Column

More flexible would be if your script looked up the desired index by examining the first line itself. A crude method:

Code:
read line
Wanted=$1
set $line
if [ $Wanted = $1 ]
then
Column=1
...

There are more clever ways to do that - Google for "Awk Associative Arrays" and see if that gives you any ideas in this vein.

I hope this gets you started. Let's see you write some code and we can help you over the rough spots.
# 4  
Old 11-20-2009
Thank Tony ,

I have tried the following code giving error , please do the needful
John.csv is the file name

If i give the column name as Password and row number as 4 the result I should get '

'Scot3'



Code:
cat John.csv if [ $1 = "Username" ]
then
  Column=1
elif  [ $1 = "Password" ]
then
  Column=2
fi
echo $Column
read line
Wanted=$1
set $line
if [ $Wanted = $1 ]
then
  Column=1
else 
  echo "Hello"
fi


Thanks and Regards,
John

Last edited by Franklin52; 11-20-2009 at 06:23 AM.. Reason: Please indent your code and use code tags!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Filtering records of a csv file based on a value of a column

Hi, I tried filtering the records in a csv file using "awk" command listed below. awk -F"~" '$4 ~ /Active/{print }' inputfile > outputfile The output always has all the entries. The same command worked for different users from one of the forum links. content of file I was... (3 Replies)
Discussion started by: sunilmudikonda
3 Replies

2. Shell Programming and Scripting

Get maximum per column from CSV file, based on date column

Hello everyone, I am using ksh on Solaris 10 and I'm gathering data in a CSV file that looks like this: 20170628-23:25:01,1,0,0,1,1,1,1,55,55,1 20170628-23:30:01,1,0,0,1,1,1,1,56,56,1 20170628-23:35:00,1,0,0,1,1,2,1,57,57,2 20170628-23:40:00,1,0,0,1,1,1,1,58,58,2... (6 Replies)
Discussion started by: ejianu
6 Replies

3. Shell Programming and Scripting

Compare two csv's with column based

Hi, I am having below two CSV's col_1,col_2,col_3 1,2,4 1,3,6 col_1,col_3,col2,col_5,col_6 1,2,3,4,5 1,6,3,,, I need to compare based on the columns where the mismatch is expected output col_1,col_2,col_3 1,2,4 (3 Replies)
Discussion started by: rohit_shinez
3 Replies

4. Shell Programming and Scripting

Fetching values in CSV file based on column name

input.csv: Field1,Field2,Field3,Field4,Field4 abc ,123 ,xyz ,000 ,pqr mno ,123 ,dfr ,111 ,bbb output: Field2,Field4 123 ,000 123 ,111 how to fetch the values of Field4 where Field2='123' I don't want to fetch the values based on column position. Instead want to... (10 Replies)
Discussion started by: bharathbangalor
10 Replies

5. Linux

Filter a .CSV file based on the 5th column values

I have a .CSV file with the below format: "column 1","column 2","column 3","column 4","column 5","column 6","column 7","column 8","column 9","column 10 "12310","42324564756","a simple string with a , comma","string with or, without commas","string 1","USD","12","70%","08/01/2013",""... (2 Replies)
Discussion started by: dhruuv369
2 Replies

6. Shell Programming and Scripting

Pick the column value based on another column from .csv file

My scenario is that I need to pick value from third column based on fourth column value, if fourth column value is 1 then first value of third column.Third column (2|3|4|6|1) values are cancatenated. Main imp point, in my .csv file, third column is having price value with comma (1,20,300), it has... (2 Replies)
Discussion started by: Ganesh L
2 Replies

7. Shell Programming and Scripting

Read in 2-column CSV, output many files based on field

Is there a way to read in a two-columned CSV file, and based on the fields in 1st column, output many different files? The input/output looks something like: input.csv: call Call Mom. call Call T-Mobile. go Go home. go Go to school. go Go to gas station. play Play music. play Play... (4 Replies)
Discussion started by: pxalpine
4 Replies

8. Shell Programming and Scripting

awk read column csv and search in other csv

hi, someone to know how can i read a specific column of csv file and search the value in other csv columns if exist the value in the second csv copy entire row with all field in a new csv file. i suppose that its possible using awk but i m not expertise thanks in advance (8 Replies)
Discussion started by: giankan
8 Replies

9. Shell Programming and Scripting

extract csv based on column value

Hi I have a csv file which is below A,5 B,6 C,10 D,7 I want the values who's second column is greater than 7 say C,10 D,7 Help me please... Thanks, Maruth (3 Replies)
Discussion started by: maruthavanan
3 Replies

10. Shell Programming and Scripting

sorting csv file based on column selected

Hi all, in my csv file it'll look like this, and of course it may have more columns US to UK;abc-hq-jcl;multimedia UK to CN;def-ny-jkl;standard DE to DM;abc-ab-klm;critical FD to YM;la-yr-tym;standard HY to MC;la-yr-ytm;multimedia GT to KJ;def-ny-jrt;critical I would like to group... (4 Replies)
Discussion started by: tententen
4 Replies
Login or Register to Ask a Question