Get unique elements from Array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get unique elements from Array
# 8  
Old 01-01-2020
Quote:
Originally Posted by MadeInGermany
Assuming the input file has got many TKT records in sequence, an associative array is perfect:
Code:
awk '
NF>=3 {
  A[$1]=(A[$1] ORS $2)
  D[$1]=$3
}
END {
  for (t in A) {
    print "TKT:", t
    print  "ACTOR:", A[t]
    print "DEPT:", D[t]
    print ""
  }
}' file

Also doable in bash 4 (using its associative arrays).
There is no file involved here.

I tried your solution but i get syntax error

Code:
    while read -r fline; do
      fmy_array+=("$fline")
echo $fline | awk 'NF>=3 {A[$1]=(A[$1] ORS $2) D[$1]=$3 } END {   for (t in A) {    print "TKT:", t    print  "ACTOR:", A[t]    print "DEPT:", D[t]    print ""  }}'
    done <<< "$1"

error output:
awk: cmd. line:1: NF>=3 {A[$1]=(A[$1] ORS $2) D[$1]=$3 } END {   for (t in A) {    print "TKT:", t    print  "ACTOR:", A[t]    print "DEPT:", D[t]    print ""  }}
awk: cmd. line:1:                                  ^ syntax error

--- Post updated at 12:52 PM ---

Quote:
Originally Posted by nezabudka
here's another
Code:
for fline in $1; do
if ! [[ "${my_array[@]}" =~ "$fline" ]]; then
        my_array+=("$fline")
fi
done
echo ${my_array[@]}

This solution did not help. Although it took care of the duplicate entries there is no way to determine the three field of each array element into "TKT", "ACTOR" & "DEPT"

here is the output I got:

Quote:
CR30903 YU0007 SRIL Yogesh
--- Post updated at 01:06 PM ---

Quote:
Originally Posted by nezabudka
Hi
To get started, simply print the value of this parameter
Code:
echo "$1"

--- Post updated at 17:15 ---

Something like this?
Code:
cat file
30903	 YU0007	SRIL
30903	 Yogesh	SRIL
awk '{ B[$2] } END { print "TKT:", $1 RS "ACTOR:"; for (i in B) print i; print "DEPT:", $3 }' file

--- Post updated at 17:20 ---

or maybe
Code:
if [[ "${my_array[@]}" =~ $fline ]]; then
    continue
fi

There is no files involved here.

Also, I have updated the Original post with the output of $1.

--- Post updated at 01:43 PM ---

Quote:
Originally Posted by nezabudka
Code:
awk '!T[$0]++' RS='[[:space:]]+' <<<"$1"

or
Code:
grep -o '\S*' <<<"$1" | sort -u

Further, if you want, you can arrange this into an array, here is a sample
Code:
my_array=($(grep -o '\S*' <<<"$1" | sort -u))

And please pay attention to post number #4
Code:
grep -o '\S*' <<<"$1" | sort -u

<- This works but there is no order so i dont know which element is what as requested in the desired output. Can you please help assign this to get displayed as below:

Current output:
Quote:
For string "30903 YU0007 SRIL 30903 Yogesh SRIL"

Getting output:

30903
SRIL
Yogesh
YU0007



or For string "30903 YU0007 SRIL 30903 Yogesh WAFF"

Getting output:

30903
SRIL
Yogesh
WAFF
YU0007


Desired output:

TKT: 30903

DEPT: SRIL

ACTOR:
Yogesh
YU0007

or

TKT: 30903

DEPT:
SRIL
WAFF

ACTOR:
Yogesh
YU0007
# 9  
Old 01-01-2020
Code:
egrep -o '(\S+\s+){2}\S+' <<<"$1" |
  awk '
        {A[$1]; B[$3]; C[$2]}
  END   { print "TKT:" cat(A) "\n\nDEPT:" cat(B) "\n\nACTOR:" cat(C)}
  function cat(T)
        { s=""; for (i in T)
                  s = s RS i
                  return s
        }'

