Sponsored Content
Top Forums Shell Programming and Scripting Not able to read unique values in array Post 302351401 by faiz1985 on Tuesday 8th of September 2009 12:11:06 PM
Old 09-08-2009
Not able to read unique values in array

Hi Friends,

I am having some trouble reading into an array. Basically, I am trying to grep for a pattern and extract it's value and store the same into an array. For eg., if my input is:
Code:
<L:RECORD>name=faisel farooq,age=21,
company=TCS,project=BT</L:RECORD>
<L:RECORD>name=abc xyz,age=22,company=Infosys Tech,project=AT</L:RECORD>
<L:RECORD>name=uresh kum,
age=20,company=TCS L,project=VGP</L:RECORD>
<L:RECORD>name=maha laxmi,age=23,company=Tata Co,project=JP Morgan and STanly</L:RECORD>

I grep for 'age' and want to extract 21 and store it in the array.
Here's the code I have come up with:
Code:
#/bin/perl
$search="Seq";
$seqCount=0;
@seqArray;
$found=0;
open(READ,"op.txt") or die "Can not open file: $!\n";
@array=<READ>;
close READ;
sub checkExist {
        my $a=$_[0];
        for($i=0;$i<=$seqCount;$i++) {
                if($seqArray[$i]==$a) {
#                       $found=1;
                        return 1;
                }
        }
}
foreach $line(@array)
{
        if ($line =~ /$search/)
        {
                $,="\n";
                @sa=split(/([=,])/,$line);
                $count=0;
                print"@sa\n";
                $length=@sa;
                for($i=0;$i<$length;$i++)
                {
                        $found=0;
                        print "Loop $i: $sa[$i]\n";
                        if($sa[$i] =~ /$search/)
                        {
                                if(!checkExist($sa[$i+2]))
                                {
                                        $seqArray[$seqCount]=$sa[$i+2];
                                        print"Bus Txn Seq is: $sa[$i+2]\n";
                                        $seqCount++;
                                }
                        }
                }
        }
}
print "@seqArray\n";

This does not seem to be working, and the program is going into infinite loop.
One thing I noticed is, if i comment out the below if loop:
Code:
if($sa[$i] =~ /$search/)
                        {
                           #     if(!checkExist($sa[$i+2]))
                             #   {
                                        $seqArray[$seqCount]=$sa[$i+2];
                                        print"Bus Txn Seq is: $sa[$i+2]\n";
                                        $seqCount++;
                            #    }
                        }

then the contents do get stored in the array, but not uniquely. Means to say, if i have more records having age=21, then 21 gets stored in the array as many times as it is present in the input file. I would like this to be avoided, and no matter how many times age=21 is present, it should be stored just once.
Thanks in advance for helping..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Getting Unique values in a file

Hi, I have a file like this: Some_String_Here 123 123 123 321 321 321 3432 3221 557 886 321 321 I would like to find only the unique values in the files and get the following output: Some_String_Here 123 321 3432 3221 557 886 I am trying to get this done using awk. Can someone please... (5 Replies)
Discussion started by: Legend986
5 Replies

2. Shell Programming and Scripting

Read textfile and enter the values in array

Hi, I want to put values in .txt file into array. Example : $vi repo.txt abc def ghi jkl mno pqr i want the output to be like this: $echo ${mydf} abc $echo ${mydf} def $echo ${mydf} ghi (3 Replies)
Discussion started by: luna_soleil
3 Replies

3. Shell Programming and Scripting

PHP: Search Multi-Dimensional(nested) array and export values of currenly worked on array.

Hi All, I'm writing a nagios check that will see if our ldap servers are in sync... I got the status data into a nested array, I would like to search key of each array and if "OK" is NOT present, echo other key=>values in the current array to a variable so...eg...let take the single array... (1 Reply)
Discussion started by: zeekblack
1 Replies

4. Shell Programming and Scripting

Read variables names from array and assign the values

