Computing.Net > Forums > Programming > AWK - line pairing task

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

AWK - line pairing task

Reply to Message Icon

Name: user57
Date: June 4, 2008 at 10:08:59 Pacific
OS: AIX 5.3
CPU/Ram: 1.9/9
Product: IBM/550
Comment:

Hello,

I have a line matching exercise. I have identical lines prefixed with either a "s" or "d".

The idea is to pair the "s" and "d" lines together. In the event the corresponding line is missing print "<NONE>" in its place. Please refer to the desired output.

Input file:
s00PGUJU0009G00000114GI295D
s00PGUJU0009G00000114GI49HF s00PGUMU0009G00000114JPD4M5
s00PGUMU0009G00000114JPHNIC
s00PGUMU0009G000001154IMMLA
s00PGUMU0009G000001154IOR2K
s00PGUMU0009G000001154IRCFN d00PGUJU0009G00000114GI295D d00PGUMU0009G00000114JPD4M5
d00PGUMU0009G00000114JPHNIC
d00PGUMU0009G000001154IMMLA

Desires output:
s00PGUJU0009G00000114GI295D
d00PGUJU0009G00000114GI295D
s00PGUJU0009G00000114GI49HF
<NONE>
s00PGUMU0009G00000114JPD4M5
d00PGUMU0009G00000114JPD4M5
s00PGUMU0009G00000114JPHNIC
d00PGUMU0009G00000114JPHNIC
s00PGUMU0009G000001154IMMLA
d00PGUMU0009G000001154IMMLA
s00PGUMU0009G000001154IOR2K
<NONE>
s00PGUMU0009G000001154IRCFN
<NONE>



Sponsored Link
Ads by Google

Response Number 1
Name: FishMonger
Date: June 4, 2008 at 14:02:50 Pacific
Reply:

I have a line matching exercise.

That tells me that this is probably a homework assignment. We won't do someones homework, but if you post the awk command(s) that you've tried, someone will help guide you.

ShaqDiesel showed a possible Perl solution, but it could be improved. There's no need to duplicate the data across 4 arrays. It could easily be done with a single hash.

============================================================


#!/usr/bin/perl

use strict;
use warnings;

my %data;
while( <DATA> ) {
push @{$data{substr($_, 1)}}, $_;
}

for my $key ( sort keys %data ) {
print $data{$key}->[0];
print $data{$key}->[1] ? $data{$key}->[1] : "<NONE>\n";
}


__DATA__
s00PGUJU0009G00000114GI295D
s00PGUJU0009G00000114GI49HF
s00PGUMU0009G00000114JPD4M5
s00PGUMU0009G00000114JPHNIC
s00PGUMU0009G000001154IMMLA
s00PGUMU0009G000001154IOR2K
s00PGUMU0009G000001154IRCFN
d00PGUJU0009G00000114GI295D
d00PGUMU0009G00000114JPD4M5
d00PGUMU0009G00000114JPHNIC
d00PGUMU0009G000001154IMMLA



0

Response Number 2
Name: ShaqDiesel
Date: June 4, 2008 at 14:11:58 Pacific
Reply:

Do you have to use kornshell for it? Because in perl you can

1.create arrays @sLines, @dLines
2.copy the arrays:
@sLinesCOPY=@sLines;
@dLinesCOPY=@dLines;
3.put your data in two arrays so you can compare them later:
while(INPUT_HANDLE) {
if(/^s/) {
push(@sLines, $_);
}
else if(/^d/) {
push(@dLines, $_);
}
else { ; }
}
4.compare the strings and pair them if possible:
foreach(@sLinesCOPY) {
$sElement=unshift(@sLines);
$dElement=unshift(@dLines);
if($sElement ~= m/^s(.*)/) {
$sRemainder = $1;
}
if($dElement ~= m/^d(.*)/) {
$dRemainder = $1
}
print $sElement;
if($sRemainder eq $dRemainder) {
print $dElement;
}
else {
print "<NONE>";
}
}


0

Response Number 3
Name: ghostdog
Date: June 4, 2008 at 19:05:01 Pacific
Reply:

you asked for awk


awk '
/^s/{ s[substr($0,2)]++ }
/^d/ { d[substr($0,2)]++}
END {
for(i in s) {
print "s"i
if ( i in d ){
print "d"i
}else {
print "None"
}
}
}
' file


0

Response Number 4
Name: user57
Date: June 5, 2008 at 01:05:26 Pacific
Reply:

Ghostdog,

Many thanks for the solution. This was exactly what I was looking for.

The requirement was to troubleshoot a HACMP issue in particular for the grpsvcs subsystem which should have a "d" and "s" entry for each concurrent volume group. My solution was a little crude requiring manual cross checking:

lssrc -ls grpsvcs| awk '$0 ~/^s00/ || $0 ~/^d00/ {
print
}'


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


How i can set file extens... Srch Rplce Characters in ...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: AWK - line pairing task

Batch file to get last character www.computing.net/answers/programming/batch-file-to-get-last-character/16945.html

comprehensively edit an html from batch file www.computing.net/answers/programming/comprehensively-edit-an-html-from-batch-file/19080.html

Compare files with AWK www.computing.net/answers/programming/compare-files-with-awk/15182.html