This User Gave Thanks to nezabudka For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Awk: count unique elements in a field and sum their occurence across the entire file

Hi, Sure it's an easy one, but it drives me insane. input ("|" separated): 1|A,B,C,A 2|A,D,D 3|A,B,B I would like to count the occurence of each capital letters in $2 across the entire file, knowing that duplicates in each record count as 1. I am trying to get this output... (5 Replies)
Discussion started by: beca123456
5 Replies

2. Shell Programming and Scripting

Help reading the array and sum of the array elements

Hi All, need help with reading the array and sum of the array elements. given an array of integers of size N . You need to print the sum of the elements in the array, keeping in mind that some of those integers may be quite large. Input Format The first line of the input consists of an... (1 Reply)
Discussion started by: nishantrefound
1 Replies

3. Shell Programming and Scripting

Multiplication of array elements

Hi, I can't find out how to create correct code to get multiplication of each elements of array. Let's say I enter array into command line (2 3 4 5 6 8) and i need output 2*3*4*5*6*8=5760. I tried this one, but answer is 0. for i in $@; do mult=$((mult*i))done echo "mult: " $mult ... (4 Replies)
Discussion started by: rimasbimas
4 Replies

4. UNIX for Dummies Questions & Answers

Merging tables: identifiying common and unique elements

Hi all, I know how to merge two tables and to remove the duplicated lines based on a field (Column 2) . My next challenge is to be able to identify in a new column those common elements between table A & B, those elements in table A not present in table B and vice versa. A simple count would be... (6 Replies)
Discussion started by: lsantome
6 Replies

5. Shell Programming and Scripting

Grouping array elements - possible?

I have a script which takes backup of some configuration files on my server. It does that by using an array which contains the complete path to the files to backup. It copys the files to a pre defined dir. Each "program" has it's own folder, ex. apache.conf is being copied to /predefined... (7 Replies)
Discussion started by: dnn
7 Replies

6. Shell Programming and Scripting

Creating array with non-duplicate / unique elements in ksh

Hi all, I have created 3 arrays which can have common elements in each like- arr_a contains str1 str2 str3 str4 str5 arr_b contains str3 str6 str7 str1 str8 arr_c contains str4 str9 str10 str2 each array is created with "set -A arr_name values" command. I want to create a resultant array-say... (1 Reply)
Discussion started by: sanzee007
1 Replies

7. UNIX for Dummies Questions & Answers

printing array elements

Is there a way to print multiple array elements without iterating through the array using bash? Can you do something like... echo ${array}and get all those separate elements from the array? (2 Replies)
Discussion started by: jrymer
2 Replies

8. UNIX for Dummies Questions & Answers

Help with replacing Array elements

Hi, I have an array containing following sample information @array = qw (chr02 chr02 chr02 chr02 chr02 chr03 chr03 chr04 chr04 chr05 chr05 chr05 chr07 chr07) I need to replace all duplicate entries by an underscore to get the following output@array = qw (chr02 _ _ _ _ chr03 _ chr04 _ chr05 _ _... (4 Replies)
Discussion started by: pawannoel
4 Replies

9. UNIX for Dummies Questions & Answers

Deleting Array Elements

Hi, I am writing a BASH shell script. I have an array that will contain IN ANY ORDER the following elements: DAY 8D MO NS. I would like to erase the element DAY, but since the order of the elements in the array are random, I will not know which element # DAY is (ie it's not as simple as... (3 Replies)
Discussion started by: msb65
3 Replies

10. Shell Programming and Scripting

To return the elements of array

Hi, Please can someone help to return the array elements from a function. Currently the problem I face is that tempValue stores the value in myValue as a string while I need an array of values to be returned instead of string. Many Thanks, Sudhakar the function called is: ... (5 Replies)
Discussion started by: Sudhakar333
5 Replies
Login or Register to Ask a Question