Operating System - OpenVMS
1820493 Members
2166 Online
109624 Solutions
New Discussion юеВ

search command search-string - how search for '<char>' ?

 
SOLVED
Go to solution
Jim Strehlow
Advisor

search command search-string - how search for '<char>' ?

How can you search files using a search-string with single quote some character single quote?

We have some software programs that generate log files which we must search. The target string includes not just one single quote and some specific character, but: "... 'A' "

How may we avoid DCL string substitution parsing to look for the text string 'A' in several files using the OpenVMS SEARCH command?

Thank you.
19 REPLIES 19
Steven Schweda
Honored Contributor

Re: search command search-string - how search for '<char>' ?

Either I'm dense, or else that wasn't
entirely clear.

What's wrong with this?:

alp $ type apo.txt
abcdefg
abc'defg
abcd'efg
abc'd'efg

alp $ sea a*.txt "'d'"

******************************
ALP$DKA0:[SMS]APO.TXT;1

abc'd'efg

Inside quotation marks (""), you need two
apostrophes ('') to get symbol evaluation.
Steven Schweda
Honored Contributor

Re: search command search-string - how search for '<char>' ?

Are "software programs" anything like
computer programs? (You have some time to
think this over. I need to go to the local
Home Depot now to buy some hardware bolts and
hardware nuts.)
Jim Strehlow
Advisor

Re: search command search-string - how search for '<char>' ?

Steve, I was not as clear as I should have been in my first example. I will try again.

The text log file contains items such as
...A=''B''...
XYZ...B=''C''...
AABBCCDD...A=''D''...
where we must find two adjacent single quotes (apostrophes) a character and then two more adjacent single quotes.

search *.log "A=''D''"
%SEARCH-I-NOMATCHES, no strings matched

because of symbol substitution. I need to turn off symbol substitution.
Jon Pinkley
Honored Contributor

Re: search command search-string - how search for '<char>' ?

$ type test.quot
...A=''B''...
XYZ...B=''C''...
AABBCCDD...A=''D''...
A = "B"
$ sea test.quot """B"""
A = "B"
$ sea test.quot "'"+"'B'"+"'"
...A=''B''...
XYZ...B=''C''...
AABBCCDD...A=''D''...
$

Because the fonts here display two single quotes as if it were a dowble quote, I have attached a text file you can view.
it depends
Jon Pinkley
Honored Contributor

Re: search command search-string - how search for '<char>' ?

I should have stated this in the previous message. The trick I used, is to use three individual strings, none of which contain two successive single quotes, and then use the plus "+" to concatenate the strings.

In other words, to search for all occurrences of two adjacent single quotes, use:

$ search file "'"+"'"

Since this font doesn't display well, that's
it depends
Jim Strehlow
Advisor

Re: search command search-string - how search for '<char>' ?

Getting closer.
The multiple strings that we think would be concatenated appear to be treated individually by search. I get false matches on unwanted lines in the file.


Next attempt:
I use /MATCH=AND to get what I want for now temporarily; but I am not yet understanding why the strings are not combined and treated as one search-string.
It appears that the DCL SEARCH command identically treats the commands
$ search test.quot "'"+"'B'"+"'"/match=and
and
$ search test.quot "'","'B'","'"/match=and

so I am assuming that string concatenation is NOT happening.
Hein van den Heuvel
Honored Contributor

Re: search command search-string - how search for '<char>' ?

teh definition of the problem is still not entriely clear to me. Best I can tell one solution is (on OpenVMS V8.3) :

$ text = "'" + "'%'" + "'"
$ sho symb text
TEXT = "''%''"
$ sea tmp.tmp &text/wild

The & forces 'deferred substitution'.

With PERL on any VMS version the single quotes are still nasty when expressed in a 'one liner'. So I would use 'octal' notation:

$ perl -ne "/\047\047.\047\047/" tmp.tmp


Now if you just want to match on say B and D, not C then change that to :

$ perl -ne "print if /\047\047[BD]\047\047/" tmp.tmp


Cheers,
Hein.


Jim Strehlow
Advisor

Re: search command search-string - how search for '<char>' ?

Thank you for the "heads up" regarding some new string substitution feature.

We will be testing version 8.3 on Integrity soon.
Steven Schweda
Honored Contributor

Re: search command search-string - how search for '<char>' ?

If you're looking for a particular character,
and not just any single character, then "&"
seems to help:

alp $ ss = "'"+ "'"+ "d"+ "'"+ "'"

alp $ write sys$output ss
''d''

alp $ sea a*.txt &ss

******************************
ALP$DKA0:[SMS]APO.TXT;2

abc''d''efg

where:

alp $ type apo.txt
abcdefg
abc'defg
abcd'efg
abc'd'efg
abc'defg
abcd'efg
abc'd'efg
abc''defg
abcd''efg
abc'd''efg
abc''d'efg
abc''d''efg
Hoff
Honored Contributor
Solution

Re: search command search-string - how search for '<char>' ?

Massively Brute Force:

$ x="**A**"
$ x[0,8]=%x27
$ x[8,8]=%x27
$ x[24,8]=%x27
$ x[32,8]=%x27
$ search y.y &x
....Z=''A''...
ABC=''A''...
$ type y.y
....Z=''A''...
...A=''B''...
XYZ...B=''C''...
AABBCCDD...A=''D''...
ABC=''A''...
$

Ampersand-based symbol substitution has been around only slightly less than forever. It's a second phase of substitution within the DCL parsing processing -- and you can get a double symbol substitution this way, if you need to do that. You can substitute a substring into a variable, then substitute the variable itself. This is one (very brute-force) way you can implement DCL arrays.

The 2nd edition of the Writing Real Programs in DCL book does cover ampersand-based substitution as it is one of the few ways to substitute symbols within the DCL command PIPE and its related processing.

Jon Pinkley
Honored Contributor

Re: search command search-string - how search for '<char>' ?

Well I blew it in my haste to get my answer in. I tested the setting of a symbol, and if I had paid attention to the output of the example I gave, I would have seen it was matching more than intended. I was trying to avoid the use of a temporary symbol, but now I am wondering if it is possible to do without the use of & and a temporary symbol.

It appears that the concatenation is being done by DCL, at a later stage than the CLI handling routines.

So it appears that Hein's example is about as short as is possible (that I know of).

If you need to search for a general string enclosed by two single quotes, you can set a temporary symbol using one of the methods mentioned, concatenating strings with single quotes, or overlaying an existing string with binary values as Hoff demonstrated. Once the string is in the symbol, using deferred substitution allows the symbol to escape the first pass substitution, but still be passed to the program as the "substituted" value.

For example to find xyzzy

$ tmp = "'"+"'xyzzy'"+"'"
$ search file &tmp

By the way, the new feature Hein was referring to was the /WILD switch, not the "&" deferred substitution. He was saying that if you were always searching for two single quotes followed by a single character and then two more single quotes, you could use the % "match single character" wildcard along with the new /WILD switch (and the &) to do it.

As Hoff said, the & substitution has been around since very early days.

Try this:

$ a="&B" ! B must be uppercase
$ b="C" ! C must be uppercase
$ c="Hello"
$ write sys$output 'a
Hello
$

If you understand why that works, you will be well on your way to understanding DCL parsing.

P.S. I tend to avoid the use of & substitution because it is almost guaranteed to confuse the next person to visit the command procedure. There are also some seemingly arbitrary restrictions, for example the need for the symbols to be in uppercase. Try the above example with lowercase characters in the quotations, and you will see that it does not work.

However, there are cases where they are required, Hoff mentioned a few, and it seems to me that this is another case where they are necessary.
it depends
Hoff
Honored Contributor

Re: search command search-string - how search for '<char>' ?

Here's a variation of what I've used with the escape character and control sequences -- non-printable, so it doesn't work so well when entering it in editors and such -- and worse, you don't generally want to have a control sequence interpreted other than at procedure run-time -- which can be used here:

$ apost ="*"
$ apost[0,8] = %x27
$ string = apost + apost + "A" + apost + apost

Hein van den Heuvel
Honored Contributor

Re: search command search-string - how search for '<char>' ?

>> Thank you for the "heads up" regarding some new string substitution feature.

Jim, as others pointed out, and the other examples make clear, the substitution feature has always been there.

The /WILD is newish, but since you did not indicate a specific version I though it was safe to assume you were running the current version. Reasonable? :^)

Anyway... the V8.3 Alpha SEARCH.EXE image runs just fine on a V7.2-1 Alpha. I tried.
Of course you need to provide the appropriate command definition which you can get from an 8.3 box with the VERB freeware, or find it attached here. I changed the original to point to image SEARCH_V83 to keep it clear and clean.

Finally, you did not react to the PERL suggestion. If you are really serious about searching for interesting pattern, than a langue providing REGULAR EXPRESSIONS is the way to go IMHO. For OpenVMS that would be PERL or AWK. Well worth the short learning curve. Not need to get all caried away with objects and methods, just exploit the basic tools like regexpr's.

Regards,
Hein.
Jon Pinkley
Honored Contributor

Re: search command search-string - how search for '<char>' ?

I agree with Hein that learning Perl or Awk is worthwhile. They allow for more complex pattern matching than SEARCH.

The primary advantage of being able to do it in DCL is portability to other VMS systems that may or may not have other tools loaded.

The techniques discussed to allow DCL to set symbols with doubled single quotes is still useful however. Where I have used it is in command procedures that generate other command procedures, where the generated command procedure has a line like

$ mail nla0: "''notify'" /subject="Entry ''this_entry' starting ''this_proc'"

The generating command procedure would have a line like:

$ write outproc "$ mail nla0: ""'"+"'notify'"" /subject=""Entry '"+ -
"'this_entry' starting '"+"'this_proc'"""

P.S. Hein, I don't know how to do the following with regex:

$ seach file a,b,c,d,e/match=and

without testing for each permutation of ordering. Am I missing something simple?
it depends
Ian Miller.
Honored Contributor

Re: search command search-string - how search for '<char>' ?

See also freeware FIND utility which does pattern matching.

http://h71000.www7.hp.com/freeware/freeware70/find/
____________________
Purely Personal Opinion
Hein van den Heuvel
Honored Contributor

Re: search command search-string - how search for '<char>' ?

>> $ seach file a,b,c,d,e/match=and
>> without testing for each permutation of >> ordering. Am I missing something simple?

regular expressions are 'pattern matches', not the dcl searched.
So I'm afraid you need to either do the permutations if practical or combine them. For example:
/a/ && /b/ && /c/ && /d/ && print

fwiw.
Hein.


Jon Pinkley
Honored Contributor

Re: search command search-string - how search for '<char>' ?

Thanks Hein, that works.

$ sea sys$login:login.com b,c,x,p,g,2/mat=and
$ mgbook4 :== $utility:mgbook disk$axpdocsep962:[decw$book]*lib*/sh
$ awk "/b/ && /c/ && /x/ && /p/ && /g/ && /2/ { print }" sys$login:login.com
$ mgbook4 :== $utility:mgbook disk$axpdocsep962:[decw$book]*lib*/sh

$

I was under the mistaken impression that only one pattern was allowed.
it depends
Sebastian Bazley
Regular Advisor

Re: search command search-string - how search for '<char>' ?

By the way, Perl one-liners can include multiple consecutive single-quotes if one uses back-slash to protect the second single quote:

perl -ne "print if m('\'D'\')" tmp.tmp

Still a bit messy, but easier than using octal escapes.

[The qq() and q() notation can also be useful in VMS one-liners]
Jim Strehlow
Advisor

Re: search command search-string - how search for '<char>' ?

Hoff did it again.

I am not yet a PERL user. (One of these days.)

I have been around since VMS v3 and have forgotten about using & in such a way.
I do so much database work with PL/SQL I sometimes forget some DCL basics.

We have some 7.1-2 customers, 7.2-1, 7.3, 7.3-1 etc. but we use 8.2 and just installed 8.3 on Integrity.

Thank you one and all.