Need Shell Script for Array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need Shell Script for Array
# 15  
Old 02-23-2015
Try:
Code:
awk -F, -v sq="'" '
{	printf("record %s sum is %" sq ".2f\n", $1, $2 / 100)
	r[NR] = $1
	c[NR] = $3
}
END {	for(i = 1; i <= NR; i++) printf("record %s count is %s\n", r[i], c[i])
}' file

which, with an input file containing:
Code:
345,12,10
400,11,8
328,1,3
111,1234567890,9

produces the output:
Code:
record 345 sum is 0.12
record 400 sum is 0.11
record 328 sum is 0.01
record 111 sum is 12,345,678.90
record 345 count is 10
record 400 count is 8
record 328 count is 3
record 111 count is 9

Alternatively, you could do it with externally defined complete format strings as in:
Code:
awk -F, '
{	printf(sum_format, $1, $2 / 100)
	r[NR] = $1
	c[NR] = $3
}
END {	for(i = 1; i <= NR; i++) printf(count_format, r[i], c[i])
}' sum_format="record %s sum is %'.2f\n" count_format="record %s count is %s\n" file

which produces the same output as the previous script.

This isn't the format you showed in your latest request (since you did not show a decimal point nor did you show two digits following the decimal point), but should do what you were trying to do with the format string %'.2f (assuming that the field 2 value in your input is a number of pennies rather than a number of dollars).

In the future PLEASE show us representative data that you want to process and corresponding output in the format you really want to get. If you would have shown us the actual diagnostic message(s) you got from awk we wouldn't have to guess whether your version of awk does not recognize a single quote as a printf numeric format flag or if you just didn't know how to include a single quote in a single quoted string.
# 16  
Old 02-27-2015
You may try

Code:
[akshay@localhost tmp]$ cat infile
345,12,10
400,11,8
328,1,3

Code:
[akshay@localhost tmp]$ awk -F, 'BEGIN{A[1]="sum";A[2]="count"}FNR==1{fmt="Record %d "A[++i]" is %s\n"}{printf(fmt,$1,$(1+i))}' infile infile
Record 345 sum is 12
Record 400 sum is 11
Record 328 sum is 1
Record 345 count is 10
Record 400 count is 8
Record 328 count is 3

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding Two Array in shell script

Hi Experts, I've got this problem where I need to add two array in shell script such that that is an element is greater than 9 then it get further split into individual digit, something like below : Input :- array1=(2 6 8 9 10 12) array2=(5 4 6 8 12 14) Output :- array3=(7 1 0 1 4 1 7 2 2... (8 Replies)
Discussion started by: mukulverma2408
8 Replies

2. Shell Programming and Scripting

Pass C shell array to another C shell script(csh) and shell(sh)

Dear Friends, Please help me on this my script name is send.csh In this i have written the statement like this set args = ( city state country price ) I want to pass this array to another c shell called receiver.csh. and i want to use it in this c shell or how to pass to... (2 Replies)
Discussion started by: SA_Palani
2 Replies

3. Shell Programming and Scripting

Array in shell script

Hi, check=("/usr/local/bin/chk_nag | awk -F":" '{print $1}'" "/usr/local/bin/chk_kas | awk -F":" '{print $1}'" "/usr/local/bin/chk_was | awk -F":" '{print $1}'" ) for i in "${check}"; do echo $i; done when I run this. It says Syntax error: "(" unexpected Please advise. (5 Replies)
Discussion started by: ashokvpp
5 Replies

4. Shell Programming and Scripting

Comparing two array in shell script

In shell script i have two variables with value debit_sal="DOG,HIU,JIU,GYT,PPI,KIU,HUY........." debit_req='HIU, JIU, HUY, GTR, KOI, ............" i stored this two in two array arr=$(echo $debit_sal| tr "," "\n"); arr1=$(echo $debit_req| tr ", " "\n"); #(note second arry has comma with... (5 Replies)
Discussion started by: greenworld123
5 Replies

5. Shell Programming and Scripting

Array in Ksh Shell script

hi team, i have a file, which contains only variable and its value param.ksh --------- export A=123 export B=345 export C=567 export D=OPLI export E=OL89PO From shell script, i am invoking this file and use the value of this variable. Now there are 5 variable in above file. Before i... (1 Reply)
Discussion started by: ace_friends22
1 Replies

6. BSD

Creating an array out of users: shell script

I want to create a shell script for a menu selection consisting of users defined on the machine. To an administrator having the privileges, the selection menu will look as follows: Select the user you want to define the variables for: 1) my-username-1 2) my-username-2 etc Then there would be a... (7 Replies)
Discussion started by: figaro
7 Replies

7. Shell Programming and Scripting

Array declaration in Shell script

this is my code declare -a USERCERT declare -a CACERT declare -a KEYSRC this is the error + declare -a USERCERT ./clone.sh: 1: declare: not found + declare -a CACERT ./clone.sh: 1: declare: not found + declare -a KEYSRC ./clone.sh: 1: declare: not found (11 Replies)
Discussion started by: xerox
11 Replies

8. Shell Programming and Scripting

IF condition against a ARRAY in shell script

Hi, I want to check a particular string inserted by User to be checked against the values i already have in a ARRAY string using IF condition. Is this possible? if yes how to do that. example : i have a,b,c,d,e,f values in a array called values i asked user to enter a value: user entered... (2 Replies)
Discussion started by: kukretiabhi13
2 Replies

9. Solaris

i cannot give array declaration in shell script

Dear all, i m unable to give array decalartion in solaris Operating system shell script and script was so.sh declare -a bull for i in 1 2 3 4 5 6 7 8 9 do bull=$i echo "${bull}" done it is throwing an error called so.sh: declare: not found so.sh: bull=1: not... (20 Replies)
Discussion started by: naree
20 Replies

10. Shell Programming and Scripting

Size of an array in sh shell script

Is there a way to find out the size of an array in sh shell script? Thanks. (1 Reply)
Discussion started by: trivektor
1 Replies
Login or Register to Ask a Question