0

I am facing a perplexing (at least for me) problem. I'm trying to read some csv files and then extract some values to make some checking and calculus. I'm using awk to access files and extract required fields.

The strange point is that the awk sentence properly runs on a bash prompt but it does not work when run in the script. As an example I get from bash prompt:

paco@NIMBUS:~/work$ awk -F\; '$1 == "21-08-2012" && $2 == "'17'" { print $3 }' niveles-rams.csv
2

but

nrams1=`awk -F\; '$1 == "'$fecha'" && $2 == "'$area'" { print $3 }' niveles-rams.csv`
echo $nrams1

does not return any value. CSV files come from an Excel in windows so maybe there could be a problem with encoding, I guess?

You can find the script validacio.bash and csv files in the following URL

Thanks for your help and patience

3
  • I suggest you re-read the answers to your last question. Commented Oct 26, 2012 at 11:16
  • I'll do, thanks. The point is that same awk sentence is working in the script for a different input file but I tried to save all files as UTF8 to avoid problems coming from the fact that they were produced with Excel. Commented Oct 26, 2012 at 11:26
  • 1
    See cfajohnson.com/shell/cus-faq-2.html#Q24 for how to pass the value of shell variables to an awk script and the answer posted here by @JacekDominiak. Commented Oct 26, 2012 at 12:42

3 Answers 3

2

In your case will be:

nrams1=`awk -F";" -v fecha=$fecha -v area=$area '$1 == fecha && $2 == area { print $3 }' niveles-rams.csv` 

as Kailash K Pawar menitioned the right method

Sign up to request clarification or add additional context in comments.

6 Comments

always quote your shell variables unless you have a very specific reason not to. fecha="$fecha" and area="$area".
Huge difference. Within quotes you get the string that the variable contained. Without quotes you get the result of applying word splitting, filename expansion, etc,. etc. to the string the variable contained. As a trivial example, try this in writeable directory: > tmp1; > tmp2; var="tmp*"; echo "$var"; echo $var
...The output of the first echo will be "tmp*" and the output of the second echo will be "tmp1 tmp2". There are many other potentially unexpected results of not quoting your variable.
See also the comment I just posted at stackoverflow.com/a/13088359/1745001 for a different problem with an unquoted variable.
Amazingly yet another manifestation of the same problem just occurred. See my comment at stackoverflow.com/a/13088026/1745001. I won't post any more examples, just trust me - quote your shell variables.
|
0

In your file (niveles-rams.csv) in date field you using / (slash) symbol but in bash script - (minus) symbol. Maybe that is the reason.

1 Comment

Sorry, uploaded older version. That's not the problem, thanks
0

Thank you to everyone answering and commenting the question. The problem in my script was an awk issue but not the one I asked. My mistake was that I was not properly reading "area" variable in:

i=1
while [ $i -lt 32 ]     
   do
     let i=$i+1
     area=`awk 'BEGIN { FS=";" ; FNR="1" } FNR == "'$i'" {print $1}' pobles.csv`

File pobles.csv has not a header and the script was not properly reading first record. Then errors were cascading and so final result was wrong. Now, adding a header and starting to read from i=2 (second record) it works.

For sure, there has to be a smarter solution but I'm a novice to awk and can't get the point. Hope I will learn more awk and do the trick.

All your answers have been very useful for me and made me learn something about awk.

Thanks

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.