Sponsored Content
Top Forums Shell Programming and Scripting Script for identifying and deleting dupes in a line Post 302608282 by ardibehest on Friday 16th of March 2012 07:09:30 PM
Old 03-16-2012
Hello,
I wonder if it would be possible to add to Gimley's program. I had written a perl script to identify duplicates in a large file which has a structure similar to Gimley's.
Quote:
word=word1,word2,word3
where Word is the headword and word1, word2, word3 are all equivalents of the word.
It so happens that some times two entries for the same headword can be present.
Quote:
word=word1,word2,word3
word=word1,word4,word5
I have written a program in PERL which identifies such dupes and spews them out in a file where singletons and dupes are clearly identified.
However I have not been able to add to it the added functionality of merging the duplicates into one single entry.
Thus the dupes mentioned above should merge to one single entry:
Quote:
word=word1,word2,word3,word4,word5
Any help given would be greatly appreciated.

Code:
#!/usr/bin/perl

$dupes = $singletons = "";		# This goes at the head of the file

do {
    $dupefound = 0;			# These go at the head of the loop
    $text = $line = $prevline = $name = $prevname = "";
    do {
	$line = <>;
	$line =~ /^(.+)\=.+$/ and $name = $1;
	$prevline =~ /^(.+)\=.+$/ and $prevname = $1;
	if ($name eq $prevname) { $dupefound += 1 }
	$text .= $line;
	$prevline = $line;
    } until ($dupefound > 0 and $text !~ /^(.+?)\=.*?\n(?:\1=.*?\n)+\z/m) or eof;
    if ($text =~ s/(^(.+?)\=.*?\n(?:\2=.*?\n)+)//m) { $dupes .= $1 }
    $singletons .= $text;
} until eof;
print "SINGLETONS\n$singletons\n\DUPES\n$dupes";

 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

identifying duplicates line & reporting their line number

I need to find to find duplicate lines in a document and then print the line numbers of the duplicates The files contain multiple lines with about 100 numbers on each line I need something that will output the line numbers where duplicates were found ie 1=5=7, 2=34=76 Any suggestions would be... (5 Replies)
Discussion started by: stresslog
5 Replies

2. Shell Programming and Scripting

Shell Script for deleting the first line in a file

Hi, Could any one please post the shell script for deleting the first line in a file? (3 Replies)
Discussion started by: badrimohanty
3 Replies

3. Shell Programming and Scripting

Deleting a line from a flatfile using Shell Script

Hi All, Can Anyone please tell me,how can I delete a line from a file. I am reading the file line by line using whil loop and validating each line..Suppose in the middle i found a particular line is invalid,i need to delete that particular line. Can anyone please help. Thanks in advance,... (14 Replies)
Discussion started by: dinesh1985
14 Replies

4. Shell Programming and Scripting

Using an awk script to identify dupes in two files

Hello, I have two files. File1 or the master file contains two columns separated by a delimiter: a=b b=d e=f g=h File 2 which is the file to be processed has only a single column a h c b What I need is an awk script to identify unique names from file 2 which are not found in the... (6 Replies)
Discussion started by: gimley
6 Replies

5. Shell Programming and Scripting

deleting dupes in a row

Hello, I have a large database in which name homonyms are arranged in a row. Since the database is large and generated by hand, very often dupes creep in. I want to remove the dupes either using an awk or perl script. An input is given below The expected output is given below: As can be... (2 Replies)
Discussion started by: gimley
2 Replies

6. UNIX for Dummies Questions & Answers

Deleting a pattern in UNIX without deleting the entire line

Hi I have a file: r58778.3|SOURCES={KEY=f665931a...,fw,221-705}|ERRORS={16_1:T,30_1:T,56_1:C,57_1:T,59_1:A,101_1:A,115:-,158_1:C,186_1:A,204:-,271_1:T,305:-,350_1:C,368_1:G,442_1:C,472_1:G,477_1:A}|SOURCE_1="Contig_1092402550638"(f665931a359e36cea0976db191ff60ff09cc816e) I want to retain... (15 Replies)
Discussion started by: Alyaa
15 Replies

7. Shell Programming and Scripting

Identifying dupes within a database and creating unique sub-sets

Hello, I have a database of name variants with the following structure: variant=variant=variant The number of variants can be as many as thirty to forty. Since the database is quite large (at present around 60,000 lines) duplicate sets of variants creep in. Thus John=Johann=Jon and... (2 Replies)
Discussion started by: gimley
2 Replies

8. Shell Programming and Scripting

Help with Perl script for identifying dupes in column1

Dear all, I have a large dictionary database which has the following structure source word=target word e.g. book=livre Since the database is very large in spite of all the care taken, it so happens that at times the source word is repeated e.g. book=livre book=tome Since I want to... (7 Replies)
Discussion started by: gimley
7 Replies

9. Shell Programming and Scripting

Modify script to remove dupes with two delimiters

Hello, I have a script which removes duplicates in a database with a single delimiter = The script is given below: # script to remove dupes from a row with structure word=word BEGIN{FS="="} {for(i=1;i<=NF;i++){a++;}for(i in a){b=b"="i}{sub("=","",b);$0=b;b="";delete a}}1 How do I modify... (6 Replies)
Discussion started by: gimley
6 Replies

10. Shell Programming and Scripting

sed command within script wrongly deleting the last line

Hi, I have a shell script which has a for loop that scans list of files and do find and replace few variables using sed command. While doing this, it deletes the last line of all input file which is something wrong. how to fix this. please suggest. When i add an empty line in all my input file,... (5 Replies)
Discussion started by: rbalaj16
5 Replies
String::Errf(3pm)					User Contributed Perl Documentation					 String::Errf(3pm)

NAME
String::Errf - a simple sprintf-like dialect VERSION
version 0.006 SYNOPSIS
use String::Errf qw(errf); print errf "This process was started at %{start}t with %{args;argument}n. ", { start => $^T, args => 0 + @ARGV }; ...might print something like: This process was started at 2010-10-17 14:05:29 with 0 arguments. DESCRIPTION
String::Errf provides "errf", a simple string formatter that works something like "sprintf". It is implemented using String::Formatter and Sub::Exporter. Their documentation may be useful in understanding or extending String::Errf. DIFFERENCES FROM SPRINTF
The data passed to "errf" should be organized in a single hashref, not a list. Formatting codes require named parameters, and the available codes are different. See "FORMATTING CODES" below. As with most String::Formatter formatters, "%" is not a format code. If you want a literal "%", do not put anything between the two percent signs, just write "%%". FORMATTING CODES "errf" formatting codes require a set of arguments between the "%" and the formatting code letter. These arguments are placed in curly braces and separated by semicolons. The first argument is the name of the data to look for in the format data. For example, this is a valid use of "errf": errf "The current time in %{tz}s is %{now;local}t.", { tz => $ENV{TZ}, now => time, }; The second argument, if present, may be a compact form for multiple named arguments. The rest of the arguments will be named values in the form "name=value". The examples below should help clarify how arguments are passed. When an argument appears in both a compact and named form, the named form trumps the compact form. The specific codes and their arguments are: s for string The "s" format code is for any string, and takes no arguments. It just includes the named item from the input data. errf "%{name}s", { name => 'John Smith' }; # returns "John Smith" Remember, "errf" does not have any of the left- or right-padding formatting that "sprintf" provides. It is not meant for building tables, only strings. i for integer The "i" format code is used for integers. It takes one optional argument, "prefix", which defaults to the empty string. "prefix" may be given as the compact argument, standing alone. "prefix" is used to prefix non-negative integers. It may only be a plus sign. errf "%{x}i", { x => 10 }; # returns "10" errf "%{x;+}i", { x => 10 }; # returns "+10" errf "%{x;prefix=+}i", { x => 10 }; # returns "+10" The rounding behavior for non-integer values is not currently specified. f for float (or fractional) The "f" format code is for numbers with sub-integer precision. It works just like "i", but adds a "precision" argument which specifies how many decimal places of precision to display. The compact argument may be just the prefix or the prefix followed by a period followed by the precision. errf "%{x}f", { x => 10.1234 }; # returns "10"; errf "%{x;+}f", { x => 10.1234 }; # returns "+10"; errf "%{x;.2}f", { x => 10.1234 }; # returns "10.12"; errf "%{x;+.2}f", { x => 10.1234 }; # returns "+10.12"; errf "%{x;precision=.2}f", { x => 10.1234 }; # returns "10.12"; errf "%{x;prefix=+;precision=.2}f", { x => 10.1234 }; # returns "+10.12"; t for time The "t" format code is used to format timestamps provided in epoch seconds. It can be given two arguments: "type" and "tz". "type" can be either date, time, or datetime, and indicates what part of the timestamp should be displayed. The default is datetime. "tz" requests that the timestamp be displayed in either UTC or the local time zone. The default is local. The compact form is just "type" alone. # Assuming our local time zone is America/New_York... errf "%{x}t", { x => 1280530906 }; # "2010-07-30 19:01:46" errf "%{x;type=date}t", { x => 1280530906 }; # "2010-07-30" errf "%{x;type=time}t", { x => 1280530906 }; # "19:01:46" errf "%{x;type=datetime}t", { x => 1280530906 }; # "2010-07-30 19:01:46" errf "%{x;tz=UTC}t", { x => 1280530906 }; # "2010-07-30 23:01:46 UTC" errf "%{x;tz=UTC;type=date}t", { x => 1280530906 }; # "2010-07-30 UTC" errf "%{x;tz=UTC;type=time}t", { x => 1280530906 }; # "23:01:46 UTC" errf "%{x;tz=UTC;type=datetime}t", { x => 1280530906 }; # "2010-07-30 23:01:46 UTC" n and N for numbered The "n" and "N" format codes are for picking words based on number. It takes two of its own arguments, "singular" and "plural", as well as "prefix" and "precision" which may be used for formatting the number itself. If the value being formatted is 1, the singular word is used. Otherwise, the plural form is used. errf "%{x;singular=dog;plural=dogs}n", { x => 0 }; # 0 dogs errf "%{x;singular=dog;plural=dogs}n", { x => 1 }; # 1 dog errf "%{x;singular=dog;plural=dogs}n", { x => 2 }; # 2 dogs errf "%{x;singular=dog;plural=dogs}n", { x => 1.4 }; # 1.4 dogs errf "%{x;singular=dog;plural=dogs;precision=1}n", { x => 1.4 }; # 1.4 dogs errf "%{x;singular=dog;plural=dogs;precision=0}n", { x => 1.4 }; # 1 dog If "N" is used instead of "n", the number will not be included, only the chosen word. errf "%{x;singular=is;plural=are}N", { x => 0 }; # are errf "%{x;singular=is;plural=are}N", { x => 1 }; # is errf "%{x;singular=is;plural=are}N", { x => 2 }; # are errf "%{x;singular=is;plural=are}N", { x => 1.4 }; # 1.4 are errf "%{x;singular=is;plural=are;precision=1}N", { x => 1.4 }; # 1.4 are errf "%{x;singular=is;plural=are;precision=0}N", { x => 1.4 }; # 1 is The compact form may take any of the following forms: word - equivalent to singular=word word+suffix - equivalent to singular=word;plural=wordsuffix word1/word2 - equivalent to singular=word;plural=word2 If no singular form is given, an exception is thrown. If no plural form is given, one will be generated according to some basic rules of English noun orthography. AUTHOR
Ricardo Signes <rjbs@cpan.org> COPYRIGHT AND LICENSE
This software is copyright (c) 2010 by Ricardo Signes. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. perl v5.12.3 2010-10-29 String::Errf(3pm)
All times are GMT -4. The time now is 08:30 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy