Use decimal value of array in bc ends with illegal character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Use decimal value of array in bc ends with illegal character
Prev   Next
# 1  
Old 09-17-2015
Use decimal value of array in bc ends with illegal character

hi all

I have to read a long cvs file every 4 columns with decimal "3,45" numbers.
The 9th row in this cvs is the first line I need, so it I tail -n+9.
I use sed -e 's/,/./g' to get decimal values with . delimiter.

So far no problem.

Goal is to get two maximum negative forces in ranges 56-66 degrees and 33-55 degrees.



Just for better understanding:
one test case is a set of two direction (open and close)
open and close have both an value of angel in degrees and a value force in Newton.

Here is a table header of three test cases. The original csv file has thousands and around 340 rows of angel/force combinations.

Code:
;test no 3;;;;test no 2;;;;test no 1;;;;
;process open;;process close;process open;;process close;process open;;process close
;angel;force;angel;force;angel;force;angel;force;angel;force;angel;force
;68,09;8,88;0,4;0,6;68,27;8,46;0,21;0,7;68,27;8,8;0,21;0,64
;67,44;0,19;0,12;3,41;67,72;0,35;0,03;2,65;67,72;0,26;0,03;2,2
;66,62;1,5;0,05;0,73;67,08;0,37;0,24;0,05;66,89;0,28;0,24;0,38
;60,46;-4,39;4,65;0,92;60,46;-4,79;5,02;0,82;60,55;-4,65;5,2;0,92
.....



What I need to get working is the commented if-statement.

Code:
#!/bin/bash

csv=${1:-"/home/my/test.csv"}

val1_angel_high=66
val1_angel_low=56

val2_angel_high=55
val2_angel_low=33

no_cols=$(head -n 1 "${csv}"  | awk -F";" '{ print NF}')
no_val_pairs=$((((no_cols--))/4))

while [ $no_cols -gt 1 ]; do
        angelCollection_process_1=( $(tail -n+9 "${csv}" | cut -d ';' -f "$no_cols" | sed -e 's/,/./g') )
        forceCollection_process_1=( $(tail -n+9 "${csv}" | cut -d ';' -f "$((no_cols+1))" | sed -e 's/,/./g') )
        #printf "Process 1 - Angel: %s Force: %s\n" "${angelCollection_process_1[@]}" "${forceCollection_process_1[@]}"

        position=0
        max_force=0.0;
        for t in "${angelCollection_process_1[@]}"; do
                if (( $(bc <<< "$t <= $val1_angel_high") && $(bc <<< "$t >= $val1_angel_low") )); then
                        #echo $t : $max_force">"${forceCollection_process_1[$position]}; #works
                        force="${forceCollection_process_1[$position]}";
                        echo $max_force $force; #works

                        force2=$(bc <<< "$force*100");
                        echo $force2; # not working (standard_in) 1: illegal character: ^M

                        #if (( $(bc <<< "${forceCollection_process_1[$position]} < $max_force") )); then
                        #       max_force="${forceCollection_process_1[$position]}";
                        #fi
                fi
                ((position++))
        done

        echo $max_force

        no_cols=$((no_cols-4))
done

Code:
Output when I disable force2
0.0 0.9
0.0 0.96
0.0 1.04
0.0 1.16
0.0 1.1
0.0 1
0.0 0.94
0.0 0.94
0.0 0.98
0.0 1.02
0.0 1
0.0 0.98
0.0 1.04
0.0 0.96
0.0 0.9
0.0 0.72
0.0 0.56
0.0 0.38
0.0 0.22
0.0 0.18
0.0 0.08
0.0 0.04
0.0 0
0.0 0.05
0.0 0.09
0.0 0.15
0.0 0.39
0.0 0.47
0.0 0.77
0.0 -1.21
0.0 -1.53
0.0 -2.71
0.0 -1.87
0.0 -1.75
0.0 -1.91
0.0 -2.03
0.0 -1.81
0.0 -1.71
0.0 -1.53
0.0 -1.55
0.0 -1.57
0.0 -1.45
0.0 -1.45
0.0 -1.49
0.0 -1.45
0.0 -1.53
0.0 -1.31