Hi, I have requirement to assign values to variables which are created dynamically. Below is the code which i am using to achieve above requirement. #!/bin/ksh oIFS="$IFS"; IFS=',' STR_FAIL_PARENT_IF_FAILS="WF_F_P_IF_FAILS1,WF_F_P_IF_FAILS2,WF_F_P_IF_FAILS3" set -A... (1 Reply)
Discussion started by: tmalik79
1 Replies

5. Shell Programming and Scripting

PERL : Read an array and write to another array with intial string pattern checks

I have an array and two variables as below, I need to check if $datevar is present in $filename. If so, i need to replace $filename with the values in the array. I need the output inside an ARRAY How can this be done. Any help will be appreciated. Thanks in advance. (2 Replies)
Discussion started by: irudayaraj
2 Replies

6. Shell Programming and Scripting

unique inside array

I have a file root@server # cat /root/list12 11.22.33.44 22.33.44.55 33.44.55.66 33.44.55.66 33.44.55.66 I try to pass to array and display unique. root@server# cat /root/test12.sh #!/bin/bash #delcare array badips and accumulate values to array elemenrs badips=( $( cat... (4 Replies)
Discussion started by: anil510
4 Replies

7. Shell Programming and Scripting

How to read values and store in array?

I am reading a value from a file and want to store the value in a dynamic array as i don't know the number of occurrences of the value in that file. How can i do that and then later fetch that value from array (25 Replies)
Discussion started by: Prachi Gupta
25 Replies

8. Shell Programming and Scripting

Count number of unique values in each column of array

What is an efficient way of counting the number of unique values in a 400 column by 1000 row array and outputting the counts per column, assuming the unique values in the array are: A, B, C, D In other words the output should look like: Value COL1 COL2 COL3 A 50 51 52... (16 Replies)
Discussion started by: Geneanalyst
16 Replies

9. UNIX for Beginners Questions & Answers

How To Read a File and Assign the line values to an Array?

i have this basic code that i wrote to read a file and place it's values to an array. the source/input file will have multiple strings on it that is separated by a whitespace. sample_list.txt file contents: ACCT1 TABLE1 ACCT2 TABLE2 ACCT3 TABLE3 script file: sample_list.sh ... (3 Replies)
Discussion started by: wtolentino
3 Replies

10. Shell Programming and Scripting

Get unique elements from Array

I have an array code and output is below: echo $1 while read -r fline; do echo "%%%%%%$fline%%%%%" fmy_array+=("$fline") done <<< "$1" Output: CR30903 YU0007 SRIL CR30903 Yogesh SRIL %%%%%%CR30903 YU0007 SRIL%%%%% %%%%%%CR30903 Yogesh SRIL%%%%% ... (8 Replies)
Discussion started by: mohtashims
8 Replies
strclean(8)						      System Manager's Manual						       strclean(8)

NAME
strclean - Removes outdated STREAMS error log files SYNOPSIS
/usr/sbin/strclean [-d logdir] [-a age] FLAGS
Specifies a maximum age in days for the STREAMS error log files if this not the default age of 3. The value of age must be an integer greater than or less than 3. Specifies a directory for the location of the STREAMS error log files to be removed if this is not the default directory /var/adm/streams. DESCRIPTION
The strclean command cleans the STREAMS error logger directory of log files (error.mm-dd) that contain error messages sent by the STREAMS log driver (strlog). If no other directory is specified (the logdir argument), the command removes error log files in the /var/adm/streams directory. If no other age is specified (the age argument), the command removes error log files that have not been modified in 3 days. The strclean command is executed by the cron daemon on a periodic basis. EXAMPLES
To remove the day-old error log files from a directory called /tmp/streams, enter: strclean -d /tmp/streams -a 1 FILES
One or more error log file or files on which strclean operates. The mm.dd in the filename indicates the month and day of the messages con- tained in the file. RELATED INFORMATION
Commands: strerr(8), cron(8). Interfaces: strlog(7). delim off strclean(8)
All times are GMT -4. The time now is 10:19 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy