Sponsored Content
Top Forums Shell Programming and Scripting Use decimal value of array in bc ends with illegal character Post 302955502 by Don Cragun on Thursday 17th of September 2015 09:53:16 PM
Old 09-17-2015
I am assuming that you are really dealing with angles (the slope between two lines) and forces (instead of angels (benevelant attendant spirits) and forces) and have changed variable names to match; but I have not changed the typos in your sample input file. I also assume that the val2_* variables are intended to be used to select ranges of angles to be processed using the 3rd and 4th values in each set of 4 values comprising a set (even though these variables are not referenced at all in your sample code).

The code you provided seemed to be saving a minimum value in the variable named max_force instead of a maximum value. The code below sets max_force1 to the maximum value found in the 2nd field in each test set with an angle in in the inclusive range $val1_angle_low <= field1 <= $val1_angle_high and sets max_force2 to the maximum value found in the 4th field in each test set with an angle in in the inclusive range $val2_angle_low <= field3 <= $val2_angle_high. Your code also multiplied force values by 100 for no obvious reason. At the end max_force1, max_force2, and the maximum of those two values are all printed. The code below does not modify input values, but does print maximum force values at the end after multiplying them by 100.

I do not know why the tail commands in your code start collecting data on line 9 of your input file. The sample data file given seems to have data starting on line 4 of the. The code below assumes data starts on the line following the 1st line in the file where the 3rd semicolon separated field is the string force (which appears to be the last line of the headers in your sample input).
Code:
;angel;force;angel;force;angel;force;angel;force;angel;force;angel;force

The code below makes wild guesses at what in your sample code was desired output and what was intended to be printed only as debugging information. With the unconditional output and the debugging output, I hope that you will find ways to print what you want. I don't know if you wanted a single maximum force value or one value for the data from the 1st two values in each set of values and one value for the data from the 2nd two values in each set. The following codes prints individual and combined data.

The sample data you provided didn't now have any column three values in the range 33 through 55, so no value was selected from the 3rd and 4th values in each set. The following code strips DOS <carriage-return> characters from the input and (when run in a Locale where the LC_NUMERIC category has period as the radix character) can process input files that have period, comma, or a mixture of both as the radix character.

The following code was written and tested using the Korn shell, but should work with any shell that recognizes Bourne shell syntax (rather than csh shell syntax). It won't work with a pure Bourne shell because it needs basic POSIX parameter expansions (copied from your sample script). So, it should work with ash, bash, dash, ksh, zsh, and other shells that recognize the syntax used by these shells.

If you want to enable debugging printouts, switch the line just before the exit at the end of the script with the line just after the exit.

If someone wants to try this code on a Solaris/SunOS system, change awk in this script to /usr/xpg4/bin/awk or nawk.

Code:
#!/bin/ksh

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

val1_angle_high=66
val1_angle_low=56

val2_angle_high=55
val2_angle_low=33

awk -F';' -v v1low=$val1_angle_low -v v1high=$val1_angle_high \
    -v v2low=$val2_angle_low -v v2high=$val2_angle_high '
FNR == 1 {
	# Note that we are looking for the end of the header in this file.
	hdr = 1
}
hdr {	# Look for the last header line in this file.
	if($3 == "force")
		hdr = 0
	if(debug) printf("hdr %d deleted: %s\n", FNR, $0)
	next
}
{	# Process data lines...
	# Convert commas to periods (assume European data collector with
	# "," as radix character and American data processor with "." as
	# radix character) and get rid of DOS <carriage-return>s.
	gsub(",", ".")
	gsub("\r", "")
	if(debug) printf("line %d: %s\n", FNR, $0)
	# Process all test sets on the current line...
	for(col = 2; col < NF; col += 4) {
		# Look for val1 angle in range.
		if($col >= v1low && $col <= v1high)
			# Look for new maximum...
			if(found1++) {
				if($(col + 1) > max_force1)
					max_force1 = $(col + 1)
			} else	max_force1 = $(col + 1)
		# Look for val2 angle in range.
		if($(col + 2) >= v2low && $(col + 2) <= v2high)
			# Look for new maximum...
			if(found2++) {
				if($(col + 3) > max_force2)
					max_force2 = $(col + 3)
			} else	max_force2 = $(col + 3)
		if(debug) {
			printf("subscript: %d\n", col)
			printf("range1 cnt: %d, angle1=%s, max=%s\n",
			    found1, $col,
			    found1 ? max_force1 : "undefined")
			printf("range2 cnt: %d, angle2=%s, max=%s\n",
			    found2, $(col + 2),
			    found2 ? max_force2 : "undefined")
		}
	}
}
END {	# Print results.
	printf("# of values found in range1 (%f <= angle1 <= %f): %d\n%s ",
	    v1low, v1high, found1, "Maximum selected range1 value: ")
	if(found1)
		printf("%f\n", max_force1 * 100)
	else	print "undefined"
	printf("# of values found in range2 (%f <= angle2 <= %f): %d\n%s ",
	    v2low, v2high, found2, "Maximum selected range2 value: ")
	if(found2)
		printf("%f\n", max_force2 * 100)
	else	print "undefined"
	printf("# of values found in range: %d\nMaximum selected value: ",
	    found1 + found2)
	if(found1 + found2)
		printf("%f\n",
		    ((found1 && found2) ? \
			max_force1 > max_force2 ? max_force1 : max_force2 : \
			found1 ? max_force1 : max_force2) * 100)
	else	print "undefined"
}' "$csv"
exit
}' debug=1 "$csv"

With the sample data you provided (and debugging disabled), the above code produces the output:
Code:
# of values found in range1 (56.000000 <= angle1 <= 66.000000): 3
Maximum selected range1 value:  -439.000000
# of values found in range2 (33.000000 <= angle2 <= 55.000000): 0
Maximum selected range2 value:  undefined
# of values found in range: 3
Maximum selected value: -439.000000

 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 01:19 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy