Need Shell Script for Array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need Shell Script for Array
# 8  
Old 02-23-2015
Hello Hemanth,

Following are the answers for your queries.
1: yes sort -r -k 3,3 means to sort the text in reverse order by taking the 3rd column as primary key.
2: yes, as I have mentioned in my very first post this code will work only on 3 fields seperated with ,.


Hope this helps, let us know if you have any queries on same. If you have exact data in a different format please do let us know always from the very first post itself, it helps us too to help/guide.


Thanks,
R. Singh
# 9  
Old 02-23-2015
With a simple input structure as in your sample, no matter how many lines (records) the file will contain, this might work:
Code:
awk '{print "record", $1, "sum is", $2; print "record", $1, "count is", $3; }' FS="," file
record  345 sum is 12
record  345 count is 10
record  400 sum is 11
record  400 count is 8
record  328 sum is 1
record  328 count is 3

For the order that you require, pipe it through sort as RavinderSingh13 proposed.
# 10  
Old 02-23-2015
If all of the following are true:
  • you want the output order to be the same as the input order for both "sum" lines and "count" lines,
  • you don't really have leading spaces in your input file, and
  • you don't really want spaces at the start of each output line after the first output line,
you could try something like:
Code:
awk -F, '
{	printf("record %s sum is %s\n", $1, $2)
	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 produces the output:
Code:
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

Note that the input line:
Code:
400,11,8

in your sample input produces the output shown in red above, not the output:
Code:
record 400 sum is 10

that you said you wanted. If you really want the line above, you need to clearly explain what transformations are supposed to be applied to the data in the 2nd input field in your input records to produce the output you say you want.

If you want to try this script on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
# 11  
Old 02-23-2015
hi ,
thanks for the reply..i just gave a example for the input file,

Can you please tell me like how to print the sum digit in currecy format ..?

like
Code:
record 345 sum is 12,345

Consider like in the input file the 345 record sum is 12345
i know that awk command do...
Code:
awk -F "," '{printf(fmt,value)}' FS=,OFS=, fmt="%'.2f"

can you please tell me like how to do that in the code you mentioned i didnt get that %s concept in the code

thanks in advance
sai
# 12  
Old 02-23-2015
You'll have to divide the number by 100 and replace the "%s" with your desired format "%.2f". Note that not all versions will recognize the " ' " in the format string.
# 13  
Old 02-23-2015
hi ,
in the below code
Code:
awk -F, '
{ printf("record %s sum is %s\n", $1, $2)
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

how can i represent to get the $2 value as currency format as in awk command its not accepting "%'.2f" as it contains ' symbol... can u help me on this like wat symbol i should add in the place of %s to get the desired format like 123,456 , 1,234 ..... Smilie
# 14  
Old 02-23-2015
Why don't you do it stepwise - like print the number divided by 100, print it with the "%.2f" format, and so on - and present us with the intermediate results so we see what goes wrong when.

As I said, the " ' " is not accepted on all systems/tools. Looks like yours is one of those. You'll now have to work on the resulting string if you insist on the thousands' separator.
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