finding first instance


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers finding first instance
# 1  
Old 08-09-2002
finding first instance

I have a file with 3 columns. Often the first two will hold the same values and the third may differ. I only want one row for each.

i.e.

aaa,bbb,1
aaa,bbb,2
aaa,bbb,3
xxx,yyy,1
xxx,yyy,2
zzz,rrr,5

Should be

aaa,bbb,1
xxx,yyy,1
zzz,rrr,5

I just want the first instance of each pariing of column 1 and 2 - and it's 3rd column value and ignore any subsequent rows with the same 1st and second column pairing.
# 2  
Old 08-09-2002
Do u need a shell script that would do this process when give this file as an input???

-Nisha
# 3  
Old 08-09-2002
This should do it
Code:
# !/bin/sh
toggle=0
field1=""
field2=""
lastfield1=""
lastfield2=""
exec < inputfile
exec > outputfile
while read line ; do
        echo $line > linetemp
        field1=`awk 'BEGIN {FS=","} {print $1}' linetemp`
        field2=`awk 'BEGIN {FS=","} {print $2}' linetemp`

        if [ $field1 != $lastfield1 -o $field2 != $lastfield2 ]
        then
                lastfield1=$field1
                lastfield2=$field2
                toggle=0
        fi

        if [ $toggle -eq 0 ]
        then
                echo $line
                toggle=1
        fi
done
rm linetemp

Sorry about the indenting if it's messed up, I haven't quite figured out how to keep it when I paste into this.

Moderator's note: I took the liberty of adding the code tag to fix the indenting. - Perderabo

Last edited by Perderabo; 08-09-2002 at 09:10 AM..
# 4  
Old 08-09-2002
Here is a solution using Perl and a hash. This perl routine will work even if the data is not in a sorted order.

Code:
open(INFILE, "myFile.txt") || die "$!";

my %dataHash;

while ($inputLine = <INFILE>) {
  chomp($inputLine);
  my ($column1, $column2, $column3) = split(/\,/, $inputLine);
  my $key = "$column1,$column2";
  
  if (!exists($dataHash{$key})) {
    $dataHash{$key} = $column3;
  };
};

foreach $outputLine (sort(keys(%dataHash))) {
  print "$outputLine,$dataHash{$outputLine}\n";
};

# 5  
Old 08-09-2002
Well I've found the answer from a colleague ..... so I'll share.....for anyone who's interested.

nawk -F\| ' { if ( $1 != field1|| $2 != field2)
{ print $1 "," $2 "," $3 ; field1 = $1; field2 = $2 }
} ' TMPSERVICE_mappings > SERVICE_mappings
# 6  
Old 08-12-2002
Hey Peter,

I am unable to execute the script that u have sent....
Smilie

Help me!!!!

Thanks,
Nisha

P.S. I too tried writing a script for this problem.. but couldn't really succeed.. I would like to post it ask doubts abt it..

Otherwise, i have done with similar types of records but with two different files...long ago.
# 7  
Old 08-12-2002
I assume you have nawk in your path? (/usr/bin/nawk usually).

If not if should be fine with awk.

If you want to see why it's not working break it down....
you need a file with pipe '|' delimiters
you need a file with at least 3 fields
you need an input file (mine is TMPSERVICE_mappings)

That's all you should need. What's the problem with you nawk script?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Grep the only instance name

Hi, I want to get the only application name from the server. Ex: if i give $ ps -ef | grep bw. It will show all BW process with entire path. It will little confuse to list out the process. Can anyone have syntax to get only the instance name. I need this for be, hawk,ems also. Please... (2 Replies)
Discussion started by: ckchelladurai
2 Replies

2. Shell Programming and Scripting

Keep the last instance of the record

Hi All, I have a input file like 1| abc 1| abcd 1| abcde 2| abc 2| abcd 3| abcde I want the output like 1| abcde 2| abcde Any help would be highly appreciated. Thanks in advance. (9 Replies)
Discussion started by: lrkp
9 Replies

3. Red Hat

Apache instance

Hi , Maximum How many instances of apache can we run in one box? (2 Replies)
Discussion started by: krish4linux
2 Replies

4. Shell Programming and Scripting

What does : do in this instance

Guys please see below functions to return a status depending on user input. Both seem to work the same. The second way has a : line which i can't understand or see in a ksh manual anywhere. Instead of doing the variable change if its empty on this line the first function simply does it on the... (7 Replies)
Discussion started by: lavascript
7 Replies

5. Shell Programming and Scripting

matching first instance of FS

Hi All, I have a property in a file as: property=value=a If I use FS="=" then I want only first = to be considered as field separator and remaining as value echo -e "property=value=a" | awk -F= '{print $2}' ie my $2 should be value=a Can anyone please help me with this. I need it in... (3 Replies)
Discussion started by: gurukottur
3 Replies

6. Shell Programming and Scripting

replace first instance(not first instance in line)

Alright, I think I know what I am doing with sed(which probably means I don't). But I cant figure out how to replace just the first occurance of a string. I have tried sed, ed, and grep but can't seem to figure it out. If you have any suggestions I am open to anything! (3 Replies)
Discussion started by: IronHorse7
3 Replies

7. Shell Programming and Scripting

finding duplicate files by size and finding pattern matching and its count

Hi, I have a challenging task,in which i have to find the duplicate files by its name and size,then i need to take anyone of the file.Then i need to open the file and find for more than one pattern and count of that pattern. Note:These are the samples of two files,but i can have more... (2 Replies)
Discussion started by: jerome Sukumar
2 Replies

8. Shell Programming and Scripting

Single Instance

Hi, I have a script. I want only one instance of the script to be running at any point of the time. How can I do it. what would be the exact format of the ps command for doing this. For example the name of my script is "Inst.sh" Thanx in advance (2 Replies)
Discussion started by: sendhil
2 Replies

9. Linux

OTRS instance

hi frnds here i m trying to configure OTRS instance but i m getting the following error message while runnning through browser. I m writing the following http://192.168.1.55:8080/otrs2/index.pl " #!/usr/bin/perl -w... (7 Replies)
Discussion started by: naik_mit
7 Replies

10. UNIX for Dummies Questions & Answers

Copy Db Instance

I need to copy my Live Db Instance to my Test Db Instance Can somebody please tell me the easiest way to go about this It is an Informix Database running on HP-UX Thanks (0 Replies)
Discussion started by: cobdeng
0 Replies
Login or Register to Ask a Question