comparing scalars contaning "DOUBLE QUOTES" as data


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting comparing scalars contaning "DOUBLE QUOTES" as data
# 1  
Old 04-20-2008
comparing scalars contaning "DOUBLE QUOTES" as data

Hello to all,

Does anyone know the solution ?

Two strings A and B are present. I want to check whether B is a Substring of A.

1. The value of A is - 29 * * * /bin/ls "test" "tmp*" "log*"
(Note: Pl note that A contains DOUBLEQUOTES, ASTERISK & FRONTSLASH)

2. The value of B is - /ls "test" "tmp*" "log*"
(Note: again B contains DOUBLEQUOTES, ASTERISK & FRONTSLASH)

Since the variable scalars A and B have DOUBLEQUOTES as a part of the data stored "=~" operator is not working.

[ $A =~ $B ] - results in error..

Is there any other way (any sed/awk/perl one liners). Requesting help to solve this.

Thanks
rssrik
# 2  
Old 04-20-2008
First of all, you need to learn to quote arguments properly. Simply double-quote anything with a variable in it, and you should be fine, most of the time.

If the strings also contain regex specials (in this case, asterisks), those need to be escaped somehow, or you need to use a different comparison function. May I suggest the plain old case statement?

Code:
case $A in *"$B"*) echo "$A contains $B";; *) echo does not;; esac


Last edited by era; 04-20-2008 at 09:12 AM.. Reason: Had $A and $B mixed up
# 3  
Old 04-20-2008
thanks .. it works

just wondering how come double quotes are escaped properly in case statement ! while [ "$A" =~ "$B" ] does not work ! seems like I have lot of catching up to do in shell basics ..

Thanks again era
rssrik
# 4  
Old 04-20-2008
The double quotes are not the problem, it's that you are using the regular expression match operator on something which is just a string, and which happens to contain characters which have a special meaning in regular expressions. " *" (space asterisk) matches zero or more spaces in a regular expression, but doesn't match a literal asterisk. "log*" matches "lo", "log", "logg", "loggg", "logggg" etc but doesn't match "log*".
# 5  
Old 04-20-2008
Gotcha !

Thanks to you "ERA"...
# 6  
Old 04-20-2008
You can also use a conditional expression to test if B is a substring of A i.e.
Code:
$!/usr/bin/ksh93

# ANSI C string quoting!
A=$'29 * * * /bin/ls "test" "tmp*" "log*"'
B=$'/ls "test" "tmp*" "log*"'

[[ ${A} == *${B}* ]] && print "Yes, B is a substring of A"

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. AIX

Apache 2.4 directory cannot display "Last modified" "Size" "Description"

Hi 2 all, i have had AIX 7.2 :/# /usr/IBMAHS/bin/apachectl -v Server version: Apache/2.4.12 (Unix) Server built: May 25 2015 04:58:27 :/#:/# /usr/IBMAHS/bin/apachectl -M Loaded Modules: core_module (static) so_module (static) http_module (static) mpm_worker_module (static) ... (3 Replies)
Discussion started by: penchev
3 Replies

2. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. Shell Programming and Scripting

Replacing double quotes with the unicodes "urgent!"

Hi, I have the following text in a file <div class="snippet abstract"> We present a new "model" for multiple-input-multiple-output (MIMO) 'outdoor' has many things "what" ever </div></a href=sdfkkf"> </div> <div class="snippet context"> I have to replace the string between the <div... (1 Reply)
Discussion started by: vms
1 Replies

5. Shell Programming and Scripting

Confusion with "su -c" and quotes, user context switching?

Trying to execute commands for different Unix user with that user's environment variable context without fully switching as that user using sudo && su capabilities. Hoping this would help with security and not having to waste time switching between 10 different app users on same server. I do... (6 Replies)
Discussion started by: kchinnam
6 Replies

6. Shell Programming and Scripting

Expect scripting - How to match a double quotes " "

I am trying to match a text which contains the " ", from the log file. But it doesn't match. I understand that " " has got a special meaning to TCL/Expect. hence I tried the following, but no luck. expect -ex { "lp -c -demail -ot\\\"firstname_surname@gmail.com\\\"... (3 Replies)
Discussion started by: prakasuj
3 Replies

7. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

8. Shell Programming and Scripting

Removal of comma(,) present inbetween double quotes(" ")

Hi Experts, I have a file with some of the records contain double quotes. If I found a double quote(") in any particular record , I need to look for the next double quote in that particular record and in between these quotes, if any comma(,) is there I need to replace with Tilde (~) in the same... (12 Replies)
Discussion started by: vsairam
12 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question