I have two parameters:
security.server.port=8443
security.authorization.enabled=true
I want every time to check those parameters in file
if one of them missing to add after the next one or opositive
or if they missing at all to added after "security.server.ip="
For example:
If we have in the file parameter:
security.authorization.enabled=true
Expected View:
security.server.port=8443
security.authorization.enabled=true
Not able to create whole process grep and add after the grep
Something like this might be what you're looking for:
awk '
BEGIN{
params["security.server.port=8443"]
params["security.authorization.enabled=true"]
}
{
for (param in params)
if ($0 == param)
params[param] = 1
print
}
END {
for (param in params)
if (params[param] != 1)
print param
}
' file
It's hard to say without more representative input and expected output.
Related
i have a array of filenames , and i have to traverse this array and check whether the content of ALL the files is empty.
Here is the code
foreach my $reportFile (sort { getDateInName($b) <=> getDateInName($a)} #ReportFiles)
{
my #fileData = readFile($reportFile);
if(!#fileData)
{
outputLog("FAIL: File Doesnt Contain Any Data.");
return;
}
}
But in the code above, iam returning even if a single file is empty,
I would like to know how can we check whether ALL the content of ALL files is empty and then return.
So I would like to return only if none of the files in the array has content.
Even if one file has content i wouldnt return
Thanks
Use List::Util::all:
Similar to any, except that it requires all elements of the #list to make the BLOCK return true. If any element returns false, then it returns false. If the BLOCK never returns false or the #list was empty then it returns true.
use List::Util 'all';
sub check_files {
...
warn( "All files empty" ), return
if all { -z } #_;
...
}
or
sub are_all_empty { all { -z } #_ }
First off, if you only want to check if a file is empty, you should not try to read it. This might be dangerous as you might ending up reading a huge file in memory. You can test for a file being empty like that if (-s $reportFile) {...}. Secondly, to fix the problem that you have with returning if any file is empty, you need to invert the logic of you code, i.e. you must check if any file is not empty. This is because of the following logical equivalence: saying that "all the files are empty" is the same as "no file is nonempty". Putting it all together, you get something like that:
sub all_empty {
foreach my $reportFile (sort { getDateInFileName($b) <=> getDateInName($a)} #ReportFiles)
{
if (-s $reportFile) {
return 0;
}
}
return 1;
}
Try to have a boolean flag, which is set to one if u have content in atleast one file, so that you do not have to traverse through the array and return as soon as u find a file with content
And if there is no content in the file, go to next iteration to check if the content is avaialble.
And come out of loop if content is present in any of the file
my $isFileEmpty = 0 ;
foreach my $reportFile (sort { getDateInFileName($b) <=> getDateInName($a)} #ReportFiles)
{
my #fileData = readFile($reportFile);
if(#fileData) {
$isFileEmpty = 1;
last;
}
else {
next ;
}
}
if($isFileEmpty eq 0)
{
return;
}
PS : Do you have content available in most of the cases ?
Is there any way to search a string only inside a function definition.
I mean to say suppose there is a c program file a.c , in which there is definition of several functions are present , but i want output of search only when that string present inside specific function ( lets say do_something()) definition, is there any way to search string like that, from command prompt?
for example , for following code:
#include <stdio.h>
void f(int n,
int j,
int k)
{
printf("name is is pankaj ");
printf("name is is kumar ");
printf("name is is mayank ");
}
int main()
{
printf("name is is pankaj ");
return 0;
}
for above program, I want only one occurrence of pankaj which is present in function f(), I don't want pankaj present in main function as output of search.
Please ignore any semantic or syntax error in program , my query is only for search of a string in program.
Of course, try this:
$0 ~ fun {
count = 1
while (! ($0 ~ /{/))
getline
getline
}
count > 0 {
if ($0 ~ /{/)
count++
if ($0 ~ /}/)
count--
if ($0 ~ query)
print FILENAME ": l" FNR ". " $0
}
And invoke the script like this:
awk -v query="pankaj" -v fun="void f[(]" -f script.awk inputfile.java
Where query is the string to search and fun the regex for the function name.
This script counts { and } to see when we leave the function and should print the line if a match is found.
Edit: you may want to extend the regex for counting brackets, perhaps an extra check to see if they aren't placed in comments is required (although you'd never do that).
I'm trying to visualize a formula which is in a single string of this format:
AND(operator1(operator2(x,a),operator3(y,b)),operator4(t))
There's no limit to how many arguments are in an operator.
Currently I'm trying to solve it in perl. I guess the easiest way is to transform it to a parent-child representation, and then it would be easy to present it in a collapsable way like this:
[-]AND(
[-]operator1(
[-]operator2(
x
a
)
[+]operator3(...)
)
[-]operator4(
t
)
)
I doubt i'm the first one to tackle this, but I can't find any such example online. Thanks!
I did something like this a few months ago and think this might be along the lines of what you are looking for...
c:\Perl>perl StackOverflow.pl
SomeValue
AND
-operator1
--operator2
---x
---a
--operator3
---y
---b
-operator4
--t
c:\Perl>
If so, here's the code (below). It's a bit of a mess, but it should be enough to get you started.
#!c:/perl/bin/perl.exe
my $SourceString="SomeValue AND (operator1(operator2(x,a),operator3(y,b)),operator4(t))";
my $NodeIndex=0;
my $IndexString="-";
print ProcessString($SourceString, $NodeIndex);
exit;
#----- subs go here -----
sub ProcessString {
my $TargetString=shift||return undef;
my $NodeIndex=shift;
my $ReturnString="";
#are we starting with a paren or comma?
if($TargetString=~m/^([\(\)\, ])/) {
#yep, delete the char and pass it through again for further processing
if($1 eq " ") {$ReturnString.=ProcessString(substr($TargetString, index($TargetString, $1)+1), $NodeIndex);}
elsif($1 eq ",") {$ReturnString.=ProcessString(substr($TargetString, index($TargetString, $1)+1), $NodeIndex);}
elsif($1 eq "(") {$ReturnString.=ProcessString(substr($TargetString, index($TargetString, $1)+1), ++$NodeIndex);}
elsif($1 eq ")") {$ReturnString.=ProcessString(substr($TargetString, index($TargetString, $1)+1), --$NodeIndex);}
} else {
#nope, must be a keyword or the end
if($TargetString=~m/([\(\)\, ])/) {
my $KeywordString=substr($TargetString, 0, index($TargetString, $1));
$ReturnString.=$IndexString x $NodeIndex;
$ReturnString.=$KeywordString."\n";
$ReturnString.=ProcessString(substr($TargetString, index($TargetString, $1)), $NodeIndex);
} else {
#we should never be here
} #end keyword check if
} #end if
#send the string back
return $ReturnString;
} #end sub
__END__
In my Perl Catalyst application, I get the value of a URL parameter like this, typically:
my $val = $c->request->params->{arg} || '';
But the URL could contain multiple arg=$Val. I only want to keep the first value of arg=. I could add this throughout my code:
my $val = $c->request->params->{arg} || '';
$val = $val->[0] if (ref($val) eq 'ARRAY');
That is rather ugly. Is there a way to pick up the first value or a url parameter in a better way?
Does your app actually expect multiple values for parameter arg? If not, all you need is
my $val = $c->request->params->{arg} || '';
Sure, it will be garbage if the user provides you with a garbage url, but there's nothing you can do to prevent the user from giving you garbage.
If it's actually valid to have more than one value for parameter arg, why would you want just the first value? You'd actually want all the values.
sub param_vals {
my ($params, $name) = #_;
return () if !exists($params->{name});
return $params->{$name} if !ref($params->{name});
return #{ $params->{$name} };
}
my #args = param_vals($c->request->{params}, 'arg');
I just read the code to Catalyst::Request but I don't see anything to always pull out a single value. Too bad Cat doesn't use something like Hash::MultiValue!
So I'm using a basic formmail script. Within the script I'm using a redirect variable. The value of the redirect is something like:
http://www.mysite.com/NewOLS_GCUK_EN/bling.aspx?BC=GCUK&IBC=CSEE&SIBC=CSEE
When the redirect action happens however, the URL appears in the browser as:
http://www.mysite.com/NewOLS_GCUK_EN/bling.aspx?BC=GCUK&IBC=CSEE&SIBC=CSEE
You can see the & characters are replaced with &
Is there any way to fix this?
Maybe you can edit the script with a string substitution:
$myRedirectURL =~ s/\&/\&/g;
Or perhaps look in the script where the opposite substitution is taking place, and comment out that step.
HTML::Entities's decode_entities could decode this for you:
$redirect_target = decode_entities($redirect_target);
But passing the destination URL as HTTP argument (e.g. hidden form field) is dangerous (as #Sinan Ünür already said in the comments). Better store the target URL within your script and pass a selector from the form:
if ($selector eq 'home') { $target_url = 'http://www.foo.bar/'; }
elsif ($selector eq 'bling') { $target_url = 'http://www.foo.bar/NewOLS_GCUK_EN/bling.aspx'; }
else {
$target_url = 'http://www.foo.bar/default.html'; # Fallback/default value
}
Using a Hash would be shorter:
my %targets = {
home => 'http://www.foo.bar/',
bling => '/NewOLS_GCUK_EN/bling.aspx',
};
$target_url = $targets{$selector} || '/default_feedback_thanks.html';