Sponsored Content
Top Forums Shell Programming and Scripting awk uniq and longest string of a column as index Post 302699943 by yifangt on Wednesday 12th of September 2012 04:04:25 PM
Old 09-12-2012
I tried perl script, but did not get what I want, actually empty output. Can anybody help me on my code?
Code:
#!/usr/bin/perl
#This script is to print the longest string of each type, ignore any substrings

use strict;
use warnings;

my $infile  = $ARGV[0];
my $outfile = $ARGV[1];
my @DB;

open(INFILE, "<$infile") or die "Cannot open the input file $!\n";

while (<INFILE>) {
    chomp $_;
foreach my $member (@DB) {
 if (index($member, $_)>=0) {
    next;
    } else   {
       push (@DB, $_);
    }
}
}
close(INFILE);

open (OUTFILE, ">$outfile") or die "Cannot open the output file $!\n";

foreach my $ID (@DB) {
     print OUTFILE "$ID\n";
    }

close(OUTFILE);

infile.txt:
Code:
ABCDEFGHI
ABCDEFGH
CDEFG
ACBDEFGH
ACBDEFG
ABCDE
ADBCE
ADBC
ABC
DBC

And I am expecting output as:
Code:
ABCDEFGHI
ACBDEFGH
ADBCE

Thanks again!
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using Awk in shell script to extract an index of a substring from a parent string

Hi All, I am new to this shell scripting world. Struck up with a problem, can anyone of you please pull me out of this. Requirement : Need to get the index of a substring from a parent string Eg : index("Sandy","dy") should return 4 or 3. My Approach : I used Awk function index to... (2 Replies)
Discussion started by: sandeepms17
2 Replies

2. UNIX for Dummies Questions & Answers

How to remove duplicated based on longest row & largest value in a column

Hii i have a file with data as shown below. Here i need to remove duplicates of the rows in such a way that it just checks for 2,3,4,5 column for duplicates.When deleting duplicates,retain largest row i.e with many columns with values should be selected.Then it must remove duplicates such that by... (11 Replies)
Discussion started by: reva
11 Replies

3. Shell Programming and Scripting

Find longest string and print it

Hello all, I need to find the longest string in a select field and print that field. I have tried a few different methods and I always end up one step from where I need to be. Methods thus far: nawk '{if (length($1) > long) long=length($1); if(length($1)==long) print $1}' The above... (6 Replies)
Discussion started by: SEinT
6 Replies

4. Shell Programming and Scripting

Longest length of string in array

