Regex in grep to match all lines ending with a double quote (") OR a single quote (')


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Regex in grep to match all lines ending with a double quote (") OR a single quote (')
# 1  
Old 08-26-2009
Error Regex in grep to match all lines ending with a double quote (") OR a single quote (')

Hi,
I've been trying to write a regex to use in egrep (in a shell script) that'll fetch the names of all the files that match a particular pattern. I expect to match the following line in a file:
Code:
Name = "abc"

The regex I'm using to match the same is:
Code:
egrep -l '(^[nN][aA][mM][eE]) *= *" *[a-zA-Z0-9_+-]* *"$' /PATH_TO_SEARCH

But I now want to search all filenames which have the name quoted in either " OR '
i.e., the pattern to match could be:
Code:
Name = "abc"

OR
Code:
Name = 'abc'

I've tried various ways to include ' in the regex I've posted above, but I'm not able to get it right. I've tried using a backslash (\) and also tried things like [\'\"].
Any help to get this right is appreciated.
Thanks.

Last edited by rbatte1; 07-07-2016 at 11:37 AM.. Reason: Code tags
# 2  
Old 08-26-2009
With awk:

Code:
awk -F "\'|\"" '/Name =/{print $2}' file

# 3  
Old 08-26-2009
Thanks Franklin52,
Is there a way to do the same in grep itself. I wanted the regex to be part of the grep regex itself. Are you suggesting that I use awk instead of grep?

Thanks
NJ
# 4  
Old 08-26-2009
Hi.

When you need to protect special characters on the command line, you need to use quoting. However, as you've found, if the special characters are the quote symbols themselves, you can run into trouble.

One solution for some versions of grep is to have the pattern in a file so that it does not appear on the command line. That can be accomplished by using a here document to create the file. There are features in the here document syntax to ignore special characters, in addition to creating a file from within a script. Once that is done, we can use grep to read the regular expressions from the newly-created file. Here is an example:
Code:
 #!/usr/bin/env bash

# @(#) s1	Demonstrate isolation of quotes in file for grep.

echo
set +o nounset
LC_ALL=C ; LANG=C ; export LC_ALL LANG
echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) grep
set -o nounset
echo

FILE=${1-data1}

echo " Data file $FILE:"
cat $FILE

echo
echo " Results:"
cat > my-pattern <<'EOF'
[nN][aA][mM][eE] *= *['"].*['"]
EOF
grep -f my-pattern $FILE

exit 0

producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0 
GNU bash 3.2.39
GNU grep 2.5.3

 Data file data1:
name = "double-1"
Name = "double-2"
name = 'single-1'
name = none
name = 'single-2'

 Results:
name = "double-1"
Name = "double-2"
name = 'single-1'
name = 'single-2'

Another method is to surround the regular expression on the command line with double quotes. Inside of double quotes you may have escaped double quotes, \", and single quotes. However, you may not have escaped single quotes within a single-quoted string.

See man pages for details. Good luck ... cheers, drl
# 5  
Old 08-26-2009
Writing the pattern to a file without the hassles of escaping the quotes worked perfectly fine!
Thanks a ton.. Smilie

I had escaped both " and ' when I had the regex directly in the command. Wonder why it failed despite using "\"....
Had tried the following:
Code:
egrep -l "(^[nN][aA][mM][eE]) *= *[\"\'] *[a-zA-Z0-9_+-]* *[\"\']$" /FILE
egrep -l "(^[nN][aA][mM][eE]) *= *\"\|\' *[a-zA-Z0-9_+-]* *\"\|\'$" /FILE

I'd used both these options to get the OR, but they'd failed.

Thanks
NJ

Last edited by rbatte1; 07-07-2016 at 11:38 AM.. Reason: Code tags
# 6  
Old 08-26-2009
Hi.

Modifications to your regular expressions, all searches done with egrep:
Code:
#!/usr/bin/env bash

# @(#) s2	Demonstrate isolation of quotes in file for egrep.

echo
set +o nounset
LC_ALL=C ; LANG=C ; export LC_ALL LANG
echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) egrep
set -o nounset
echo

FILE=${1-data1}

echo " Data file $FILE:"
cat $FILE

echo
echo " Results with here-document solution:"
cat > my-pattern <<'EOF'
[nN][aA][mM][eE] *= *['"].*['"]
EOF
egrep -f my-pattern $FILE

echo
echo " Results with command-line solution 1:"
# egrep -l "(^[nN][aA][mM][eE]) *= *[\"\'] *[a-zA-Z0-9_+-]* *[\"\']$" /FILE
egrep "^[nN][aA][mM][eE] *= *[\"'] *[a-zA-Z0-9_+-]* *[\"']$" $FILE

echo
echo " Results with command-line solution 2:"
# egrep -l "(^[nN][aA][mM][eE]) *= *\"\|\' *[a-zA-Z0-9_+-]* *\"\|\'$" /FILE
egrep "^[nN][aA][mM][eE] *= *(\"|') *[a-zA-Z0-9_+-]* *(\"|')$" $FILE


exit 0

producing:
Code:
% ./s2

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0 
GNU bash 3.2.39
egrep GNU grep 2.5.3

 Data file data1:
name = "double-1"
Name = "double-2"
name = 'single-1'
name = none
name = 'single-2'

 Results with here-document solution:
name = "double-1"
Name = "double-2"
name = 'single-1'
name = 'single-2'

 Results with command-line solution 1:
name = "double-1"
Name = "double-2"
name = 'single-1'
name = 'single-2'

 Results with command-line solution 2:
name = "double-1"
Name = "double-2"
name = 'single-1'
name = 'single-2'

See man pages, experiment on small cases ... cheers, drl
# 7  
Old 08-26-2009
Smilie
Thanks!!! That was me caught up in an endless loop of regex learning!!!

Another question posted by me which you might be able to answer. Pls do check it when you find some time. (https://www.unix.com/shell-programmin...-filepath.html)

Thanks a lot for the help again!
NJ
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk: single quote match in If

Hello, I'd like to print line if column 5th doesn't match with exm. But to reach there I have to make sure I match single quote. I'm struggling to match that. I've input file like: Warning: Variants 'exm480340' and '5:137534453:G:C' have the same position. Warning: Variants 'exm480345'... (9 Replies)
Discussion started by: genome
9 Replies

2. Shell Programming and Scripting

Replacing all but the first and last double quote in a line with a single quote with awk

From: 1,2,3,4,5,This is a test 6,7,8,9,0,"This, is a test" 1,9,2,8,3,"This is a ""test""" 4,7,3,1,8,"""" To: 1,2,3,4,5,This is a test 6,7,8,9,0,"This; is a test" 1,9,2,8,3,"This is a ''test''" 4,7,3,1,8,"''"Is there an easy syntax I'm overlooking? There will always be an odd number... (5 Replies)
Discussion started by: Michael Stora
5 Replies

3. Shell Programming and Scripting

Replacing Double Quote in Double Quote incsv file

Hi All , We have source data file as csv file and since data could contain commas ,each attribute is quoted into double quotes.However problem is that some of the attributa data also contain double quotes which is converted to double double quote while creating csv file XLs data : ... (2 Replies)
Discussion started by: Shalini Badal
2 Replies

4. Shell Programming and Scripting

Replace double quotes with a single quote within a double quoted string

Hi Froum. I have tried in vain to find a solution for this problem - I'm trying to replace any double quotes within a quoted string with a single quote, leaving everything else as is. I have the following data: Before: ... (32 Replies)
Discussion started by: pchang
32 Replies

5. Shell Programming and Scripting

How to match character with single quote?

I need to check whether first character of variable is single quote. I tried the below constructions but they are all not working (always return true) if (test `echo "$REGEXP" |cut -c1` != "'"); then echo "TRUE"; fi if (test `echo "$REGEXP" |cut -c1` != '\''); then echo "TRUE"; fi if (test... (5 Replies)
Discussion started by: urello
5 Replies

6. Shell Programming and Scripting

printing lines contain double quote

Hello , I got html file , these file are normal html as we can see . what i would like to do is in this html file , i want to print only string start with double quote and end with double quote by line by line. <tr><td valign=top>25.</td><td><A... (8 Replies)
Discussion started by: davidkhan
8 Replies

7. Shell Programming and Scripting

replacing a quote in some lines with multiple quote fields

i want to replace mistaken quotes in line starting with tag 300 and relocate the quote in the correct position so the input is 223;25 224;20100428064823;1;0;0;0;0;0;0;0;8;1;3;9697;18744;;;;;;;;;;;; 300;X;Event:... (3 Replies)
Discussion started by: wradwan
3 Replies

8. Shell Programming and Scripting

grep : having trouble with a single quote

Hi, I search for the string below which contains a single quote, some text '/home/myuser in the file myfile.txt as another user with the grep command as follows su - myuser -c "grep 'some text \'/home/myuser' myfile.txt" I also tried using two backslashes su - myuser... (6 Replies)
Discussion started by: cimcmahon
6 Replies

9. Shell Programming and Scripting

Records which are staring with double quote(") and a number

Hi Experts... I am trying to find out separting the records which are staring with double quote(") and a six digit number(ex: 012456,987654,etc) from a file. For example : Source File : "116462","SMITH CHEVR "164098","SIMPS "104498","SIMPSONVIL "Export lments" "Copyrts... (4 Replies)
Discussion started by: vsairam
4 Replies

10. Shell Programming and Scripting

single or double quote in SED

i m trying the following command but its not working: sed 's/find/\'replace\'/g' myFile but the sed enters into new line # sed 's/find/re\'place/g' myFile > I havn't any idea how to put single quote in my replace string. Your early help woud be appreciated. Thanx (2 Replies)
Discussion started by: asami
2 Replies
Login or Register to Ask a Question