![]() |
|
|
grep unix.com with google
|
|||||||
| Forums | Register | Blog | Man Pages | Forum Rules | Links | Albums | FAQ | Our Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|||
|
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 |
|
|||
|
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. |
|
|||
|
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! |
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| name the column based on symbols in another column | repinementer | Shell Programming and Scripting | 7 | 08-27-2009 01:37 AM |
| Change names in a column based on the symbols in another column | repinementer | Shell Programming and Scripting | 8 | 08-14-2009 04:30 AM |
| column to rows based on another column... | malcomex999 | Shell Programming and Scripting | 5 | 07-08-2009 07:28 AM |
| Changing one column of delimited file column to fixed width column | manneni prakash | Shell Programming and Scripting | 5 | 06-22-2009 06:27 AM |
| how to read the column and print the values under that column | gemini106 | Shell Programming and Scripting | 6 | 03-28-2008 07:05 AM |