replace name with another keywork in qlikview script - replace

Here i want to ask i have this line in qlivkiew script in that i want if there is "abc" or "def" available then i want to consider these as XYZ and if not available then want to consider as "NONE"
if(if(Filename = '1.xlsx','DR',
if(Filename = '2.xlsx','DR',
if(Filename = '3.xlsx','DRD',
if(Filename = '4.xlsx', 'DRD',
ApplyMap('Segm',Ext.,RGro') = 'abc' or 'def', 'XYZ','None') as In_Flag
how i modified this line according to my requirement
how i do that
any idea about this condition ?

You need to repeat the test to use the or
if(if(Filename = '1.xlsx','DR',
if(Filename = '2.xlsx','DR',
if(Filename = '3.xlsx','DRD',
if(Filename = '4.xlsx', 'DRD',
ApplyMap('Segm',Ext.,RGro') = 'abc' or ApplyMap('Segm',Ext.,RGro') = 'def','XYZ','None') as In_Flag
otherwise match can work
match(ApplyMap('Segm',Ext.,RGro'),'abc','xyz')

Related

Target child pages with regex

I need an regex which target all child pages of a certain group of parent pages, but NOT the parent pages them selfes.
To be more specific, I need an expression which targets:
/categoryA/XXX
/categoryB/YYY
/categoryC/ZZZ
But I do not want to include
/categoryA/
/categoryB/
/categoryC/
All help much appreciated!
Gustav
Try this one:
\/(\w+)\/([a-zA-Z]+)
I am assuming that the strings after the categories use letters only.
Input:
/categoryA/XXX
/categoryB/YYY
/categoryC/ZZZ
/categoryA/
/categoryB/
/categoryC/
Matches:
/categoryA/XXX
/categoryB/YYY
/categoryC/ZZZ
This one
([^\/]+$)
targets everything after the last slash
You could use this in an if() statement to filter out what you need, if I understand the question correctly.
Or this one:
\/category[A-Z]\/(.*)
In C#
childpage = Regex.Match(target, "/category[A-Z]/(.*)").Groups[1].Value;
In JavaScript
var myregexp = /\/category[A-Z]\/(.*)/;
var match = myregexp.exec(target);
if (match != null) {
childpage = match[1];
} else {
childpage = "";
}
In PHP
if (preg_match('%/category[A-Z]/(.*)%', $target, $groups)) {
$childpage = $groups[1];
} else {
$childpage = "";
}
In PowerShell
if ($target -match '/category[A-Z]/(.*)') {
$childpage = $matches[1]
} else {
$childpage = ''
}
In Python
match = re.search("/category[A-Z]/(.*)", target)
if match:
childpage = match.group(1)
else:
childpage = ""

ucwords() is not capitalizing the field text with UPDATE query

i have a name filed which has all the words in lower case. I want to update field as first letter as capital of all words of complete table at once.
im trying the below code which is updating/replacing the field for all rows:
$queryP = "madinah hi madinah";
$queryD = ucwords($queryP);
$pupdt = mysql_query("
update media_detail
set test = '$queryD'
");
and the below is not working:
$queryP = $row['unique_name'];
$queryD = ucwords($queryP);
$pupdt = mysql_query("
update media_detail
set test = '$queryD'
");
please help im not getting it to be working.
Your code is correct except that you should do updates in the loop. I also added the data retrieval step (that you probably omitted for brevity) which precedes the loop. Here is the complete code:
$sql = "SELECT id, unique_name FROM media_detail";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
$id = $row['id'];
$queryP = ucwords($row['unique_name']);
$pupdt = mysql_query("
UPDATE media_detail
SET test = $queryP
WHERE id = $id
");
$conn->query($pupdt);
}

Perl string parsing different variation

Experts... I'm creating another question in continuation with my earlier query.... this question is different from earlier request so I thought better to create new thread instead of confusing experts answers.
Below code connects each alias in tnsfile to the database...
1. Is there anyway I can limit each database connection only once and don't allow different alias connecting to the same database again?
I tried using hash but no able to fix this..
use strict;
use warnings;
if /^(([A-Za-z][A-Za-z0-9]*)(\.([A-Za-z][A-Za-z0-9]*))*)(\s|,|=)/
{
{
$hashref->{$1}="";
}
}
Below regex can select each SID value from file but not able to combine with if...
(/\(CONNECT_DATA\s+=\s+\(SID\s+=\s+(\w+\d+?)(\s+)?\)/)
Sample file (tnsfile.txt)
DB1.UK, DB2.UK =
(
(ADDRESS = (PROTOCAL = TCP))
(CONNECT_DATA = (SID = db1))
)
DB1.EU, DB2.CH =
(
(ADDRESS = (PROTOCAL = TCP))
(CONNECT_DATA = (SID = db1))
)
DB3.UK =
(
(ADDRESS = (PROTOCAL = TCP))
(CONNECT_DATA = (SID = db3))
)
DB3.US =
(
(ADDRESS = (PROTOCAL = TCP))
(CONNECT_DATA = (SID = db3))
)
DB4.UK.US, DB4.US =
(
(ADDRESS = (PROTOCAL = TCP))
(CONNECT_DATA = (SID = db4))
)
Expected $hashref value:
DB1.UK
DB3.UK
DB4.UK.US
Are you caching your database handles? It sounds like you are.
If so, you just need to create a new hash that relates SID to database handles. Then you only optionally create a new handle if that SID hasn't already been loaded.
In psuedocode:
my %dbh_by_sid;
while (<DATA>) {
my $sid = ...;
my $dbh = $dbh_by_sid{$sid} ||= DBI->connect(...)
or die "DB connect failed: $DBI::errstr";
# ...
}
This way if that particular SID has already been connected to, it will reuse the same handle.
This is far from a refined solution, and it breaks if the format of your tns file changes dramatically, but you could try something like this:
use strict;
my (%tns, %sid, $tns);
open IN, 'tnsfile.txt' or die;
while (<IN>) {
if (/^([\w.]+)/) {
($tns) = $1;
}
if (/SID *= *([\w.]+)/) {
$tns{$tns} = $1 unless ($sid{$1}++)
}
}
close IN;
print join "\n", keys %tns;

Need the Groovy way to do partial file substitutions

I have a file that I need to modify. The part I need to modify (not the entire file), is similar to the properties shown below. The problem is that I only need to replace part of the "value", the "ConfigurablePart" if you will. I receive this file so can not control it's format.
alpha.beta.gamma.1 = constantPart1ConfigurablePart1
alpha.beta.gamma.2 = constantPart2ConfigurablePart2
alpha.beta.gamma.3 = constantPart3ConfigurablePart3
I made this work this way, though I know it is really bad!
def updateFile(String pattern, String updatedValue) {
def myFile = new File(".", "inputs/fileInherited.txt")
StringBuffer updatedFileText = new StringBuffer()
def ls = System.getProperty('line.separator')
myFile.eachLine{ line ->
def regex = Pattern.compile(/$pattern/)
def m = (line =~ regex)
if (m.matches()) {
def buf = new StringBuffer(line)
buf.replace(m.start(1), m.end(1), updatedValue)
line = buf.toString()
}
println line
updatedFileText.append(line).append(ls)
}
myFile.write(updatedFileText.toString())
}
The passed in pattern is required to contain a group that is substituted in the StringBuffer. Does anyone know how this should really be done in Groovy?
EDIT -- to define the expected output
The file that contains the example lines needs to be updated such that the "ConfigurablePart" of each line is replaced with the updated text provided. For my ugly solution, I would need to call the method 3 times, once to replace ConfigurablePart1, once for ConfigurablePart2, and finally for ConfigurablePart3. There is likely a better approach to this too!!!
*UPDATED -- Answer that did what I really needed *
In case others ever hit a similar issue, the groovy code improvements I asked about are best reflected in the accepted answer. However, for my problem that did not quite solve my issues. As I needed to substitute only a portion of the matched lines, I needed to use back-references and groups. The only way I could make this work was to define a three-part regEx like:
(.*)(matchThisPart)(.*)
Once that was done, I was able to use:
it.replaceAdd(~/$pattern/, "\$1$replacement\$3")
Thanks to both replies - each helped me out a lot!
It can be made more verbose with the use of closure as args. Here is how this can be done:
//abc.txt
abc.item.1 = someDummyItem1
abc.item.2 = someDummyItem2
abc.item.3 = someDummyItem3
alpha.beta.gamma.1 = constantPart1ConfigurablePart1
alpha.beta.gamma.2 = constantPart2ConfigurablePart2
alpha.beta.gamma.3 = constantPart3ConfigurablePart3
abc.item.4 = someDummyItem4
abc.item.5 = someDummyItem5
abc.item.6 = someDummyItem6
Groovy Code:-
//Replace the pattern in file and write to file sequentially.
def replacePatternInFile(file, Closure replaceText) {
file.write(replaceText(file.text))
}
def file = new File('abc.txt')
def patternToFind = ~/ConfigurablePart/
def patternToReplace = 'NewItem'
//Call the method
replacePatternInFile(file){
it.replaceAll(patternToFind, patternToReplace)
}
println file.getText()
//Prints:
abc.item.1 = someDummyItem1
abc.item.2 = someDummyItem2
abc.item.3 = someDummyItem3
alpha.beta.gamma.1 = constantPart1NewItem1
alpha.beta.gamma.2 = constantPart2NewItem2
alpha.beta.gamma.3 = constantPart3NewItem3
abc.item.4 = someDummyItem4
abc.item.5 = someDummyItem5
abc.item.6 = someDummyItem6
Confirm file abc.txt. I have not used the method updateFile() as done by you, but you can very well parameterize as below:-
def updateFile(file, patternToFind, patternToReplace){
replacePatternInFile(file){
it.replaceAll(patternToFind, patternToReplace)
}
}
For a quick answer I'd just go this route:
patterns = [pattern1 : constantPart1ConfigurablePart1,
pattern2 : constantPart2ConfigurablePart2,
pattern3 : constantPart3ConfigurablePart3]
def myFile = new File(".", "inputs/fileInherited.txt")
StringBuffer updatedFileText = new StringBuffer()
def ls = System.getProperty('line.separator')
myFile.eachLine{ line ->
patterns.each { pattern, replacement ->
line = line.replaceAll(pattern, replacement)
}
println line
updatedFileText.append(line).append(ls)
}
myFile.write(updatedFileText.toString())

Find guids between strings

Problem:
Trying to get all matches where guid = guid. I'm expecting to receive a collection of matches where one match looks like:
{9659BAE5-632F-4195-BD5D-414C1F2C1066} = {6E298F2A-129A-4491-B053-F12D67561572}
I'm trying to match all of the guid = guid between GlobalSection(NestedProjects) = preSolution and EndGlobalSection specifically. There are other places in the file where guid = guid exists.
Here is a data snippet:
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client", "Applications\", "{297BE1A3-A6A3-4835-BB87-63B4B4E2AE0D}"
ProjectSection(ProjectDependencies) = postProject
{A459406A-94FF-4CA9-8183-C7472419CC7D} = {A459406A-94FF-4CA9-8183-C7472419CC7D}
EndProjectSection
EndProject
GlobalSection(NestedProjects) = preSolution
{3D84A2B1-536D-4953-B331-D86E421905E7} = {AF46FD2E-710D-49CD-A203-CB0F8B7EF415}
{02CB05EC-6902-417E-AD50-B3910B245B22} = {2F54A6F1-5D32-4673-8AEE-B845CC622D64}
{DE303EF0-E3B1-4BA9-8CB3-544D37D29576} = {2F54A6F1-5D32-4673-8AEE-B845CC622D64}
{5A095236-0EE1-4480-B7A6-833ECCFE4257} = {AF070137-227F-42F7-9487-00CB26C46E04}
{6CCA189C-0D45-4E80-8486-38AB3E625E69} = {AF070137-227F-42F7-9487-00CB26C46E04}
{EAE3152A-C003-4E39-BFB7-B4F7CACE1606} = {AF070137-227F-42F7-9487-00CB26C46E04}
{9659BAE5-632F-4195-BD5D-414C1F2C1066} = {6E298F2A-129A-4491-B053-F12D67561572}
EndGlobalSection
EndGlobal
What I've Tried:
Here's what I'm using to match guid = guid
{[A-Za-z0-9]{8}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{12}} = {[A-Za-z0-9]{8}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{12}}
This works fine except it doesn't discriminate against the location of the match, obviously. So I receive other matches from other parts of the file.
I've been trying to use a positive look behind like so (with many variations):
(?<=GlobalSection\(NestedProjects\) = preSolution(\r\n|.)+?)
Am I misusing the lookbehind or something else?
I tried the following regex and got correct results using your example.
(?<= GlobalSection\(NestedProjects\) = preSolution(\r\n|.)+?){[A-Za-z0-9]{8}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{12}} = {[A-Za-z0-9]{8}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{12}}
You may need to use RegexOptions.Multiline as an option in your regex since it is C# (if you're not already).
EDIT
I wrote a little test program with your data snippet. I doubled it to make sure it did not just match the first group after GlobalSection(NestedProjects) = preSolution
matches only returned the GUIDs between GlobalSection(NestedProjects) = preSolution and EndGlobalSection for both sections.
The line {A459406A-94FF-4CA9-8183-C7472419CC7D} = {A459406A-94FF-4CA9-8183-C7472419CC7D} was not in the matched results as I would expect. I hope something in this code helps you out.
static void Main(string[] args)
{
string input = System.IO.File.ReadAllText(#"c:\test\directory\test.txt");
string pattern =
#"(?<=GlobalSection\(NestedProjects\) = preSolution(\r\n|.)+?){[A-Za-z0-9]{8}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{12}} = {[A-Za-z0-9]{8}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{12}}";
Regex re = new Regex(pattern, RegexOptions.Multiline);
MatchCollection matches = re.Matches(input);
foreach (var match in matches)
{
Console.Write(match);
}
}