in reply to String searching in file
Using perl, grep on the lines in the file not the filename. For example (using test data instead of your command output)
poj#!/usr/bin/perl use warnings; use strict; my $awsLists = '/ansible/awsLists'; my @awsLists = (); if (-e $awsLists){ open my $fh,'<',$awsLists or die "$!"; @awsLists = <$fh>; close $fh; } my $cmd = 'aws ec2 describe-instances --filters "Name=instance-state-n +ame,Values=running" | grep PrivateDnsNam$'; my @output = <DATA>;#qx/$cmd/; open my $fh,'>>',$awsLists or die "$!"; foreach my $line (@output){ chomp($line); if (grep /$line/, @awsLists ){ print "$line found\n"; } else { print "$line not found - added\n"; print $fh $line."\n"; } } close $fh; __DATA__ test 1 test 89
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: String searching in file
by cbtshare (Monk) on Feb 09, 2018 at 22:06 UTC | |
|
Re^2: String searching in file
by cbtshare (Monk) on Feb 09, 2018 at 23:02 UTC |