Thursday, September 02, 2010

PERL REGULAR EXPRESSIONS: MATCHING TEXT WITH REGULAR EXPRESSIOS, ~M

In Perl, The “m” means to attempt a regular expression match. The requrlar expressions, or search patterns, are put between to forwardslashes, “/../”. And the string that to be searched is linked by” =~”. Truse, if you want to search any digits from a string, you can do the following:

my $varString="George Washington was born on February 22, 1732";
print "Search string \"$varString\" for digits\n";
while ( $varString=~ m/(\d+)/g) {
print "Found digits: $1\n";
}

And the output will be:

Search string "George Washington was born on February 22, 1732" for digits
Found digits: 22
Found digits: 1732

Here the search pattern is “/(\d+)/”, “m” means match and ‘=~” tells perl to link to the string “$varString”. Lastly, “g” means do a global search on the string (from left to right). The search resulf, is stored in “$1”.

If we changed the code a little bit, without using the while loop and without using global search “g”::

my $varString="George Washington was born on February 22, 1732";
print "Search string \"$varString\" for digits\n";
$varString=~ m/(\d+)/;
print "Found digits: $1\n";

The outout will be:

Search string "George Washington was born on February 22, 1732" for digits
Found digits: 22

Perl find the first digits and stop and the resulf still store in $1.

Now, assuming that we forgot to use “~” and have the codes as the following:

my $varString="George Washington was born on February 22, 1732";
print "Search string \"$varString\" for digits\n";
$varString= m/(\d+)/;
print "Found digits: $1\n";

The output are:

Search string "George Washington was born on February 22, 1732" for digits
Use of uninitialized value in pattern match (m//) at ./perl_rex.pl line 8.
Use of uninitialized value in concatenation (.) or string at ./perl_rex.pl line 9.
Found digits:

The reason is when the “~” is omitted, perl is looking for a match of the regex in $_ and stores the search resulf to $varString.
Lets test it using the following codes:

my $varString="George Washington was born on February 22, 1732";
print "Search string \"$varString\" for digits\n";
$_="George Washington was born on February 22, 1732";
$varString= m/(\d+)/;
print "Found digits: $1\n";
print "Found digits: $varString\n";

And the output are:

Search string "George Washington was born on February 22, 1732" for digits
Found digits: 22
Found digits: 22

In fact, “m” is not reuested as long as you have used “~”, but using “~m” make the codes a bit easy to read, in my opions.

my $varString="George Washington was born on February 22, 1732";
print "Search string \"$varString\" for digits\n";
$varString=~ /(\d+)/;
print "Found digits: $1\n";

References:


ORACLE ERROR: ORA-01031: INSUFFICIENT PRIVILEGES

ORA-01031: INSUFFICIENT PRIVILEGES (ORACLE  LINUX)

When tried to sarted oracle, I got the following error:

ERROR: ORA-01031: insufficient privileges

Following were the steps that I used to start the database instance:

1) Start sqlplus with nolog

# > sqlplus /nolog
SQL*Plus: Release 10.2.0.1.0 - Production on Thu Aug 12 14:40:02 2010
(c) 1982, 2005, Oracle. All rights reserved.
SQL>

2) Connect to oracle as sysdba

SQL> connect sys/password as sysdba;

ERROR:
ORA-01031: insufficient privileges

I hecked around and it turned out the problem was the parameter setting in the sqlnet.ora file.
In my case, the sqlnet.ora file (stored in the $ORACLE_HOME/network/admin directory) contained the following line:

SQLNET.AUTHENTICATION_SERVICES= (NTS)

I changed it to:

SQLNET.AUTHENTICATION_SERVICES= (ALL)

And, I was able to connect to oracle using "connect sys/password as sysdba" and stated the database instance and open the database.

This pointed me to look at the SQLNET.AUTHENTICATION_SERVICES again.

The parameter can set to :

• NONE : cannot connect to database without a password as sysdba (sqlplus /as sysdba)
• NTS: set for using windows NT native authentication
• ALL: ALL for all authentication methods

Authentication Methods Available with Oracle Advanced Security:

• kerberos5 for Kerberos authentication
• cybersafe for Cybersafe authentication

• radius for RADIUS authentication
• dcegssapi for DCE GSSAPI authentication

If authentication has been installed, it is recommended that this parameter be set to either none or to one of the authentication methods.

References:


LINUX SED: APPEND, INSERT AND WRITE TO A FILE FROM A FILE USING SED

APPEND, INSERT AND WRITE LINES TO A FILE FROM A FILE USING SED

This is the first note on Text Manipulations Using Linux Sed.

This note covers how to using Linux sed command to do simple append, insert, and write lines to an output file using a text file as input. Sed provides lot of commands to perform number of operations with the lines in a file. This comes handy in situations that we want to use a script to update files.

1 Sed Dose Not Change Input file.

"SED" stands for Stream EDitor. Sed reads its input from stdin ("standard input," i.e., the console or pipe), or from files, and writes its results to stdout (“standard output”, i.e., the screen). Therefore, sed doesn't modify any input files. Sed takes any number of user-specified editing operations ("commands") and performed on each line in order on the input data.

2 Sed command syntax and option switches.

Sed commands need to be put inside a pair of sigle quotes ‘’. All text lines that involve append, insert and replace need to be preceded by a “backslash” ( “\”). Make sure that there is no space between the command switch and the “\”. For example, the following command adds a line “add this line after the 3rd line”, to the input file inputfile.txt:

#sed ‘3 a\add this line after the 3rd line’ inputfile.txt 

Following are option switches and commands that use in this note. There is more information on sed can be found in the man page here.

Options:
• -n, --quiet, --silent suppress automatic printing of pattern space
• -e, script, --expression=script add the script to the commands to be executed
• -f, script-file, --file=script-file add the contents of script-file to the commands to be executed
Commands:
• P – Upper case P. Print up to the first embedded newline of the current pattern space.
• p – Lower case p. Print the current pattern space.
• = -- print current line number.
• W filename – Uper case W. Write the first line of the current pattern space to filename.
• w filename – Lower case w. Write the current pattern space to filename.
• a \text – Append text, which has each embedded newline preceded by a backslash.
• i \text – Insert text, which has each embedded newline preceded by a backslash.
• c\ text -- Replace the selected lines with text, which has each embedded newline preceded by a backslash.
• D – Upper case D. Delete up to the first embedded newline in the pattern space. Start next cycle, but skip reading from the input if there is still data in the pattern space.
• d – Delete pattern space. Start next cycle.

To test each of the commands and options, let’s use the following files as an example:

# cat sed_edit.txt
New York, Big Apple
Washington DC, The Capital
New Jersey, Garden Stata
Paris, The City of Light
Hong Kong, Pearl of the Orient

3 Sed Append
3.1 Use Sed ‘a\’ to appends a line after every line with the address

Following is the Syntax to append a line with address:

#sed 'ADDRESS a\
  Line which you want to append' filename
‘ADDRESS’ is a number with represents the line, i.e., 3, represent the 3rd line.

Here are some examples using sed append with address command:

• Example 1. Add a line after the 3rd line of the file.
Add the line “San Francisco, Golden City” after the 3rd line.

#sed ‘3 a\San Francisco, Golden City’ sed_edit.txt
New York, Big Apple
Washington DC, The Capital
New Jersey, Garden Stata
San Francisco, Golden City
Paris, The City of Light
Hong Kong, Pearl of the Orient

• Example 2: Add a line at the end of the file.
Sed also works with regurlar expression. The following command add the line “San Francisco, Golden City” at the end of the file

#sed ‘$,a\San Francisco, Golden City’ sed_edit.txt
New York, Big Apple
Washington DC, The Capital
New Jersey, Garden Stata
Paris, The City of Light
Hong Kong, Pearl of the Orient
San Francisco, Golden City

Here, the ‘$’ is the regurlar expression for end of file.

3.2 Use Sed ‘a\’ to appends a line after every line that match with a pathern

Following is the Syntax to append a line with a match of a pattern:

#sed '/PATTERN/ a\
Line which you want to append' filename

‘/PATTERN/’ is the pattern that wants match,i.e., /Paris/. Pattern need to be put between two forwardslashes,”/”.
Here is an examples using sed append command with a match of pattern:

• Append a line,” San Francisco, Golden City”, after a match of pattern “Paris”

#sed '/Paris/ a\San Francisco, Golden City' sed_edit.txt
New York, Big Apple
Washington DC, The Capital
New Jersey, Garden Stata
Paris, The City of Light
San Francisco, Golden City
Hong Kong, Pearl of the Orient

4 Sed Insert
The different between sed append and sed insert is that sed append adds the line after the address or match of pattern while sed insert adds the line before the address or match of the parttern.

4.1 Use Sed ‘i\’ to insert a line before every line with the address
Following is the Syntax to append a line with address:

#sed 'ADDRESS a\
Line which you want to append' filename

‘ADDRESS’ is a number with represents the line, i.e., 3, represent the 3rd line.

Here are some examples using sed insert to insert a line before an address.

• Example 1: Insert a line before the 3rd line of the file.
Insert the line “San Francisco, Golden City” before the 3rd line.

# sed '3 i\San Francisco, Golden City' sed_edit.txt
New York, Big Apple
Washington DC, The Capital
San Francisco, Golden City
New Jersey, Garden Stata
Paris, The City of Light
Hong Kong, Pearl of the Orient

• Example 2: insert a line at the beginning of the file.
Insert the line “San Francisco, Golden City” at the beginning of the file.

#sed '1 i\San Francisco, Golden City' sed_edit.txt
San Francisco, Golden City
New York, Big Apple
Washington DC, The Capital
New Jersey, Garden Stata
Paris, The City of Light
Hong Kong, Pearl of the Orient

Here address “1” represents the first line of the file.

4.2 Use Sed ‘i\’ to insert a line before every line that match with a pathern

Following is the Syntax to append a line with a match of a pattern:

#sed '/PATTERN/ i\
Line which you want to append' filename

Here is an example using sed insert to insert a line to a file.
• Insert a line,” San Francisco, Golden City”, before each match of pattern “Paris”

#sed '/Paris/ i\San Francisco, Golden City' sed_edit.txt
New York, Big Apple
Washington DC, The Capital
New Jersey, Garden Stata
San Francisco, Golden City
Paris, The City of Light
Hong Kong, Pearl of the Orient


5 Sed Write To a File
 Following is the Syntax to write to a file with address and pathern:

#sed 'ADDERSSw outputfilename' inputfilename

#sed '/PATTERN/w outputfilename' inputfilename
To store output of the above example, we can do the following:

#sed -n '/Washington/,/Paris/w sed_edit.out' sed_edit.txt

 Sed_edit.out will contain the output of the sed command.

However, to save a copy of the file that after insert,delete or update(replace) you may find it easier just to use the “>outputfilename” syntax.

For example, to save the changes make to the input file with the replace command, we can do:

#sed '/New Jersey, Garden Stata/ c\New Jersey, Garden State' sed_edit.txt > sed_edit.out

References: