UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 24th, 2006, 06:34 AM
ejazzy ejazzy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Posts: 20 ejazzy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 55 m 44 sec
Reputation Power: 0
Search for a value and print underlying column

Hi All

Firstly, I'm not sure if awk is the best tool for what I am about to do but as its served me well initially, I'm hoping it can still do the trick. Below is a sample of my simple awk command that is currently in use.

awk -F, '{print $1","$4 > "CHF_XS"}' Ribit.csv

At the time, CHF, my search criteria was always located in the $4 position so there were no problems. Unfortunately, this has changed. CHF can now be in any position in the header line.

Consequently, I'm looking for a way in awk, preferably, whereby I can do a search for CHF, or any other currency, and only print out the underlying column for that currency. For instance

Date,USD,CHF,JPY
2006/24/01,9.8,7.6,5.4
2005/25/02,3.2,1.9,8.7

will return the following results

$searchCriteria = CHF

Date,CHF
2006/24/01,7.6
2005/25/02,1.9

$searchCriteria = USD

Date,USD
2006/24/01,9.8
2005/25/02,3.2

I get the feeling awk var= is a good place to start but I'm just not sure how to find which column USD is in and then passing that as the column to print.

Any help as always would be appreciated.

Reply With Quote
  #2  
Old January 24th, 2006, 10:24 AM
ejazzy ejazzy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Posts: 20 ejazzy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 55 m 44 sec
Reputation Power: 0
Hi All

For those interested, the solution is as follows

I created a file called column.awk with the following data

NR == 1 {
for ( i = 1; i <= NF; i++ )
if ( $i == var )
col = i
}
{ print $1","$col }

In a seperate script I call this by setting an environment variable searchCriteria and passing this in the awk command as follows

awk -v var="$searchCriteria" -F "," -f column.awk Ribit.csv > fx_$searchCriteria

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Search for a value and print underlying column


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT