Normally while working on PL/SQL stored procedures , we use
DBMS_TRACE for knowing about the values of various variables and put some print statements.
This looks fine until you use your stored procedures on cli. But Lets assume your stored procedure is being called from a different process,lets say a c++ or a Java process. In this case you dont have a way to send your log statements to a log file unless you have some admin permissions. But as a designer you might not have the rights with you.
This procedure below will present you a way to put traces in your PLSQL procedure and see the values.
Below is a shell script which provides you with that provision.
Simple idea is insert our trace statements in a table.
#!/bin/ksh
#Get the filename and filepath of the input files for MD process
CONNECT_STRING=${DATABASE}
if [ $1 = "create" ];then
CRE_RESULT=`sqlplus -s ${CONNECT_STRING} << EOF
create table DEBUG_MESSAGES(
current_date DATE,
message varchar2(500)
);
CREATE OR REPLACE PROCEDURE DEBUG_OUTPUT( p_text IN VARCHAR2 ) AS
BEGIN
insert into DEBUG_MESSAGES (current_date,message)
values (sysdate, p_text);
commit;
END;
exit;
EOF`
echo $CRE_RESULT
fi
if [ $1 = "clean" ];then
CLN_RESULT=`sqlplus -s ${CONNECT_STRING} << EOF
drop table DEBUG_MESSAGES;
drop procedure DEBUG_OUTPUT;
exit;
EOF`
echo $CLN_RESULT
fi
Now You can add your trace statements inside your procedure like below:
DEBUG_OUTPUT('Hello World!');
DEBUG_OUTPUT('Hello World!'||vMyVariable);
After you execute your procedure , your trace statements will be available in the table DEBUG_MESSAGES .
You can go there and check
Lets say I have C/C++ file which has some pattern in the line at the start. I also know that there will be 2 lines following the line that will match my pattern. I want to remove these three lines and add a new line which has a different string which is nothing but I want to replace a all those 3 three lines with a different line.
MTTRACE("ARG1",
"ARG2",
"ARG3");
//some code follows
MTTRACE("ARG1",
"ARG2",
"ARG3");
New fIle should look as below:
MYTRACE(ARG);
//some code follows
MYTRACE(ARG);
Solution:
perl -pe '$x=3 if /^\s*MTTRACE.*/;
if($x>0){$x-- and undef $_;$_="MYTRACE(ARG);\n" if $x==0}' myfile.cc
With the look-ahead and look-behind constructs ,you can "roll your own" zero-width assertions
to fit your needs. You can look forward or backward in the string being processed, and you can
require that a pattern match succeed (positive assertion) or fail (negative assertion) there.
Every extended pattern is written as a parenthetical group with a question mark as the first
character. The notation for the look-arounds is fairly mnemonic, but there are some other,
experimental patterns that are similar, so it is important to get all the characters in the
right order.
(?=pattern)
is a positive look-ahead assertion
(?!pattern)
is a negative look-ahead assertion
(?<=pattern)
is a positive look-behind assertion
(?<!pattern)
is a negative look-behind assertion
EXAMPLES
Look-Ahead:
echo $mytmp2
uvw_abc uvw_def uvw_acb
Positive:
echo $mytmp2 | perl -pe 's/uvw_(?=(abc|def))/xyz_/g'
xyz_abc xyz_def uvw_acb
Description: replace every occurance of uvw_ with xyz_ where uvw_ followed by abc or def
Negative:
echo $mytmp2 | perl -pe 's/uvw_(?!(abc|def))/xyz_/g'
uvw_abc uvw_def xyz_acb
Description: replace every occurance of uvw_ with xyz_ where uvw_ is not followed by abc or def
Look-Behind:
echo $mytmp
abc_uvw def_uvw acb_uvw
Positive:
echo $mytmp | perl -pe 's/(?<=(abc|def))_uvw/_xyz/g'
abc_xyz def_xyz acb_uvw
Description: replace every occurance of _uvw with _xyz where _uvw is preceeded by abc or def
Negative:
echo $mytmp | perl -pe 's/(?<!(abc|def))_uvw/_xyz/g'
abc_uvw def_uvw acb_xyz
Description: replace every occurance of _uvw with _xyz where _uvw is not preceeded by abc or def
Have you ever tried this. Dont go on writing big perl code for this. Here's a simple solution for this.
my @arr=split /\S+/,$str;
where
$str is your string
\s obviously matches a white space character. But \S matches a non white space character.
So \S+ matches atleast one non white space character.
This can be handy many a times when you are working on a C++ application.
There is a no direct method in the standard to do the same except when you are using a boost library.
Below is a simple function that I use regularly in my applications which comes in handy for me all the time
template<class T>
int inline findAndReplace(T& source, const T& find, const T& replace)
{
int num=0;
int fLen = find.size();
int rLen = replace.size();
for (int pos=0; (pos=source.find(find, pos))!=T::npos; pos+=rLen)
{
num++;
source.replace(pos, fLen, replace);
}
return num;
}
I have input file that look's like :
cellIdentity="42901"
cellIdentity="42902"
cellIdentity="42903"
cellIdentity="52904"
Numbers inside the quotes can be anything. The output needed is original line followed by the copy of same line
except the last digit of the number should be a series of 5,6,7. So the output should look like below:
cellIdentity="42901"
cellIdentity="42905"
cellIdentity="42902"
cellIdentity="42906"
cellIdentity="42903"
cellIdentity="42907"
cellIdentity="52904"
cellIdentity="52905"
Below is the Perl command that I have written.
perl -pe 'BEGIN{$n=4}$n++;
$n>7?$n=5:$n;
$a=$_;print $a;
s/(\d).$/$n."\""/ge'
This is an awk assignment given to one of my friend.
Its quite challenging.
We have two files:
File1:(List of companies)
Joe's Garage
Pip Co
Utility Muffin Research Kitchen
File2:(List of payments and dues of the companies in File1)
Pip Co $20.13 due
Pip Co $20.3 due
Utility Muffin Research Kitchen $2.56 due
Utility Muffin Research Kitchen 2.56 due
Joe's Garage $120.28 due
Joe's Garage $100.24 payment
Now the challenge is we need to create an output file which states the total amount due by each company.
Additionally there is one more requirement where we need to handle the format errors in teh File2.
- The list of fomrat errors to be handled are:
- The dollor symbol not present in the amount
There should be exactly 2 decimals after the decimal point.
If any of the above format errors are encountered, then the complete line should be ignored and proceed to the next line.
The expected output here is:
Joe's Garage $20.04
Utility Muffin Research Kitchen $2.56
Pip Co $20.13
Below is the awk script that I have written for this. and its working at my side.
{
if(FNR==NR)
{
for(i=1;i<=NF;i++)
str=str","$i;
a[str]=1;str="";
next;
}
{
if($(NF-1)!~/^\$/)
{
print "Format Error!-No dollor sign"FNR,FILENAME,$(NF-1);
next;
}
if($(NF-1)!~/\.[0-9][0-9]$/)
{
print "Format Error!-should have 2 digits after a decimal point"FNR,FILENAME,$(NF-1);
next;
}
for(i=1;i<(NF-1);i++)str=str","$i;
if(a[str]){
gsub(/\$/,"",$(NF-1));
if($NF~/payment/){
a[str]-=$(NF-1);}
else if($NF~/due/){
a[str]+=$(NF-1);}
}
str="";
}
}
END{
for(i in a)
{
t=i;
gsub(/,/," ",t);
print t,"$"(a[i]-1);
}
}
I am sure that this can be optimized. I put it long so that its more convincing to all.
Below is the way we have to execute this. I am using nawk on solaris.Others can use awk itself.
Copy the above code in a file and name it as mycode.awk and then execute the awk command as below:
nawk -f mycode.awk File1 File2
Out that I have got with the above command is:
> nawk -f temp.awk temp2 temp1
Format Error!-should have 2 digits after a decimal point2 temp1 $20.3
Format Error!-No dollor sign4 temp1 2.56
Joe's Garage $20.04
Utility Muffin Research Kitchen $2.56
Pip Co $20.13
>
Let's say I have a input file which looks like below:
Apr 24 2014;
is;
a;
sample;
;
Jun 24 2014 123;
may 25 2014;
is;
b;
sample;
;
Dec 21 2014 987
I want to merge 6 lines at a time. Which means my output should look like:
Apr 24 2014;is;a;sample;;Jun 24 2014 123
may 25 2014;is;b;sample;;Dec 21 2014 987
Below is a simple command that I would use:
awk '{ORS=(NR%6?"":RS)}1' file
Explanation:
By doing,
ORS=(NR%6?"":RS)
I am setting the output record separator to actual record separator only if line number is a multiple of 6.
In general if there is a need for us to iterate though a string character by character, then we normally split the string using a statement like:
@chars=split("",$var);
Now after the array is created we iterate through that array.But an easy way of doing this in Perl without creating an array is :
while ($var =~ /(.)/sg) {
my $char = $1;
print $char."\n"
}
Below is the explanation for the same:
$var =~ /(.)/sg
Match any character though out the string and round braces "()" captures the matched character.
/s
Treat string as single line. That is, change "." to match any character whatsoever, even a newline, which normally it would not match.
/g
Match all occurrences of the regexp throughout the line instead of only the first occurrence.
I recently came to know about this and I thought its worth sharing. I will try to keep it very simple.
Lets say I have a file as below:
1
2
3
4
5
I need to join all the lines with a pipe so that my output should look like below:
1|2|3|4|5
Normally I use the below command to achieve the same:
perl -lne 'push @a,$_;END{print join "|",@a}' File
Now, here's another option for you below:
perl -lne 'push @a,$_;}{ print join "|",@a' File
The change here is:
}{
This is called butterfly option in perl. Basically it closes while loop imposed by -n switch, and what follows }{ is block executed after while loop.
There are many different way to do this. You might write your own once you figure out what has to be done exactly. But I came up with this. Additionally I wanted the input to be numeric as well. So below is what I came up with.
private class NumericAndLengthFilter extends DocumentFilter {
/**
* Number of characters allowed.
*/
private int length = 0;
/**
* Restricts the number of charcacters can be entered by given length.
* @param length Number of characters allowed.
*/
public NumericAndLengthFilter(int length) {
this.length = length;
}
@Override
public void insertString(FilterBypass fb, int offset, String string,
AttributeSet attr) throws
BadLocationException {
if (isNumeric(string)) {
if (this.length > 0 && fb.getDocument().getLength() + string.
length()
> this.length) {
return;
}
super.insertString(fb, offset, string, attr);
}
}
@Override
public void replace(FilterBypass fb, int offset, int length, String text,
AttributeSet attrs) throws
BadLocationException {
if (isNumeric(text)) {
if (this.length > 0 && fb.getDocument().getLength() + text.
length()
> this.length) {
return;
}
super.insertString(fb, offset, text, attrs);
}
}
/**
* This method tests whether given text can be represented as number.
* This method can be enhanced further for specific needs.
* @param text Input text.
* @return {@code true} if given string can be converted to number; otherwise returns {@code false}.
*/
private boolean isNumeric(String text) {
if (text == null || text.trim().equals("")) {
return false;
}
for (int iCount = 0; iCount < text.length(); iCount++) {
if (!Character.isDigit(text.charAt(iCount))) {
return false;
}
}
return true;
}
}
}
This time I came across a slightly different way than extending the documentfilter.
That is I will write my own plain document here.
class IntegerRangeDocument extends PlainDocument {
int minimum, maximum;
int currentValue = 0;
public IntegerRangeDocument(int minimum, int maximum) {
this.minimum = minimum;
this.maximum = maximum;
}
public int getValue() {
return currentValue;
}
public void insertString(int offset, String string, AttributeSet attributes)
throws BadLocationException {
if (string == null) {
return;
} else {
String newValue;
int length = getLength();
if (length == 0) {
newValue = string;
} else {
String currentContent = getText(0, length);
StringBuffer currentBuffer = new StringBuffer(currentContent);
currentBuffer.insert(offset, string);
newValue = currentBuffer.toString();
}
try {
currentValue = checkInput(newValue);
super.insertString(offset, string, attributes);
} catch (Exception exception) {
Toolkit.getDefaultToolkit().beep();
}
}
}
public void remove(int offset, int length) throws BadLocationException {
int currentLength = getLength();
String currentContent = getText(0, currentLength);
String before = currentContent.substring(0, offset);
String after = currentContent.substring(length + offset, currentLength);
String newValue = before + after;
try {
currentValue = checkInput(newValue);
super.remove(offset, length);
} catch (Exception exception) {
Toolkit.getDefaultToolkit().beep();
}
}
public int checkInput(String proposedValue) throws NumberFormatException {
int newValue = 0;
if (proposedValue.length() > 0) {
newValue = Integer.parseInt(proposedValue);
}
if ((minimum <= newValue) && (newValue <= maximum)) {
return newValue;
} else {
throw new NumberFormatException();
}
}
}
Now you can attach your text field with this plain document as below to achieve our requirement.
Document rangeOne = new IntegerRangeDocument(0, 255);
JTextField textFieldOne = new JTextField();
textFieldOne.setDocument(rangeOne);
This is slightly opposite to what we have seen in one of the previous posts
Please use the below DocumentFilter for the same.
class MyDocFilter extends DocumentFilter {
private static final String REMOVE_REGEX = "\\d";
private boolean filter = true;
public boolean isFilter() {
return filter;
}
public void setFilter(boolean filter) {
this.filter = filter;
}
@Override
public void insertString(FilterBypass fb, int offset, String text,
AttributeSet attr) throws BadLocationException {
if (filter) {
text = text.replaceAll(REMOVE_REGEX, "");
}
super.insertString(fb, offset, text, attr);
}
@Override
public void replace(FilterBypass fb, int offset, int length, String text,
AttributeSet attrs) throws BadLocationException {
if (filter) {
text = text.replaceAll(REMOVE_REGEX, "");
}
super.replace(fb, offset, length, text, attrs);
}
}
I have read a lot for this and the best way I found to do this is by extending the DocumentFilter class and using this new DocumentFilter
class MyIntFilter extends DocumentFilter {
@Override
public void insertString(FilterBypass fb, int offset, String string,
AttributeSet attr) throws BadLocationException {
Document doc = fb.getDocument();
StringBuilder sb = new StringBuilder();
sb.append(doc.getText(0, doc.getLength()));
sb.insert(offset, string);
if (test(sb.toString())) {
super.insertString(fb, offset, string, attr);
} else {
// warn the user and don't allow the insert
}
}
private boolean test(String text) {
try {
Integer.parseInt(text);
return true;
} catch (NumberFormatException e) {
return false;
}
}
@Override
public void replace(FilterBypass fb, int offset, int length, String text,
AttributeSet attrs) throws BadLocationException {
Document doc = fb.getDocument();
StringBuilder sb = new StringBuilder();
sb.append(doc.getText(0, doc.getLength()));
sb.replace(offset, offset + length, text);
if (test(sb.toString())) {
super.replace(fb, offset, length, text, attrs);
} else {
// warn the user and don't allow the insert
}
}
@Override
public void remove(FilterBypass fb, int offset, int length)
throws BadLocationException {
Document doc = fb.getDocument();
StringBuilder sb = new StringBuilder();
sb.append(doc.getText(0, doc.getLength()));
sb.delete(offset, offset + length);
if (test(sb.toString())) {
super.remove(fb, offset, length);
} else {
// warn the user and don't allow the insert
}
}
}
Now after this use this filter with your JtextField like below:
PlainDocument doc = (PlainDocument) textField.getDocument();
doc.setDocumentFilter(new MyIntFilter());
I have a list of strings in a file separated by a new line.
for example:
input.txt
temp1
temp2
temp3
Now I have a directory with multiple dat files like:
>ls -1 *.dat
one.dat
two.dat
three.dat
And many more dat like like above with random names.
Now I want to search for all the strings in input.txt in all the dat files present in directory(let's say current working directory).This is what I came up with:
create a perl script given below and name it as anything you wish(I named here as temp.pl).place the file input.txt in the current working directory.
#!/usr/bin/perl -w
open (INP,"input.txt") or die $!;
while(<INP>)
{
my $cmd="find . -name \"*.dat\"|xargs grep -w -i $_";
my $output=`$cmd`;
if($output!~/^\s*$/)
{
print $_."\n";
print "------------------\n";
print $output."\n";
print "-------------------\n";
}
}
exit;
Run this script as :
>./temp.pl
This solved my need.I hope it solves yours too :)
Below is a implementation of Graph Data Structure in C++ as Adjacency List.
I have used STL vector for representation of vertices and STL pair for denoting edge and destination vertex.
struct vertex{
typedef pair ve;
vector adj; //cost of edge, destination vertex
string name;
vertex(string s)
{
name=s;
}
};
class graph
{
public:
typedef map vmap;
vmap work;
void addvertex(const string&);
void addedge(const string& from, const string& to, double cost);
};
void graph::addvertex(const string &name)
{
vmap::iterator itr=work.begin();
itr=work.find(name);
if(itr==work.end())
{
vertex *v;
v= new vertex(name);
work[name]=v;
return;
}
cout<<"\nVertex already exists!";
}
void graph::addedge(const string& from, const string& to, double cost)
{
vertex *f=(work.find(from)->second);
vertex *t=(work.find(to)->second);
pair edge = make_pair(cost,t);
f->adj.push_back(edge);
}
There are lot of ways where one can identify memory leaks and lots of blogs and materieals
are available to help a designer to identify the same.
Some say using dbx and others say use mdb or gdb. I agree that all those tools will be useful, but it takes a lot of effort for understanding as well as putting it to work takes some time.
I have thought to break the legacy rules and do something new with the knowledge that
I have on various tools of solaris. So I thought of giving it a try to use tools like
dtrace, perl, ps command etc along with c++.
Putting this toll to action is an effort less job.
I am happy to have created a tool which I think would be very use for any solaris unix programmer.
This tool will give the list of all memory leaks of a process running in the background.
It uses dtrace to identify the memory allocations,
It uses the ps command to identify the memory used by a process.
It uses perl to filter and generate reports of meory leaks.
Main functionalities of this tool are:
-
Generate a report which contains actual physical memory used by the process, Virtual memory used, % of cpu used and %memory used.
- Generate a report which contains all the memory leaks of the process due to inefficient coding techniques.
-
Generate a separate report for each and every process.
I have tested this on:
SunOS 5.10 Generic_147440-27 sun4u sparc SUNW,SPARC-Enterprise
Thanks to Frederic, I have taken part of his logic written in his blog:
You can download the zip file from the link here:
The file name is cpuleaks.gz.
Unzip this file using gzip -d cpuleaks.gz and you can place it on any server and give the execute permissions and run this process as a root user(dtrace can be run only using root access on my server. If dtrace can be used by a user other than root, well and fine).
Execution of the process should be as below:
cpuleaks <comma separated pid list> -st <time interval for mem usage>
ex: cpuleaks 1234,2341 -st 2
This above command will give 4 files as output after either the processes 1234,2341 complete or if you press CTRL +C(an interrupt signal).
1234.cpumem
1234.leaks
2341.cpumem
2341.leaks
where
-
.leaks report will have the list of all memory leaks
-
.cpumem report will have the memory usage of the process for every interval passed as an argument to the cpuleaks binary.
One of my colleagues asked this query to me. And I liked the query very much.
So I started thinking about that.
For example i have a below file:
1 name1
2 name2
3 name3
4 name1
5 name4
6 name2
7 name1
Now i want to find strings in the file , lets say in the order name1, name2, name3, name4.
And i also want the output to be in the same order in the way i searched as below:
1 name1
4 name1
7 name1
2 name2
6 name2
3 name3
5 name4
Obviously we cannot do this with a single grep command without any temporary files being created.
So I thought perl would be a better option for this. So I came up with a simple perl solution.
perl -lne '/name1/?push @a,$_:
(/name2/?push @b,$_:
(/name3/?push @c,$_:
/name4/?push @d,$_:next));
END{print join "\n",@a,@b,@c,@d}' your_file
And this worked like a a charm.