1

I have a long csv file with 5 columns of values. How can I extract every value from a column and pass this value to bc to extract a cosine on it?

I'm trying using awk to extract the values but I fail when I try to pass every single value to bc.

Thank you in advance for your interest.

Roberto

Roberto
  • 23
  • 4
  • What do you mean when you say it fails? Did you get an error? Did you get something different from what you expected? And do give some examples of your input and expected output. – Shawn Chin Nov 28 '11 at 12:41
  • 1
    doesn't your version of awk support the `cos()` function. If not, look for nawk, /usr/xpg4/bin/awk or gawk. You should be able to do any math operation in awk that bc can do. Good luck. – shellter Nov 28 '11 at 12:43
  • sometimes bash is really retarded for this kind of tasks. IMHO, if you are using linux or unix, just do it in Python ... – oz123 Nov 28 '11 at 12:48
  • @Oz123: Only if you don't know how to use it. In my opinion it's pretty silly to use python for such a simple task. – flesk Nov 28 '11 at 21:26
  • @flesk, as you can see from my post below, I also know how to do it BASH, and IMHO it is much more readable than the AWK answer. However, the expressibility and clarity of Python code simply win ! – oz123 Nov 28 '11 at 22:38
  • @Oz123: I see that you've come up with one way to do it. It's not a very bash like solution though, so your argument of the clarity of python isn't valid. A couple of solutions that would be better `cat csvfile.csv | awk -F',' '{print $1 "+" $2}' | bc` or `cat csvfile.csv | sed 's/,/+/;s/,.*//' | bc`. The OP isn't asking for a python solution, and this is a perfectly good example of a good use of an awk and bc solution, so I really don't get the downvote. – flesk Nov 29 '11 at 06:23

3 Answers3

2

You can process a csv with (g)awk to calculate per field cosines, like:

awk -v FS=',' '{for (i=1;i++;i=NF) { printf(" %s\tcos: %s" FS,$i,cos($i)) } ; print }' INPUTFILE

HTH

Zsolt Botykai
  • 50,406
  • 14
  • 85
  • 110
1

If you just want to use awk and bc, like you've asked in your question you could do something like this:

awk '{print "c(" $1 ")"}' file.csv | bc -l

Just make sure you pipe a string containing e.g. "c(1)" to bc -l.

flesk
  • 7,439
  • 4
  • 24
  • 33
0

Ha, Just a few minutes late, and although your question is lacking info, and despite what I wrote you should do it in python, I decide that for the challenge I'll do it in BASH (and NOT AWK).

oz123@debian:~$ cat csvfile.csv 
1,2,3
4,5,6
7,8,9
oz123@debian:~$ cat csvfile.csv | ( while 
> read line; 
> do  
> VAR1=`echo $line | cut -d "," -f1` ;
> VAR2=`echo $line | cut -d "," -f2` ;
> echo $VAR1+$VAR2 
> echo $VAR1+$VAR2 | bc
> 
> done; 
> 
> )
1+2
3
4+5
9
7+8
15
oz123@debian:~$ 

OK, My first solution is indeed UGLY, and computationally expansive. Here is a nicer solution:

oldifs=$IFS; export IFS=","; cat csvfile.csv | while read -a line; do echo ${line[2]}+${line[1]} | bc; done

the -a flag turns the lines into arrays. The IFS is The Internal Field Separator (see man bash).

oz123
  • 27,559
  • 27
  • 125
  • 187
  • 1
    Like I said above, why would his problem require the use of a scripting language of YOUR choice, when it's really a very simple task? It's not like he's asking for help to use awk and bc to create a chat bot that also fetches live football scores from the internet while generating random haikus. For that I agree that python would be a good choice among many other equally good scripting languages. – flesk Nov 29 '11 at 06:28
  • @flesk, well it might be silly to use Python. But I do consider readability important. AWK is not readable as BASH (IMHO). So, I supplied here a computationally cheaper solution which is also easier to read than the whole AWK or SED solutions. – oz123 Dec 19 '11 at 14:41