How to grep a shell variable to match end of line? - regex

How can I match a bash variable in the end of the line?
The code below can do that for a number in the end:
grep '[0-9]$'
But in my case the number is a variable. I was hoping something like
grep '{$i}$'
where $i is my variable, would work, but no avail. I've tried with several single/double comma combinations but no sucess so far.
Thanks in advance.

You can use this grep:
grep "$i\$" file
OR:
grep $i'$' file

You have to use "double quoting" for variable expansion in shell.. use the following:
grep "$i\$" file

Use single quotes to make the $ work as end-of-line. If you want to grep with some variable also, use double and single quotes as below:
grep "$var"'$'

Related

How to pass a variable from bash to regex

I'm trying to print only lines that contain the variable $foo. I've tried using double quotes and curly braces to no avail. What is the proper way to pass a shell variable to a regex in sed?
sed -n 's:\("${foo}".*$\):\1:p' file.txt
sed is overkill if you don't actually need to modify the matching lines. Just use grep:
grep "$foo" file.txt
Try the below sed command to print the lines which contains value assigned to the variable foo.
sed -n "/$foo/p" file
Using single quote you get the result like
$-pattern='foo'
$-sed 's/('$pattern'.*$)/<\1>/g'
fo
fo
foo
<foo>

Basic grep regex

I'm trying to use grep to find all files whose contents contain ".ple". I tried:
grep -ilr /.\.ple/ *
But it did not work.
Would someone be able to tell me what I did wrong?
Thanks.
there are many programming/script languages allow us to write regex between /.../ (slashes). But with grep, you cannot do it, you should wrap your regex expression in quotes. I think that is the problem of your grep line. The correct one:
grep -ilr '.\.ple' *
with yours: grep -ilr /.\.ple/ * grep will look for lines like this:
foo/a.ple/bar
that is, the slash would be literal letter.
What do you mean by it did not work?
Try this:
grep -rlE '\.ple$'
The above should work.
Edit: To search all files whose contents contain ".ple" try this
grep -rl '\.ple'

How to use sed and regex?

I need to use sed to look for all lines in a file with pattern "[whatever]|[whatever]" so I'm using the following regex:
sed '/\"[a-zA-Z0-9]+\|[a-zA-Z0-9]+\"/p' test2.txt
But it's not working because in this file is returning something when it shouldn't
RTV0031605951US|3160595|20/03/2013|0|"Laurie Graham"|"401"
Does anybody know with regex should I use? Thanks in advance
I see three problems with your regular expression:
+ is not a metacharacter, so you need to escape it to get its special meaning.
Similar issue happens with the pipe. Neither it is a metacharacter, so don't escape it to match it literally.
Sed by default prints each line that matches, so add -n that avoids that, if you already use /p that prints it. Otherwise you will have those lines twice in the output.
sed will output anything that is a partial match.
To match only whole lines that match your regex, add ^ and $ to the start/end:
sed '/^\"[a-zA-Z0-9]+\|[a-zA-Z0-9]+\"$/p' test2.txt
sed '/\B\"[ [:alnum:]]\+\"|\"[ [:alnum:]]\+\"\B/!d' file
If you use this in a sed script, do not escape double quotes.

Having trouble with GREP and REGEX

I have a text file that stores combinations of four numbers in the following format:
Num1,Num2,Num3,Num4
Num5,Num6,Num7,Num8
.............
I have a whole bunch of such files and what I need is to grep for all filenames that contains the pattern described above.
I constructed my grep as follows:
grep -l "{d+},{d+},{d+},{d+}" /some/path/to/file/name
The grep terminates without returning anything.
Can somebody point out what I might be doing wrong with my grep statement?
Thanks
This should do what you want:
egrep -l '[[:digit:]]+,[[:digit:]]+,[[:digit:]]+,[[:digit:]]+' /some/path/to/file/name
One way is using a perl regexp:
grep -Pl "\d+,\d+,\d+,\d+" /some/path/to/file/name
In your syntax d is literal. It should be escaping that letter, but is not accepted by grep regular regexp.

Unix grep regex how to grep for

I have a text file
$ cat test.log
SYB-01001
SYB-18913
SYB-02445
SYB-21356
I want to grep for 01001 and 18913 only whats the way to do this
I want the output to be
SYB-01001
SYB-18913
SYB-02445
I tried this but not sure whats wrong with it
grep 'SYB-(18913)|0*)' test.log
Use the -E flag for "extended regular expressions" with grep.
e.g.
grep -E 'SYB-(0|18913)' test.log
Other things to be aware of:
parentheses must match (for every opening bracket you want a closing bracket)
0* means zero or more 0 characters - in truth this will match everything
Try that :
grep 'SYB-\(18913\|0*\)' test.log
But you maybe don't want the 0* part to act like this. Maybe 0+ is better.
awk
awk '/SYB-(0|18913)/' file
Your brackets are out. Try
grep 'SYB-[18913|0*]' test.log