grep variable with tricky content


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep variable with tricky content
# 1  
Old 12-08-2011
grep variable with tricky content

Hello,
i have this issue:
Code:
text="-8x7YTVNk2KiuY-PWG5zzzjB-zzw"
string=-8x7YTVNk2KiuY-PWG5zzzjB-zzw
echo $text | grep -v \'$string\'
-8x7YTVNk2KiuY-PWG5zzzjB-zzw
echo \'$string\'
'-8x7YTVNk2KiuY-PWG5zzzjB-zzw'

..and ofcourse if I do like this :
Code:
echo $text | grep -v $string
grep: invalid option -- Y
Usage: grep [OPTION]... PATTERN [FILE]...


all this because variable string stores content that begins with "-"

Any ideas how can I grep some text with a variable with such content ?


Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 12-08-2011 at 05:53 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 12-08-2011
use

Code:
 
grep --  -v $string

# 3  
Old 12-08-2011
I'm not familiar with this grep usage, I probably miss something, your command executed as you posted :
Code:
grep --  -v $string
grep: -8x7YTVNk2KiuY-PWG5zzzjB-zzw: No such file or directory

gives me this error. My problem is that the variable $string has content that begins with "-" . the variable $text can very well be a file with lot of content. my problem is variable $string


Moderator's Comments:
Mod Comment Please use code tags!

Last edited by zaxxon; 12-08-2011 at 06:17 AM.. Reason: code tags, see PM
# 4  
Old 12-08-2011
where is your echo statement ?

can you post the $string and $text values ?

---------- Post updated at 03:47 PM ---------- Previous update was at 03:45 PM ----------

Code:
 
$ echo "ab-cd-ef" | grep -v -- $a 
ab-cd-ef

$ echo $a
-ab

---------- Post updated at 03:47 PM ---------- Previous update was at 03:47 PM ----------

Code:
 
$ echo "ab-cd-ef" | grep -v -- $a 
ab-cd-ef

$ echo $a
-ab

This User Gave Thanks to itkamaraj For This Post:
# 5  
Old 12-08-2011
Can't you escape the "-" before executing "grep"?

Check below:
Code:
$ text="-8x7YTVNk2KiuY-PWG5zzzjB-zzw"
$ string="-8x7YTVNk2KiuY-PWG5zzzjB-zzw"
$ echo "${text}" | sed 's/^-/\\-/g'
\-8x7YTVNk2KiuY-PWG5zzzjB-zzw

$ nstring=$(echo "${text}" | sed 's/^-/\\-/g')
$ echo "${text}" | grep -cv "${nstring}"
0
$ echo "${text}" | grep -v "${nstring}"

#
# Or, as commented above:
#
$ echo "${text}" | grep -cv -- "${string}"
0
$ echo "${text}" | grep -v -- "${string}"

I hope it helps! =o)
This User Gave Thanks to felipe.vinturin For This Post:
# 6  
Old 12-08-2011
Code:
echo $text | grep -- -v $string
grep: -8x7YTVNk2KiuY-PWG5zzzjB-zzw: No such file or directory

obviously I've tried that also. the problem was the parameter order that you provided first. grep -- -v instead of grep -v --

nevertheless it finally worked :

Code:
echo $text | grep -- $string
-8x7YTVNk2KiuY-PWG5zzzjB-zzw

Thank you very much.

Last edited by Franklin52; 12-08-2011 at 06:39 AM.. Reason: Please use code tags for code and data samples, thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Getting ls content into a file using variable

hi i just cant figure out how can i do this ls -lt > log.txt using $PWD what i mean is how can i get the ls command content into a file using $PWD variable? :confused: (4 Replies)
Discussion started by: chinababy
4 Replies

2. UNIX for Dummies Questions & Answers

Tricky GREP question..

I have some large login files that I need to extract (user)@(server) from. Where it gets tricky is that there is usually more than one '@' sign on each line(although it does have a leading space if it's not part of the (user)@(server) string), I need only the (user)@(server) section, I need only... (6 Replies)
Discussion started by: Mordaris
6 Replies

3. Shell Programming and Scripting

how to get tags content by grep

1) Is it possible to get tags content by grep -E ? For example title. Source text "<title>My page<title>"; to print "My page". 2) which bash utility to use when I want to use regex in this format? (?<=title>).*(?=</title) (11 Replies)
Discussion started by: visitor123
11 Replies

4. Shell Programming and Scripting

Tricky recursive removal (find with grep)

Tricky one: I want to do several things all at once to blow away a directory (rm -rf <dir>) 1) I want to find all files recursively that have a specific file extension (.ver) for example. 2) Then in that file, I want to grep for an expression ( "sp2" ) for example. 3) Then I want to... (1 Reply)
Discussion started by: jvsrvcs
1 Replies

5. Shell Programming and Scripting

Loop over variable content

Hello all, I do have a variable containing one line like this: Waiting for job XXXXXX to start I needed to get the 'XXXXXX' literal, so I did the following: job_interno=`echo $log_exec | sed 's/.*Waiting for job \(*\).*/\1/' ` #other stuff Now, my variable is have more... (5 Replies)
Discussion started by: manolain
5 Replies

6. Shell Programming and Scripting

Can i use grep to check variable content correctnes?

I need to know if is possible to use grep to check content of a local variable, for eg. i use read index and i want to check if the index i read is in correct form, how do i do that i tried with grep but i get errors all the time dont know how to make it work.. thanks! (3 Replies)
Discussion started by: Goroner
3 Replies

7. Shell Programming and Scripting

Grep content between timestamp

Hi all, I have a file which will be updated every half an hour and time stamp will be printed in the beginning of the updation. i just want to grep the content between every hoalf an hour. Pls help me on this issue. how to grep contents between tim stamp? Ex of file: 29/09/2010... (20 Replies)
Discussion started by: steve2216
20 Replies

8. Shell Programming and Scripting

Content of variable

I have a variable that contains filenames like this: variable="file_1.extension<blank>file_2.extension<blank>file_3.extension<blank>file_4.extension and so on" How can I make filenames to be separated by newline: (I tried Sed but it didn't worked well) file_1.extension file_2.extension... (6 Replies)
Discussion started by: MartyIX
6 Replies

9. Shell Programming and Scripting

Content of Content of a variable!

I got a sample BASH script like this : $ cat test MYVAR=$1 DUMMY1="This is tricky" DUMMY2=24 echo $ $ ./test DUMMY1 ./test: line 5: This is tricky: syntax error in expression (error token is "is tricky") **I was expecting the output as "This is tricky", ah! but no luck **But... (2 Replies)
Discussion started by: jaduks
2 Replies

10. Shell Programming and Scripting

How to replace a variable content

Hi, variable1="This is a car" Now I want to replace the content of variable1, "car" to "dog". Is there any simple command I can use. Thanks. Joseph (4 Replies)
Discussion started by: josephwong
4 Replies
Login or Register to Ask a Question