I would be grateful if someone could help me. I am trying to write a .sh script in UNIX. I have the following code; User=john User=james User=ian User=martin for x in ${User} do print ${#x} done This produces the following output; 4 5 3 6 (12 Replies)
Discussion started by: mmab
12 Replies

5. Shell Programming and Scripting

Finding the length of the longest column

Hi, I am trying to figure out how to get the length of the longest column in the entire file (because the length varies from one row to the other) I was doing this at first to check how many fields I have for the first row: awk '{print NF; exit}' file Now, I can do this: awk '{ if... (4 Replies)
Discussion started by: MIA651
4 Replies

6. Shell Programming and Scripting

awk : search last index in specific column

I am trying to search a given text in a file and find its last occurrence index. The task is to append the searched index in the same file but in a separate column. I am able to accomplish the task partially and looking for a solution. Following is the detailed description: names_file.txt ... (17 Replies)
Discussion started by: tarun.trehan
17 Replies

7. Shell Programming and Scripting

Bring values in the second column into single line (comma sep) for uniq value in the first column

I want to bring values in the second column into single line for uniq value in the first column. My input jvm01, Web 2.0 Feature Pack Library jvm01, IBM WebSphere JAX-RS jvm01, Custom01 Shared Library jvm02, Web 2.0 Feature Pack Library jvm02, IBM WebSphere JAX-RS jvm03, Web 2.0 Feature... (10 Replies)
Discussion started by: kchinnam
10 Replies

8. Shell Programming and Scripting

Need help in awk: running a loop with one column and segregate data 4 each uniq value in that field

Hi All, I have a file like this(having 2 column). Column 1: like a,b,c.... Column 2: having numbers. I want to segregate those numbers based on column 1. Example: file. a 5 b 9 b 620 a 710 b 230 a 330 b 1910 (4 Replies)
Discussion started by: Raza Ali
4 Replies

9. Shell Programming and Scripting

Parse the longest matching string

Hello experts, I am trying to unscramble a mixed signal into component signals. Let the list of known signals be $ cat tmplist DU DU4016 GFF GFF2010 GFF201019 G2115 G211 DU40 (1 Reply)
Discussion started by: senhia83
1 Replies

10. UNIX for Beginners Questions & Answers

Replace substring by longest string in common field (awk)

Hi, Let's say I have a pipe-separated input like so: name_10|A|BCCC|cat_1 name_11|B|DE|cat_2 name_10|A|BC|cat_3 name_11|B|DEEEEEE|cat_4 Using awk, for records with common field 2, I am trying to replace all the shortest substrings by the longest string in field 3. In order to get the... (5 Replies)
Discussion started by: beca123456
5 Replies
Crypt::CipherSaber(3pm) 				User Contributed Perl Documentation				   Crypt::CipherSaber(3pm)

NAME
Crypt::CipherSaber - Perl module implementing CipherSaber encryption. SYNOPSIS
use Crypt::CipherSaber; my $cs = Crypt::CipherSaber->new('my pathetic secret key'); my $coded = $cs->encrypt('Here is a secret message for you'); my $decoded = $cs->decrypt($coded); # encrypt from and to a file open(INFILE, 'secretletter.txt') or die "Can't open infile: $!"; open(OUTFILE, '>secretletter.cs1') or die "Can't open outfile: $!"; binmode(INFILE); binmode(OUTFILE); $cs->fh_crypt(*INFILE, *OUTFILE, 1); # decrypt from and to a file open(INFILE, 'secretletter.cs1') or die "Can't open infile: $!"; open(OUTFILE, '>secretletter.txt') or die "Can't open outfile: $!"; binmode(INFILE); binmode(OUTFILE); $cs->fh_crypt(*INFILE, *OUTFILE); DESCRIPTION
The Crypt::CipherSaber module implements CipherSaber encryption, described at http://ciphersaber.gurus.com. It is simple, fairly speedy, and relatively secure algorithm based on RC4. Encryption and decryption are done based on a secret key, which must be shared with all intended recipients of a message. METHODS
new($key, $N) Initialize a new Crypt::CipherSaber object. $key, the key used to encrypt or to decrypt messages is required. $N is optional. If provided and greater than one, it will implement CipherSaber-2 encryption (slightly slower but more secure). If not specified, or equal to 1, the module defaults to CipherSaber-1 encryption. $N must be a positive integer greater than one. encrypt($message) Encrypt a message. This uses the key stored in the current Crypt::CipherSaber object. It will generate a 10-byte random IV (Initial- ization Vector) automatically, as defined in the RC4 specification. This returns a string containing the encrypted message. Note that the encrypted message may contain unprintable characters, as it uses the extended ASCII character set (valid numbers 0 through 255). decrypt($message) Decrypt a message. For the curious, the first ten bytes of an encrypted message are the IV, so this must strip it off first. This returns a string containing the decrypted message. The decrypted message may also contain unprintable characters, as the CipherSaber encryption scheme can handle binary files with fair ease. If this is important to you, be sure to treat the results correctly. crypt($iv, $message) If you wish to generate the IV with a more cryptographically secure random string (at least compared to Perl's builtin rand() func- tion), you may do so separately, passing it to this method directly. The IV must be a ten-byte string consisting of characters from the extended ASCII set. This is generally only useful for encryption, although you may extract the first ten characters of an encrypted message and pass them in yourself. You might as well call decrypt(), though. The more random the IV, the stronger the encryption tends to be. On some operating systems, you can read from /dev/random. Other approaches are the Math::TrulyRandom module, or compressing a file, removing the headers, and compressing it again. fh_crypt(*INPUT, *OUTPUT, ($iv)) For the sake of efficiency, Crypt::CipherSaber can now operate on filehandles. It's not super brilliant, but it's relatively fast and sane. Pass in a reference to the input file handle and the output filehandle. If your platform needs to use "binmode()", this is your responsibility. It is also your responsibility to close the files. You may also pass in an optional third parameter, an IV. There are three possibilities here. If you pass no IV, "fh_crypt()" will pull the first ten bytes from *INPUT and use that as an IV. This corresponds to decryption. If you pass in an IV of your own (gener- ally ten digits, but more than one digits as the code is now), it will use your own IV when encrypting the file. If you pass in the value '1', it will generate a new, random IV for you. This corresponds to an encryption. COPYRIGHT AND LICENSE
Copyright (C) 2000 - 2001 chromatic This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. AUTHOR
chromatic <chromatic@wgz.org> thanks to jlp for testing, moral support, and never fearing the icky details and to the fine folks at http://perlmonks.org Additional thanks to Olivier Salaun and the Sympa project (http://www.sympa.org) for testing. SEE ALSO
the CipherSaber home page at http://ciphersaber.gurus.com perl(1), rand(). perl v5.8.4 2002-05-25 Crypt::CipherSaber(3pm)
All times are GMT -4. The time now is 08:42 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy