Need Shell Script for Array


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

Hi all,
I have an input file like below (a comma seperated file)

Code:
 345,12,10
 400,11,8
 328,1,3

I need to get the output as below ...
Code:
  record 345 sum is 12
  record 400 sum is 10
  record 328 sum is 1
  record 345 count is 10
  record 400 count is 8
  record 328 count is 3


I am trying to achieving it by reading the input file through array concept,i dnt have any idea on array concept in shell scripting

Thanks in advance
Sai
# 2  
Old 02-23-2015
Hello Hemanth,

Following may help you in same. This should work and tested only on the provided format of input.
Code:
awk -F, '{for(i=1;i<2;i++){A[o]=A[o]?A[o] ORS "record " $i " sum is "  $(i+1):A[o] ORS "record " $i " sum is "  $(i+1);o++;A[o]=A[o]?A[o] ORS "record " $i " sum is "  $(i+2):"record " $i " sum is "  $(i+2);}}END{for(i in A){print A[i]}}'  Input_file

Output will be as follows.
Code:
record 345 sum is 12
record 345 sum is 10
record 400 sum is 11
record 400 sum is 8
record 328 sum is 1
record 328 sum is 3

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 02-23-2015
Hi Singh,
Thanks for the reply,

In the output iam getting as
Code:
record 345 sum is 12
record 345 sum is 10
record 400 sum is 11
record 400 sum is 8
record 328 sum is 1
record 328 sum is 3

Like,everthing is coming with sum only i had count too iam getting sum text in count place too..

my output should be like
Code:
record 345 sum is 12
  record 400 sum is 10
  record 328 sum is 1
  record 345 count is 10
  record 400 count is 8
  record 328 count is 3

and can you please tell like wat is A[o]=A[o]?A[o] ORS exactly means....

thanks in advance,
Sai

---------- Post updated at 02:03 AM ---------- Previous update was at 01:59 AM ----------

hi ,
one more question will the loop work if the records in the input file increase...?
# 4  
Old 02-23-2015
Hello Hemanth,

Could you please try following.
Code:
awk -F, '{for(i=1;i<2;i++){A[o]=A[o]?A[o] ORS "record " $i " sum is "  $(i+1):A[o] ORS "record " $i " sum is "  $(i+1);o++;A[o]=A[o]?A[o] ORS "record " $i " count is "  $(i+2):"record " $i " count is "  $(i+2);}}END{for(i in A){print A[i]}}'  Input_file

Output will be as follows.
Code:
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

Also for explaination for asked query is:
Code:
A[o]=A[o]?A[o] ORS "record " $i " count is "  $(i+2):"record " $i " count is "  $(i+2)
A[o]=A[o]  ## Store values in aray A whose index is variable o.
?               ## When condition if any in above command explaned is TRUE  do following actions.
A[o] ORS "record " $i " count is "  $(i+2) ## Keep value of A[o] as well as concatenate current value too.
:               ## If condition in first step if any is FALSE then do following.
"record " $i " count is "  $(i+2) ## Keep A[o]'s value to "record " $i " count is "  $(i+2)


Hope this helps, let me know if you have any queries.

Thanks,
R. Singh
# 5  
Old 02-23-2015
Thanks for the explanation singh,

Final Query
you are displaying the output as
Code:
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

can it be like below because i want the output as follows

Code:
record 345 sum is 12
  record 400 sum is 10
  record 328 sum is 1
  record 345 count is 10
  record 400 count is 8
  record 328 count is 3

# 6  
Old 02-23-2015
Hello Hemath,

Could you please try following and let me know if this helps.
Code:
awk -F, '{for(i=1;i<2;i++){A[o]=A[o]?A[o] ORS "record " $i " sum is "  $(i+1):A[o] ORS "record " $i " sum is "  $(i+1);o++;A[o]=A[o]?A[o] ORS "record " $i " count is "  $(i+2):"record " $i " count is "  $(i+2);}}END{for(i in A){print A[i]}}' test06 | sort -r -k 3,3

Also it will sort according to the keyword sum and count.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 7  
Old 02-23-2015
hi thanks for the help,
I appreciate your skill.... Smilie
one more doubt after sort you are specifying 3,3 right is it the records count...?
and one more will the script work if i increase the records count in input file...?
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