Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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
d00PGUMU0009G000001154IMMLADesires output:
s00PGUJU0009G00000114GI295D
d00PGUJU0009G00000114GI295D
s00PGUJU0009G00000114GI49HF
<NONE>
s00PGUMU0009G00000114JPD4M5
d00PGUMU0009G00000114JPD4M5
s00PGUMU0009G00000114JPHNIC
d00PGUMU0009G00000114JPHNIC
s00PGUMU0009G000001154IMMLA
d00PGUMU0009G000001154IMMLA
s00PGUMU0009G000001154IOR2K
<NONE>
s00PGUMU0009G000001154IRCFN
<NONE>

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/perluse 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

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>";
}
}

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

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/ {
}'

![]() |
How i can set file extens...
|
Srch Rplce Characters in ...
|

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