Perl verify if numbers in a column of a file are in sequence


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl verify if numbers in a column of a file are in sequence
# 1  
Old 05-22-2011
Perl verify if numbers in a column of a file are in sequence

I am just a newbie to perl scripting. I need help with listing of hexadecimal numbers in a column as follows.

INPUT FIle:
Code:
08AF  ship    steel
08B0  ship    steel
08B1  ship    steel
08B2  flight  docs 
08B3  flight  docs 
08B4  flight  docs 
08B5  flight  docs 
08B6  flight  decl 
08B7  flight  decl 
08B8  ship    steel
08B9  ship    steel
08BA  ship    steel
08BB  ship    steel
08BC  ship    steel
08BD  ship    steel
2DB0  truck   indm 
299B  truck   indm 
299C  truck   hmv  
29AB  train   hmv  
29AC  train   hmv
29AD  truck   hmv



Desired OUTPUT:

Item ID list - 08AF:08BD, 2DB0, 299B:299C, 29AB:29AD

Appreciate everybody's help

---------- Post updated at 05:45 AM ---------- Previous update was at 05:43 AM ----------

List numbers in sequence with column and numbers not in range saperated by comma.

Last edited by radoulov; 05-22-2011 at 06:51 AM.. Reason: Code tags.
# 2  
Old 05-22-2011
Got gawk?
Code:
awk '
(strtonum("0x"$1)!=strtonum("0x"last)+1){         #discontinuity encountered
  if(last)                                        #don't print if last undefined
    printf("%s:%s, ",beg,last); 
  beg=$1                                          #remember the beginning of sequence
}
{
  last=$1; 
}
END{
  printf("%s:%s\n",beg,last) #print the last range
}' hexData.txt | sed 's/\(\w\w*\):\1/\1/g'

The sed command replaces
ranges for 1 number, e.g. '2DB0:2DB0' with '2DB0'

---------- Post updated at 01:07 AM ---------- Previous update was at 12:55 AM ----------

Same thing in perl:
Code:
perl -ane '
  if(hex($F[0]) ne hex($last)+1) {
    printf("%s:%s, ",$beg,$last) unless !defined $last;
    $beg=$F[0];
  } 
  $last=$F[0];
END{
  printf("%s:%s\n",$beg,$last);
}' hexData.txt | sed 's/\(\w\w*\):\1/\1/g'


This User Gave Thanks to mirni For This Post:
# 3  
Old 05-22-2011
Thank you mirni !!! that works like a charm. You are really champion of scripting.

---------- Post updated at 09:18 PM ---------- Previous update was at 02:23 PM ----------

Hi Mirni,

How can incorporate that in a script.
# 4  
Old 05-23-2011
Glad to be of help, but I am no champion, trust me Smilie

You can just stick it in script as is -- if you have your input in a file (as that's what it seemed like).
Code:
#!/bin/bash

#do something here
input=/path/to/data.txt
output=processedData.txt

#get ranges of hexes:
perl -ane '
   if(hex($F[0]) ne hex($last)+1) {
    printf("%s:%s, ",$beg,$last) unless !defined $last;
    $beg=$F[0];
  } 
  $last=$F[0];
END{
  printf("%s:%s\n",$beg,$last); 
}' $input | sed 's/\(\w\w*\):\1/\1/g' > $output #change 2BBB:2BBB to 2BBB

#do whatever else you need

---------- Post updated at 12:29 AM ---------- Previous update was at 12:11 AM ----------

Or you can make it an executable script by itself (slightly modified to correct for same address ranges without sed):
Code:
$ cat getHexes.pl
#!/usr/bin/perl 

while(<>) {
    @F = split;
    if(hex($F[0]) ne hex($last)+1) {
      $range = ($beg eq $last) ? $beg : "$beg:$last";
      print "$range, " unless !defined $last;
      $beg=$F[0];
    } 
    $last=$F[0];
}
$range = ($beg eq $last) ? $beg : "$beg:$last";
print "$range\n";
 $ chmod u+x getHexes.pl

Then you can run it like
Code:
 $ ./getHexes inputFile 

or chain it into pipeline, if your input is output of other command:
Code:
 $ tail -n100 someHugeFile.txt | ./getHexes.pl

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use awk to replace numbers in a file with a column from another file

Hello, I am trying to make a awk code that will take 2 files, a txt file like this : 1 1 88 c(1:38, 42, 102) 2 2 128 c(39:41, 43:101, 103:105, 153, 155:189, 292, 344:369) 3 3 84 c(190:249, 603, 606:607, 609:629) 4 4 12 ... (8 Replies)
Discussion started by: nastaziales
8 Replies

2. Shell Programming and Scripting

Creating a sequence of numbers in a line for 1000 files

Hi, I try to explain my problem , I have a file like this: aasdsaffsc23 scdsfsddvf46567 mionome0001.pdb asdsdvcxvds dsfdvcvc2324w What I need to do is to create 1000 files in which myname line listing a sequence of numbers from 0001 to 1000. So I want to have : nomefile0001.txt that must... (10 Replies)
Discussion started by: danyz84
10 Replies

3. Shell Programming and Scripting

Script to generate sequence of numbers

I need awk script to generate part number sequencing based on data in multiple columns like below Input File --------- Col A|Col B|Col C| 1|a|x| 2|b|y| |c|z| | |m| | |n| And out put should be like 1ax 1ay 1az 1am 1an 1bx 1by (6 Replies)
Discussion started by: aramacha
6 Replies

4. UNIX for Dummies Questions & Answers

Hope to create a file with two large column, with several numbers

I hope to create a file made up of 2 columns - first column print out number 0~61000 every 50 of it - second column just contains 0 delineated by space such as 0 0 50 0 100 0 150 0 200 0 ... 60900 0 60950 0 61000 0 Which command should I need to use? I think I might need to use... (5 Replies)
Discussion started by: exsonic
5 Replies

5. UNIX for Dummies Questions & Answers

Adding a column to a text file with row numbers

Hi, I would like to add a new column containing the row numbers to a text file. How do I go about doing that? Thanks! Example input: A X B Y C D Output: A X 1 B Y 2 C D 3 (5 Replies)
Discussion started by: evelibertine
5 Replies

6. Homework & Coursework Questions

program to find and print a Fibonacci sequence of numbers. --Errors

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I am trying to convert a C language program over to Sparc Assembley and I am getting Undefined first referenced... (4 Replies)
Discussion started by: kenjiro310
4 Replies

7. Shell Programming and Scripting

How to insert a sequence number column inside a pipe delimited csv file using shell scripting?

Hi All, I need a shell script which could insert a sequence number column inside a dat file(pipe delimited). I have the dat file similar to the one as shown below.. |A|B|C||D|E |F|G|H||I|J |K|L|M||N|O |P|Q|R||S|T As shown above, the column 4 is currently blank and i need to insert sequence... (5 Replies)
Discussion started by: nithins007
5 Replies

8. Shell Programming and Scripting

Need to find the gap in the sequence of numbers

Hi Guys, I have a file with numbers in sequence. The sequence have been broken somewhere.. I need to find out at which number the sequence has been broken... For an example, consider this sequence, it needs to give me output as 4 (as 5 is missing) and 6(as 7 is missing) Thanks for... (3 Replies)
Discussion started by: mac4rfree
3 Replies

9. UNIX for Dummies Questions & Answers

creating sequence numbers in unix

Hi, Is there a way to create sequence numbers in unix i have a set of batches(which contain records) and i want to assign a number to every batch. how can i do that? (1 Reply)
Discussion started by: dnat
1 Replies

10. UNIX for Dummies Questions & Answers

how can i isolate the random sequence of numbers using awk?

as you can see there is a delimiter after c8 "::". Awk sees the rest as fields because it doesn't recognize spaces and tabs as delimiters. So i am basically looking to isolate 20030003ba13f6cc. Can anyone help? c8::20030003ba13f6cc disk connected configured unknown (2 Replies)
Discussion started by: rcon1
2 Replies
Login or Register to Ask a Question