1752323 Members
5621 Online
108786 Solutions
New Discussion юеВ

using awk with open

 
SOLVED
Go to solution
Ed Rantanen
New Member

using awk with open

I am new to perl so this might sound simple.

Here is a little script I have been working on, from a flat file I can get it to work but not from a pipe. print comes out as it should, but I am not sure why it dose not work for the while.
Any help would be appreciated.


#open(INFO, "iscan.txt");
open(INFO, "ioscan -fnCdisk |awk '\$1==\"disk\" {getline dev; print \$0, dev }|");

# print ;

$search = "CX3";
while ( ) {
if (( /$search/ ) && ( s/^.* $search-[234]0cWDR5 (.*) (.*)$/$2/ )) {
print;
}
}
8 REPLIES 8
James R. Ferguson
Acclaimed Contributor

Re: using awk with open

Hi:

You want a piped open:

#!/usr/bin/perl
open(INFO, '-|', 'ioscan -fnCdisk' ) or die "Open error: $!\n";

$search = "CX3";
while ( ) {
if (( /$search/ ) && ( s/^.* $search-[234]0cWDR5 (.*) (.*)$/$2/ )) {
print;
}
}

Regards!

...JRF...
Rita C Workman
Honored Contributor

Re: using awk with open

I am not a programmer...but...
your print command seems to be out of place.

Here's a piece of one of my old ones.

awk 'BEGIN {
while ( "cat '$files' " | getline ) {
entries++
if ( $1 ~ "" ) print $1 }
}

The print is before the }.
Rita
Ed Rantanen
New Member

Re: using awk with open

I should have clarified better, when I do an ioscan the lines have to be rejoined before use.
If I run the command
ioscan -fnCdisk | awk '$1=="disk" {getline dev; print $0, dev }├в > iscan.txt
the lines are joined. I was hoping to put the complete statement into the open(), and then use INFO with the while. I am trying to script out this process.

Insf -e
Ioscan -fnCdisk
Pvcreate /dev/rdsk/c#t#d#
Lvextend
Lvcreate
Newfs
James R. Ferguson
Acclaimed Contributor
Solution

Re: using awk with open

Hi (again) Ed:

> I should have clarified better, when I do an ioscan the lines have to be rejoined before use.

OK, but you *still* don't want to mix 'awk' into Perl --- there's no need :-)

#!/usr/bin/perl
use strict;
use warnings;
my $line = '';
my $search = 'CX3';

open(INFO, '-|', 'ioscan -fnCdisk' ) or die "Open error: $!\n";

while ( ) {
next if m{^(Class|===)};
chomp;
if ( m{^disk} ) {
$line = $_;
next;
}
$_ = $line . $_;
print "$_\n";
if (( /$search/ ) && ( s/^.* $search-[234]0cWDR5 (.*) (.*)$/$2/ )) {
print;
}
}

Regards!

...JRF...
Ed Rantanen
New Member

Re: using awk with open

Thanks for the help!

Made quick mod to the prints and it works great now. The hard part will be understanding the new junks of code.
James R. Ferguson
Acclaimed Contributor

Re: using awk with open

Hi (again) Ed:

Since you are a new member, I'll ask you to please look at this, too:

http://forums11.itrc.hp.com/service/forums/helptips.do?#28

Welcome and Regards!

...JRF...
Ed Rantanen
New Member

Re: using awk with open

Thanks for the help problem solved.
Dennis Handly
Acclaimed Contributor

Re: using awk with open

>awk '$1=="disk" {getline dev; print $0, dev }' > iscan.txt
>the lines are joined.

Yes, it should do that. Alternately you can use printf then print:
{ printf "%s", $0; getline; print $0 }