Hello, all! Maybe the title is badly formulated, you can help me with that...!
I'm using the GNU grep, and I need to make sure that grep will extract only what I tell it to.
I have the following regular expression: [a-z_][a-z0-9_-]*[$]?
Well, I need to make sure I grep only a word which may start with a lowercase letter or underline, the following characters may contain numbers and dashes aswell, and the last character can be a dollar sign.
Would you help me with this command?
Code:
$ echo "u_s9Ae-u" | grep "[a-z_][a-z0-9_-]*[$]\?" # should not grep "A"
u_s9Ae-u
$ echo "u_s9Ae-u" | grep -x "[a-z_][a-z0-9_-]*[$]\?"
u_s9Ae-u
$ echo "u_s9Ae-u" | grep -o "[a-z_][a-z0-9_-]*[$]\?"
u_s9
e-u
Well, I need to make sure I grep only a word which may start with a lowercase letter or underline, the following characters may contain numbers and dashes aswell, and the last character can be a dollar sign.
It usually pays to simply re-read the definition you start with when constructing regexps. In this case:
1.) only a word
If we use the old IBM definition of a "word": A word is a sequence of non-blank characters, separated by blanks. ; we end up with something like:
We search for an (optional) blank/tab character, followed by one or more non-blanks/non-tabs, followed by an optional tab/blank.
2.) which may start with a lowercase letter or underline,
Ok, we fine-tune our definition of the word:
Code:
grep '[<b><tab>]*[^_a-z][^<b><tab>][<b><tab>]*'
We search for an (optional) blank/tab character, followed by one underline or lowercase letter, more non-blanks/non-tabs, followed by an optional tab/blank.
3.) the following characters may contain numbers and dashes aswell,
more fine-tuning on what we mean by "word" here, i think it is self-explanatory now:
Probably we could drop the ending "[<b><tab>]*" now, because it might be superfluous - you will have to decide that by running the regexp against your data. Replace "<b>" and "<tab>" with literal blanks/tabs when you enter the code, i just used this to make them visible.
Thank you, bakunin! But it still greps the "A", or I'm doing something wrong... The idea is: we prompt the user for a string, and then we check if it matches the criteria. So the echo thing is actually used, and the pipe later:
@Bakunin, we should also take words at the start (^) or the end of the line ($). Using [<b><tab>]* with no further anchors means that it may match part of a word too, since we are allowing occurrence on both sides to be zero.
@Bakunin, we should also take words at the start (^) or the end of the line ($). Using [<b><tab>]* with no further anchors means that it may match part of a word too, since we are allowing occurrence on both sides to be zero.
I have the following alias: alias grep='grep --color=auto', and the difference between the following commands is that the one which outputs colors is with the 'x', the other is black and white... To bypass the alias I tried "\grep", but it doesn't change.
$ apt-cache show grep
Description: GNU grep, egrep and fgrep
'grep' is a utility to search for text in files; it can be used from the
command line or in scripts. Even if you don't want to use it, other packages
on your system probably will.
.
The GNU family of grep utilities may be the "fastest grep in the west".
GNU grep is based on a fast lazy-state deterministic matcher (about
twice as fast as stock Unix egrep) hybridized with a Boyer-Moore-Gosper
search for a fixed string that eliminates impossible text from being
considered by the full regexp matcher without necessarily having to
look at every character. The result is typically many times faster
than Unix grep or egrep. (Regular expressions containing backreferencing
will run more slowly, however.)
I have multiple strings in a file which have special character $, when i search strings by ignoring $ with \ using single quotes it returns empty results.
My search strings are set char_1($lock) and set new_char_clear_3($unlock)
I tried searching with
but it returns empty results.However... (3 Replies)
Hi all,
I have the following entries in a file:
Cause Indicators=80 90
Cause Indicators=80 90
Cause Indicators=82 90
Cause Indicators=82 90
Cause Indicators=82 90
The first 2 digits might change so I am after a sort of grep which could find any first 2 digits + the second 2,... (3 Replies)
Hi to you all,
I'm just struggling with a regex problem and I'm pretty sure that I'm missing sth obvious... :confused:
I need a regex to feed my grep in order to find lines that contain one string but not the other.
Here's the data example:
2015-04-08 19:04:55,926|xxxxxxxxxx| ... (11 Replies)
I have a file that contains the 2 following lines (from /proc/mounts)
/dev/sdc1 /mnt/backup2 xfs rw,relatime,attr2,noquota 0 0
/dev/sdb1 /mnt/backup xfs rw,relatime,attr2,noquota 0 0
I need to match the string in the second column exactly so that only one result is returned, e.g.
> grep... (2 Replies)
I have a line of text for example
aaaa bbbb cccc dddd eeee ffffff
I would need to get the cccc however bbbb could be there or not.
So whether bbbb is in the line or not I need cccc.
I was looking at either awk or sed....and trying to start at c and end until the next space.
Also... (11 Replies)
i have a file (test.txt) that contains:
20799510617900000928000000005403020110315V
20799510617900000928000000005403020110316
20799510617900000928000000005403020110317
20799510617900000928000000005403020110318V
grep V test.txt > /tmp/void.log
if
then
mail -s "void" < test.txt
fi... (2 Replies)
My input contains a single word lines.
From each line
data.txt
prjtestBlaBlatestBlaBla
prjthisBlaBlathisBlaBla
prjthatBlaBladpthatBlaBla
prjgoodBlaBladpgoodBlaBla
prjgood1BlaBla123dpgood1BlaBla123
Desired output -->
data_out.txt
prjtestBlaBla
prjthisBlaBla... (8 Replies)
Hey everyone,
Basically, all I'm looking for is a way to regex for not a certain string. The regex I'm looking to avoid matching is:
D222
i.e. an equivalent of:
awk '!/D222/'
The problem is that I use this in the following command in a Bash script:
ls ${source_directory} | awk... (1 Reply)
Hello,
I'm working on unix with grep (GNU grep) 2.5.1. I'm going through some of the newer regex syntax using Regular Expression Reference - Advanced Syntax a guide.
ls -aLl /bin | grep "\(x\)"
Which works, just highlights 'x' where ever, when ever.
I'm trying to to get (?:) to work but... (4 Replies)
Ok, I'm stumped and can't seem to find relevant info.
(I'm not even sure, I might have asked something similar before.):
I'm trying to use shell scripting/UNIX commands to extract URLs from a fairly large web page, with a view to ultimately wrapping this in PHP with exec() and including the... (2 Replies)