Hope you find a solution how I can use the value from array in bc.
Other solutions are also welcome. I try to use perl also, but I failed with reading csv every 4 columns.

Example of csv content: ; 000X ; 000Y ; 001X ; 001Y ; 002X ; 002Y - Pastebin.com
Moderator's Comments:
Mod Comment Please use CODE tags for sample input, output, and sample CODE; not INDENT tags for sample input and QUOTE tags for output.

Last edited by Don Cragun; 09-17-2015 at 07:11 AM.. Reason: Add CODE tags; drop INDENT and QUOTE tags.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Remove a word that ends with special character "!"

Hi all. I want to use sed to remove a word that ends with "!" in the first page of a file. The word I want to remove is: "DNA!". I have search for an answer and nothing of what I found helped me. ~faizlo (2 Replies)
Discussion started by: faizlo
2 Replies

2. Shell Programming and Scripting

Replace character in awk array

somedata | awk -F"#soils#" '{split($1,a,"NITNUM="); print a}' how can i edit the content of array 2 above? what i want to do is get rid of single quotes and double quotes. and then turn the "NewLine" into new lines. the data in array 2 (a) is: ... (2 Replies)
Discussion started by: SkySmart
2 Replies

3. Programming

Urgent help needed.. C++ program to convert decimal to hexa decimal

Hi , seq can be 0...128 int windex = seq / 8; int bindex = seq % 8; unsigned char bitvalue = '\x01' << (7-bindex) ; bpv.bitmapvalue = bitvalue; This is the part of a program to convert decimal to bitmap value of hexadecimal. I want this to change to convert only to... (1 Reply)
Discussion started by: greenworld123
1 Replies

4. Programming

Character pointer to Character array

how to copy content of character pointer to character array in c programming.. char *num; char name=num; (1 Reply)
Discussion started by: zinat
1 Replies

5. Shell Programming and Scripting

Bash 3.2 - Array / Regex - IF 3rd member in array ends in 5 digits then do somthing...

Trying to do some control flow parsing based on the index postion of an array member. Here is the pseudo code I am trying to write in (preferably in pure bash) where possible. I am thinking regex with do the trick, but need a little help. pesudo code if == ENDSINFIVEINTS ]]; then do... (4 Replies)
Discussion started by: briandanielz
4 Replies

6. UNIX for Advanced & Expert Users

Illegal character in prototype Perl error in AIX server

HI All , I am using AIX version 6 . having issue with below perl code, sub Article ($procudure, @params) { my ($procudure, @params) = @_; #Get handle TO Dataware House DATABASE try { my $dbHandle = &Cobol::DBAccess::getDBDwConnection(); ,,,,,,,,,,,,, ,,,,,,,,,,,,... (3 Replies)
Discussion started by: Perlbaby
3 Replies

7. Shell Programming and Scripting

Decimal value for special character

I am seeing an special character in my file when i do the cat filename | od-bc I see a value of 376 for that special character. I would like to find the decimal value for the character. For example the decimal value for ctrl-Y is char(25). Appreciate help on this. (11 Replies)
Discussion started by: pinnacle
11 Replies

8. Shell Programming and Scripting

need help in expanding hexa decimal character range

Hi all, I have a input like this 3AF9:3B01 and need to expand to the below output 3AF9 3AFA 3AFB 3AFC 3AFD 3AFE 3AFF 3B00 3B01 Please let me know the easiest way for achieving this. Thanks for the help in advance... (6 Replies)
Discussion started by: leo.maveriick
6 Replies

9. Programming

Multidimension character array

Hi ! I'm having problem with assigning values to a multidimensional character array. i wanted to have an array with 48 fields ,each filed being of varying size and hence have declared the array as char struct_arr; I am trying to assign values to the fileds as struct_arr = token ... (1 Reply)
Discussion started by: janemary.a
1 Replies

10. Shell Programming and Scripting

How To Make Decimal Point Fall On The 15th Character On The Screen

If i have a variable which is a decimal number, i.e 34.05 How can you make decimal point fall on the 15th character on the screen? Or any other that you can specify? Can you do it using sed or awk? (3 Replies)
Discussion started by: Vozx
3 Replies
Login or Register to Ask a Question