The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

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 12:37 AM
Change names in a column based on the symbols in another column repinementer Shell Programming and Scripting 8 08-14-2009 03:30 AM
column to rows based on another column... malcomex999 Shell Programming and Scripting 5 07-08-2009 06:28 AM
Changing one column of delimited file column to fixed width column manneni prakash Shell Programming and Scripting 5 06-22-2009 05: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

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 5 Days Ago
JohnGG JohnGG is offline
Registered User
  
 

Join Date: Nov 2009
Posts: 2
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 (permalink)  
Old 5 Days Ago
steadyonabix steadyonabix is offline
Registered User
  
 

Join Date: Oct 2009
Location: UK
Posts: 154
What have you tried so far?
  #3 (permalink)  
Old 5 Days Ago
TonyLawrence TonyLawrence is offline
Registered User
  
 

Join Date: Sep 2007
Location: SE Mass
Posts: 133
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 (permalink)  
Old 1 Day Ago
JohnGG JohnGG is offline
Registered User
  
 

Join Date: Nov 2009
Posts: 2
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; 19 Hours Ago at 06:23 AM.. Reason: Please indent your code and use code tags!
Sponsored Links
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:11 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0