user2073484
user2073484

Reputation: 75

How to format the file using awk and sed or perl script to get a desired file

I have a file like below:

101 start_time
102 start_time
101 end_time
103 start_time
103 end_time
102 end_time
104 start_time
104 end_time
102 start_time
102 end_time

I want to have a output file like below:

101 start_time end_time
102 start_time end_time
103 start_time end_time
104 start_time end_time
102 start_time end_time

With basic sed or awk operation or with perl how it can be done? Please help!

Upvotes: 1

Views: 248

Answers (3)

psxls
psxls

Reputation: 6935

Follows a Perl approach.

  • Note 1: not very well written, but it works properly
  • Note 2: my answer is based on the consideration that by "start_time" and "end_time" you don't refer literally to a string, but to some sort of timestamp or whatever

There you go:

#!/usr/bin/perl
use warnings;
use strict;

my @waiting; #here we will keep track of the order
my %previous; #here we will save previous rows that still can't be printed
open (my $IN,'<','file.txt') or die "$!"; #assuming that your data is in file.txt
while (<$IN>) {
    chomp;
    my ($id,$time)=split/ /;
    if (exists $previous{$id}) { #if this is the end_time
        $previous{$id}->[1]=$time;
        if ($waiting[0]==$id) { #if we are not waiting for another row's end_time
            my $count=0;
            for (@waiting) { #print anything you have available
                last if !defined $previous{$_}->[1];
                print join(' ',$x,@{$previous{$_}}),"\n";
                delete $previous{$_};
                $count++;
            }
            shift @waiting for 1..$count; 
        }
    }
    else { #if this is the start_time
        push @waiting,$id;
        $previous{$id}=[$time,undef];
    }
}
close $IN;

Upvotes: 0

Hynek -Pichi- Vychodil
Hynek -Pichi- Vychodil

Reputation: 26121

perl -anE'say "@F end_time" if $F[1] eq "start_time"'

Upvotes: 0

William Pursell
William Pursell

Reputation: 212248

How about:

awk '$1 in a{ print $1, a[$1], $2; delete a[$1]; next} {a[$1] = $2}' input

Upvotes: 2

Related Questions