|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
|
#2
|
|||
|
|||
|
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 |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > Search for a value and print underlying column |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|