lacraig2
lacraig2

Reputation: 655

Use multiple lines with Awk

I have a CSV that has underscore delimiters. I have 8 lines that need to be converted to one in this way:

101_1_variableName_(value)
101_1_variableName1_(value2)

into:

101 1 (value) (value2)

(in different boxes preferably)

The problem is that I don't know how to use multiple lines in awk to form a single line. Any help is appreciated.

UPDATE: (input + output)

101_1_1_trialOutcome_found_berry            
101_1_1_trialStartTime_2014-08-05 11:26:49.510000           
101_1_1_trialOutcomeTime_2014-08-05 11:27:00.318000         
101_1_1_trialResponseTime_0:00:05.804000            
101_1_1_whichResponse_d         
101_1_1_bearPosition_6          
101_1_1_patch_9         
101_1_1_food_11 

(last part all one line)

101 1 1 found_berry 2014-08-05 11:26:49.510000 2014-08-05 11:27:00.318000 0:00:05.804000 d 6 9 11

Upvotes: 0

Views: 152

Answers (1)

dawg
dawg

Reputation: 104034

You can use Perl:

use strict;
use warnings;

my %hash=();

while (<DATA>) {
    if (m/^([0-9_]+)_(?:[^_]+)_(.*?)\s*$/) {
        push @{ $hash{join(' ', split('_', $1) )} }, $2;    
    }
}

print "$_ ". join(' ', @{ $hash{$_} })."\n" for (keys %hash);

__DATA__
101_1_1_trialOutcome_found_berry            
101_1_1_trialStartTime_2014-08-05 11:26:49.510000           
101_1_1_trialOutcomeTime_2014-08-05 11:27:00.318000         
101_1_1_trialResponseTime_0:00:05.804000            
101_1_1_whichResponse_d         
101_1_1_bearPosition_6          
101_1_1_patch_9         
101_1_1_food_11 

Prints:

101 1 1 found_berry 2014-08-05 11:26:49.510000 2014-08-05 11:27:00.318000 0:00:05.804000 d 6 9 11

Or, perl one line version:

$ perl -lane '
> push @{ $hash{join(" ", split("_", $1) )} }, $2 if (m/^([0-9_]+)_(?:[^_]+)_(.*?)\s*$/);
> END { print "$_ ". join(" ", @{ $hash{$_}})."\n" for (keys %hash); }
> ' file.txt
101 1 1 found_berry 2014-08-05 11:26:49.510000 2014-08-05 11:27:00.318000 0:00:05.804000 d 6 9 11

Upvotes: 0

Related Questions