Sponsored Content
Top Forums Shell Programming and Scripting Selecting lines having same values for first two columns Post 302748871 by pamu on Thursday 27th of December 2012 04:17:58 AM
Old 12-27-2012
Quote:
Originally Posted by manojmalhotra13
Thanks for replying so quickly and providing me the desired solution.

I tried the solution provided by RudiC and Sathyaonunix. Both are providing me the solutions. I am looking into the script by Pamu ( I am finding it difficult to understand as my expertise in Unix is not good, so kindly give me some time.)

As regarding query raised by RudiC

(i) if the third column entries are identical, then only one row is to be retained in the original file.

(ii) values cannot be negative.

(iii) the lines without duplicate values to be retained in the orginal file as such.

Thank you once again.

Manoj
Thanks for providing above details.

As per your current requirement I've modified script lit bit.

Now please check.

Code:
awk 'NR==FNR{A[$1,$2]++; B[$1,$2]=B[$1,$2]?B[$1,$2]<$3?$3:B[$1,$2]:$3;next}
{if(A[$1,$2]>1){if(B[$1,$2]==$3){B[$1,$2]=0;print }else{print > "another_file"}}else{print }}' file file

A[$1,$2]++ # Increments the array index for $1 and $2.

B[$1,$2]=B[$1,$2]?B[$1,$2]<$3?$3:B[$1,$2]:$3 # Here we compare is $3 is greater than previous $3 for $1 and $2 and retain max value and assign it to B[$1,$2]

I hope this helps Smilie

pamu
This User Gave Thanks to pamu For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Selecting Unique Values from many List

I have a question I have like 19 different list which contains the name of the server but what I need is just unique ones. First thing I need to do is just make a unique list within the list itself i.e. delete anything that is repeated inside the list like for example in list1 i... (1 Reply)
Discussion started by: pareshan
1 Replies

2. Shell Programming and Scripting

Selecting specific 'id's from lines and columns using 'SED' or 'AWK'

Hello experts, I am new to this group and to 'SED' and 'AWK'. I have data (text file) with 5 columns (C_1-5) and 100s of lines (only 10 lines are shown below as an example). I have to find or select only the id numbers (C-1) of specific lines with '90' in the same line (of C_3) AND with '20' in... (6 Replies)
Discussion started by: kamskamu
6 Replies

3. Shell Programming and Scripting

Selecting rows based on values in columns

Hi My pipe delimited .txt file contains rows with 10 columns. Can anyone advise how I output to file only those rows with the letters ‘ci' as the first 2 characters in the 3rd column ? Many thanks (4 Replies)
Discussion started by: malts18
4 Replies

4. Programming

selecting values of date

In a table, date is stored in a column as "2011-01-4". If I write query to get the dates > "2011-01-06" , then the date "2011-01-4" is also listed. The date stored in the column is a varchar datatype. So how can I make a query to not display the date "2011-01-4" ? Is there any solution ? Thank... (4 Replies)
Discussion started by: gameboy87
4 Replies

5. Programming

Selecting array values

I have two arrays DIST(1:NCOF) and X(1:NX) Let NCOF = 5 and NX = 15, with DIST = and X = I want to create an array that puts a zero if DIST is outside the region in X, otherwise putting 1. In this example I should get RES = Using DIST = would give RES = The values in... (6 Replies)
Discussion started by: kristinu
6 Replies

6. Shell Programming and Scripting

how to retrieve lines that the first 4 columns have different values

Hi, all: I am not familiar with unix,and just started awk scripts. I want to retrieve lines that have the first 4 columns with different values. For example, the input is like this (tab delimited file with one header) r1 A A A A x r2 A B B A x r3 B B B B x the output should be (header is... (15 Replies)
Discussion started by: new2awkin2011
15 Replies

7. Shell Programming and Scripting

selecting record by matching in two columns values

Hi Guys ! i want to search a record in file by matching two values in a record in two different columns suppose i have 3 columns and i want to select all those values from col1 for which in col3 has a specific value e.g select all "john" from column1 where column 3 has a value of "20" ... (9 Replies)
Discussion started by: ourned
9 Replies

8. Shell Programming and Scripting

Selecting lowest and highest values in columns 1 and 2, based on subsets in column 3

Hi, I have a file with the following columns: 361459 447394 CHL1 290282 290282 CHL1 361459 447394 CHL1 361459 447394 CHL1 178352861 178363529 AGA 178352861 178363529 AGA 178363657 178363657 AGA Essentially, using CHL1 as an example. For any line that has CHL1 in... (2 Replies)
Discussion started by: hubleo
2 Replies

9. Shell Programming and Scripting

Help with shell script: selecting rows that have the same values in two columns

Hello, everyone I am beginner for shell programming. I want to print all lines that have the same values in first two columns data: a b 1 2 a a 3 4 b b 5 6 a b 4 6 what I expected is : a a 3 4 b b 5 6 but I searched for one hour in... (2 Replies)
Discussion started by: nengcheng
2 Replies

10. Shell Programming and Scripting

How to print lines that have values in certain columns ?

Hi, everyone I have a dataset like this: A B C D A C C D E F G H F D K Y X A K K C Gsome of columns have no values in each line. I want to print all lines that have 1/2/3/4 values, export separately to four files. What I expected is like this: file1 Y file 2 A C X Afile 3... (3 Replies)
Discussion started by: nengcheng
3 Replies
Alzabo::Runtime::Row(3pm)				User Contributed Perl Documentation				 Alzabo::Runtime::Row(3pm)

NAME
Alzabo::Runtime::Row - Row objects SYNOPSIS
use Alzabo::Runtime::Row; my $row = $table->row_by_pk( pk => 1 ); $row->select('foo'); $row->update( bar => 5 ); $row->delete; DESCRIPTION
These objects represent actual rows from the database containing actual data. In general, you will want to use the "Alzabo::Runtime::Ta- ble" object to retrieve rows. The "Alzabo::Runtime::Table" object can return either single rows or row cursors. ROW STATES
Row objects can have a variety of states. Most row objects are "live", which means they represent an actual row object. A row can be changed to the "deleted" state by calling its "delete()" method. This is a row that no longer exists in the database. Most method calls on rows in this state cause an exception. There is also a "potential" state, for objects which do not represent actual database rows. You can call "make_live()" on these rows in order to change their state to "live". Finally, there is an "in cache" state, which is identical to the "live" state, except that it is used for object's that are cached via the "Alzabo::Runtime::UniqueRowCache" class. METHODS
Row objects offer the following methods: select (@list_of_column_names) Returns a list of values matching the specified columns in a list context. In scalar context it returns only a single value (the first column specified). If no columns are specified, it will return the values for all of the columns in the table, in the order that are returned by "Alzabo::Run- time::Table->columns". This method throws an "Alzabo::Runtime::NoSuchRowException" if called on a deleted row. select_hash (@list_of_column_names) Returns a hash of column names to values matching the specified columns. If no columns are specified, it will return the values for all of the columns in the table. This method throws an "Alzabo::Runtime::NoSuchRowException" if called on a deleted row. update (%hash_of_columns_and_values) Given a hash of columns and values, attempts to update the database to and the object to represent these new values. It returns a boolean value indicating whether or not any data was actually modified. This method throws an "Alzabo::Runtime::NoSuchRowException" if called on a deleted row. refresh Refreshes the object against the database. This can be used when you want to ensure that a row object is up to date in regards to the database state. This method throws an "Alzabo::Runtime::NoSuchRowException" if called on a deleted row. delete Deletes the row from the RDBMS and changes the object's state to deleted. For potential rows, this method simply changes the object's state. This method throws an "Alzabo::Runtime::NoSuchRowException" if called on a deleted row. id_as_string Returns the row's id value as a string. This can be passed to the "Alzabo::Runtime::Table->row_by_id" method to recreate the row later. For potential rows, this method always return an empty string. This method throws an "Alzabo::Runtime::NoSuchRowException" if called on a deleted row. is_live Indicates whether or not the given row represents an actual row in the database. is_potential Indicates whether or not the given row represents an actual row in the datatbase. is_deleted Indicates whether or not the given row has been deleted table Returns the "Alzabo::Runtime::Table" object that this row belongs to. schema Returns the "Alzabo::Runtime::Schema" object that this row's table belongs to. This is a shortcut for "$row->table->schema". rows_by_foreign_key This method is used to retrieve row objects from other tables by "following" a relationship between two tables. It takes the following parameters: * foreign_key => "Alzabo::Runtime::ForeignKey" object Given a foreign key object, this method returns either a row object or a row cursor object the row(s) in the table to which the relation- ship exist. The type of object returned is based on the cardinality of the relationship. If the relationship says that there could only be one match- ing row, then a row object is returned, otherwise it returns a cursor. POTENTIAL ROWS
The "potential" row state is used for rows which do not yet exist in the database. These are created via the "Alzabo::Runtime::Ta- ble->potential_row" method. They are useful when you need a placeholder object which you can update and select from, but you don't actually want to commit the data to the database. These objects are not cached. Once "make_live()" is called, the object's state becomes "live". Potential rows have looser constraints for column values than regular rows. When creating a new potential row, it is ok if none of the columns are defined. If a column has a default, and a value for that column is not given, then the default will be used. However, you cannot update a column in a potential row to undef (NULL) if the column is not nullable. No attempt is made to enforce referential integrity constraints on these objects. You cannot set a column's value to a database function like "NOW()", because this requires interaction with the database. make_live This method inserts the row into the database and changes the object's state to "live". This means that all references to the potential row object will now be references to the real object (which is a good thing). This method can take any parameters that can be passed to the "Alzabo::Runtime::Table->insert" method. Any columns already set will be passed to the "insert" method, including primary key values. However, these will be overridden, on a col- umn by column basis, by a "pk" or "values" parameters given to the "(make_live()" method. Calling this method on a row object that is not in the "potential" state will cause an "Alzabo::Runtime::LogicException" AUTHOR
Dave Rolsky, <autarch@urth.org> perl v5.8.8 2007-12-23 Alzabo::Runtime::Row(3pm)
All times are GMT -4. The time now is 07:26 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy