removing lines with similar values from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting removing lines with similar values from file
# 1  
Old 03-01-2012
removing lines with similar values from file

Hello,

got a file with this structure:
Code:
33274   171030  02/29/2012      37897   P_GEH   2012-02-29 10:31:26
33275   171049  02/29/2012      38132   P_GEH   2012-02-29 10:35:27
33276   171058  02/29/2012      38515   P_GEH   2012-02-29 10:43:26
33277   170748  02/29/2012      40685   P_KOM   2012-02-29 11:19:27
33278   170053  02/29/2012      41704   P_GEH   2012-02-29 11:35:27
33279   171042  02/29/2012      41983   P_GEH   2012-02-29 11:39:27
33280   170343  02/29/2012      42740   P_KOM   2012-02-29 11:55:27
33281   171030  02/29/2012      44042   P_KOM   2012-02-29 12:15:27
33282   171030  02/29/2012      44053   P_KOM   2012-02-29 12:15:27
33283   170748  02/29/2012      45453   P_GEH   2012-02-29 12:39:27
33284   170281  02/29/2012      45608   P_KOM   2012-02-29 12:43:26

I need to take look thru whole file searching for entries with duplicated numbers at column 2 and 5 and then compare there values from column 4. If the difference is less then 15 it should delete line with greater value.
Please help me out with that :/
# 2  
Old 03-01-2012
Quote:
Originally Posted by krecik28
got a file with this structure:
I need to take look thru whole file searching for entries with duplicated numbers at column 2 and 5 and then compare there values from column 4. If the difference is less then 15 it should delete line with greater value.
Please help me out with that :/
1st you can store line by line in an array.
then fill another array with index of col5&col2 as name.
on index '0' you store value of col4 and on index '1' actual line index.
substract values of col4 and check for your restriction.
if have to delete you know the lineNo and you set the index of the 1st array to empty string.
at the end concatenate the lines of the array to a new file except of the indices containing empty strings...
# 3  
Old 03-01-2012
awk

Hi,

Try this one,

Code:
awk '{k=$2"^"$5;if( a[k] == "" ){a[k]=$0;f4[k]=$4;}else{d=$4-f4[k];if ( d < 15 ){a[k]="";}}}END{for( i in a ){print a[i];}}' file


Cheers,
RangaSmilie
# 4  
Old 03-01-2012
Code:
# awk '{a[x++]=$0};END{for(i=0;i<x;i=i++){f=i;split(a[i],b);i++;split(a[i],c)
if(b[2]b[5]==c[2]c[5]){
if(c[4]-b[4]<15&&c[4]-b[4]>0)
{delete a[i];d=i}
if(b[4]-c[4]<15&&b[4]-c[4]>0)
{delete a[f];d=f}else if(x!=i)print a[f]
}
else
if(d!=f||!d)print a[f]
}}' infile
33274   171030  02/29/2012      37897   P_GEH   2012-02-29 10:31:26
33275   171049  02/29/2012      38132   P_GEH   2012-02-29 10:35:27
33276   171058  02/29/2012      38515   P_GEH   2012-02-29 10:43:26
33277   170748  02/29/2012      40685   P_KOM   2012-02-29 11:19:27
33278   170053  02/29/2012      41704   P_GEH   2012-02-29 11:35:27
33279   171042  02/29/2012      41983   P_GEH   2012-02-29 11:39:27
33280   170343  02/29/2012      42740   P_KOM   2012-02-29 11:55:27
33281   171030  02/29/2012      44042   P_KOM   2012-02-29 12:15:27
33283   170748  02/29/2012      45453   P_GEH   2012-02-29 12:39:27
33284   170281  02/29/2012      45608   P_KOM   2012-02-29 12:43:26


Last edited by ygemici; 03-01-2012 at 11:04 AM.. Reason: correction about if the process is last line of 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

Reducing text file using similar lines

Hello, I am a java programmer but want to try unix for a purpose where I need to reduce a file using its first field.. Here is the sample data: admin;2;0;; admission;8;0;; aman;1;0;; caroline;0;4;; cook;0;4;; cook;2;0;; far;0;3;; far;1;5;; I am explaining the dataset first. There... (5 Replies)
Discussion started by: shekhar2010us
5 Replies

2. Shell Programming and Scripting

Code to exclude lines with similar values

Hi!!! I have a problem with txt file. For example: File: CATEGORY OF XXX AAA 1 XXX BBB CCC AAA 1 XXX DDD EEE AAA 1 XXX FFF GGG AAA 1 XXX KKK LLL AAA 1 XXX MMM ... (4 Replies)
Discussion started by: Tzole
4 Replies

3. Shell Programming and Scripting

extracting lines from a file with similar first name

consider i have two files cat onlyviews1.sql CREATE VIEW V11 AS SELECT id, name, FROM etc etc WHERE etc etc; CREATE VIEW V22 AS SELECT id, name, FROM etc etc WHERE etc etc; CREATE VIEW V33 AS (10 Replies)
Discussion started by: vivek d r
10 Replies

4. UNIX for Dummies Questions & Answers

Matching and reporting near-similar lines in a file

Hi, I have a file with the lines as below: C_10_A05_T7 C_10_A06_SP6 C_10_B05_SP6 C_10_B05_T7 C_10_B01_SP6 C_10_B01_T7 C_12_G07_SP6 C_12_G11_SP6 C_12_G11_T7 C_2_H18_T7 C_2_I02_SP6 C_2_I02_T7 C_2_I13_SP6 C_2_I17_SP6 The four segments of each line are connected by '_' symbols. I... (7 Replies)
Discussion started by: Fahmida
7 Replies

5. Shell Programming and Scripting

Removing zero values from text file

Hi all, I wrote the following code to remove the value which are 0 in the input file (a columns if numbers). awk 'BEGIN { for (i=1; i<=NF; i++) if ($i) printf("%13.6e\n",$i) }' $1 >> $2 The script works if the zeros are written as 0.0000 but not as 0.000000e+00 In... (10 Replies)
Discussion started by: f_o_555
10 Replies

6. UNIX for Dummies Questions & Answers

merge lines within a file that start with a similar pattern

Hello! i have a text file.. which contains the data as follows i want to merge the declarations lines pertaining to one datatype in to a single line as follows i've searched the forum for help.. but couldn't find much help.. how can i do this?? (1 Reply)
Discussion started by: a_ba
1 Replies

7. Shell Programming and Scripting

remove one of each similar lines in a file

Hello folks I have a question for you gurus of sed or grep (maybe awk, but I would prefer the first two) I have a file (f1) that says: (actually, these are not numbers but md5sum, but for simplicity, let's assume these numbers.) 1 2 3 4 5And I have a file (f2) that says 1|a 1|b 1|c 2|d... (3 Replies)
Discussion started by: tukuyomi
3 Replies

8. Shell Programming and Scripting

Counting similar lines from file UNIX

I have a file which contains data as below: nbk1j7o pageName=/jsp/RMBS/RMBSHome.jsf nbk1j7o pageName=/jsp/RMBS/RMBSHome.jsf nbk1j7o pageName=/jsp/RMBS/RMBSHome.jsf nbk1j7o pageName=/jsp/RMBS/RMBSHome.jsf nbk1j7o pageName=/jsp/common/index.jsf nbk1j7o pageName=/jsp/common/index.jsf nbk1wqe... (6 Replies)
Discussion started by: mohsin.quazi
6 Replies

9. Infrastructure Monitoring

Remove Similar Lines from a File

I have a log file "logreport" that contains several lines as seen below: 04:20:00 /usr/lib/snmp/snmpdx: Agent snmpd appeared dead but responded to ping 06:38:08 /usr/lib/snmp/snmpdx: Agent snmpd appeared dead but responded to ping 07:11:05 /usr/lib/snmp/snmpdx: Agent snmpd appeared dead... (4 Replies)
Discussion started by: Nysif Steve
4 Replies

10. Shell Programming and Scripting

How to sort a file and then print similar lines once

Hi! I have a trouble with the sort and the uniq. I know I have to use them, I just have trouble with putting them in the right order. I have a text file with unsorted lines (each line has a few words, the first word in the line is a number.). I need to sort this file in order to be... (6 Replies)
Discussion started by: shira
6 Replies
Login or Register to Ask a Question