Translate a array from bash to awk
Hi
I've got a script in bash. Unfortunately I've to multiplicity all entries in array (witch have 4000 entries) with a number <0. bash dosen't that. Consequently I want translate the bash array into awk. Maybe it would be better to use awk in the hole script, but I have no experience in awk. Could anyone help? Thanks in advance :-) Sebastian |
looks like something like this works:
Code:
B=( echo ${A[@]} | awk ' BEGIN { RS=" " } { printf -0.25*$0 " " } ' ) |
There are plenty of people here who I'm sure have great expertise with both BASH and AWK and could do it I'm sure.
You're asking for a handout. Instead post what you have in BASH and at least try to start with AWK. Personally I'd suggest using a program. I don't see AWK to be a scripting language, but instead a command. My other suggestion is that since you've leaned that AWK supports the capability to multiply negative numbers, then why not stage one calculation in AWK as your first try and then look to expand from that. I'm sure reviewers would be very pleased to see something like:
|
Sorry. Here's my bash-script:
Code:
#!/bin/bash Code:
Variable=$(awk "BEGIN{print 10 ** -7}") I would be obliged if someone can help me. Sebastian |
I gave you a tip to go thru the array with a single awk. That will be much quicker.
you ought to use bc instead of awk: Code:
Solutiontime=$(echo "3.17 * $Variable" | bc ) |
Sorry for my "low skill questions", but I use bash since 10 am.
Only to clarify what you say: You advice me to use Code:
Variable=$(awk "BEGIN{print 10 ** -7}") Code:
Variable=$(awk "BEGIN{print 10 ** -7}") Code:
B=( echo ${A[@]} | awk ' BEGIN { RS=" " } { printf -0.25*$0 " " } ' ) Code:
for ((i=1;i<$Amount+1;i++)) Code:
(standard_in) 1: parse error Edit: I doesn't want multiply a number <0. I want multiply a number 0 < x < 1 |
So I am not 100% on what the actual issue is, however, I see glaring problems in the current bash solution that do not include the use of decimal numbers.
Also, I am not really following the logic either :( Code:
# below finds all files from current folder and below that have files of any type (including directories) |
Quote:
Quote:
Quote:
I think AWK can better deal with the floating point calculations and BASH can better deal with the array manipulations. So I'd learn how to do the calculations using AWK and write a BASH script to manage the processing of each data point. |
Let me explain:
I have 4000 datafiles who exported from a fluid dynamic program. The filename are "TecN210500-xxxxx.plt". "xxxxx" are the iteration number. And i need the iteration number to calculate the solution time of my simulation. The first loop manipulated the filename, so that only the "xxxxx" are written in the array. The first 9 filenames like "TecN210500-0100.plt"; "TecN210500-0200.plt".... Furthermore the variable "p" is needed, because after every loop, the variable i equal 1. The second loop delete the first zero because bash can't calculate with numbers with a leading zero. The variable "amount" counts the datafiles for the last loop. The last loop multiply every entry of the array with the "Time Step" of the simulation. After doing that, I want write these information into the datafile "TecN210500-xxxxx.plt". The first line in the datafile like "Static Pressure". So I write in the first line of the array "Solutiontime". Hope it's clearer now :) and sorry for my bad english.... |
I don't want multiply negativ numbers. Sorry for misunderstanding.
The math operations are: 100 * 3.17*10^-7 200 * 3.17*10^-7 300 * 3.17*10^-7 ........ Input data example (overall 4000 datafile): TecN210500-0100.plt TecN210500-0200.plt TecN210500-0300.plt TecN210500-0400.plt ...... 0100 are the iteration number. The first loop extract the "0100" from the filename. The second loop "delete" the leading zero -> "100". The last loop multiply with the time step -> "100 * 3.17*10^-7" |
All this extra information is more helpful :)
I agree with rtmistler that you should solve for a single solution first as the looping part is a no-brainer. I see now the part of the filename you wish to extract and whilst there are other ways to do this I still see issues with the logic of how you have gone about some things. I was going to say that the second loop does not remove the leading zero from your data, but I now see that your solution to do this was to overwrite the first 10 elements. This method obviously works, as long as the format never changes in the future. I am slightly perplexed on a point though, if all the file names are uniform and are always in amounts of 100 up to 4000, why not just perform the task in a for loop from 100 to 4000 step 100? Unless I have missed that these numbers may change I am not seeing why you need to go through all the trouble of stripping the file names and so on. If the above is correct, this becomes a trivial loop where awk could then be used to perform the task: Code:
awk 'BEGIN{for(i = 100; i <= 4000; i+=100)array[j++] = i * (3.17 * 10**-7)}' |
Thank you for response.
Quote:
Quote:
Quote:
The time steps are not from 100 to 4000 with step 100. There are 100 * 4000 iterations. Every export have 100 iterations included. And 4000 export files are created. Thanks for the awk array. Actually really simple. I will try with your posted code. :) |
As pan64 already suggested, the RS=" " in awk is elegant, efficient, and has no limits.
Code:
# Array[0..9] := 100..1000 Code:
Array=( `echo ${Array[@]} | awk '{print $1*3.17E-7}' RS=" "` ) |
Thank you all for the good advices.
Another problem appeared: The first solution times are: Code:
3,17e-05 Code:
awk "BEGIN {print $Timestep * 3.17 * 10**-7 }" If I do this: Code:
awk "BEGIN {print $Timestep * 3.17 * 10**-7 }" | awk '{ print sprintf("%.9f", $1)}'" Code:
0,000031700 Following results issued: Code:
Timestep Solutiontime Exact Solutiontime Why awk rounds up? And how can I deactivate this command? |
|
All times are GMT -5. The time now is 08:30 PM. |