Setting a BASH environment variable directly in AWK (in an AWK one-liner)?

You just do everything in awk awk '{cmd="c_program "$1; cmd|getline l;print $1,$2-l}' file.

Very elegant; this is exactly what I was looking for. Thanks! – Elliott Jun 25 '10 at 16:37.

Pure Bash, without using any external executables other than your program: #! /bin/bash while read num1 num2 do (( result = $(c_program num2) - num1 )) echo "$num1 $result" done.

Ah, I didn't know about this nice bash feature. Thanks! – Elliott Jun 25 '10 at 16:32 there's also the issue of floating pt calculations using bash, although OP has not stated that he might have floats(decimals) – ghostdog74 Jun 26 '10 at 0:28.

This shows how to execute a command in awk: ls | awk '/^a/ {system("ls -ld " $1)}' You could use a bash script instead: while read line do FIRST=`echo $line | cut -d' ' -f1` SECOND=`echo $line | cut -d' ' -f2` OUT=`expr $SECOND \* 4` echo $FIRST $OUT `expr $OUT - $SECOND` done.

Expr doesn't support floating values, use bc , dc or awk – ghostdog74 Jun 24 '10 at 7:21.

The shell is a better tool for this using a little used feature. There is a shell variable IFS which is the Input Field Separator that sh uses to split command lines when parsing; it defaults to which is why ls foo is interpreted as two words. When set is given arguments not beginning with - it sets the positional parameters of the shell to the contents of the arguments as split via IFS, thus: #!

/bin/sh while read line ; do set $line subtrahend=`c_program $1` echo $1 `expr $2 - $subtrahend` done.

Expr doesn't support floating values, use bc , dc or awk – ghostdog74 Jun 24 '10 at 7:20.

As others have pointed out: awk is not not well equipped for this job. Here is a suggestion in bash: #! /bin/sh data_file=$1 while read column_1 column_2 the_rest do ((result=$(c_program $column_1)-$column_2)) echo $column_1 $result "$the_rest" done Sh, then invoke it as: sh myscript.

Sh data_file The read command reads each line from the data file (which was redirected to the standard input) and assign the first 2 columns to $column_1 and $column_2 variables. The rest of the line, if there is any, is stored in $the_rest. Next, I calculate the result based on your requirements and prints out the line based on your requirements.

Note that I surround $the_rest with quotes to reserve spacing. Failure to do so will result in multiple spaces in the input file to be squeezed into one.

Hai, "awk is not not well equipped for this job. ". That's nonsense.

You should also take note of floating point calculations. Bash fails on that but awk doesn't. – ghostdog74 Jun 26 '10 at 0:24 @ghostdog74: I stand corrected.

– Hai Vu Jun 26 '10 at 6:43.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions