This question already has answers here:
Grep match only before ":"
(3 answers)
Closed 4 years ago.
I have a file and it contains lines
users:x:22845:test1
test-users:x:22845:test2,test3
If I run grep users: file, it shows both lines becuase both have users:.
How can I get only users:x:22845:
Thank you
Perhaps specify that the line must start with "users". You can use the caret (^) special character to anchor the match at the beginning of the line:
egrep '^users:' file
This will not match the line beginning with "test-users".
Related
This question already has answers here:
How do you use a plus symbol with a character class as part of a regular expression?
(3 answers)
Closed 2 years ago.
I have a problem to work on and was wondering why my regex won't work. It's a simple exercise to match words in a text dictionary that contains the top row. I believe I have a solution but grep comes up blank every time:
grep ^[qwertyuiop]+$ /opt/~~~~~~/data/web2
this is my command, which does nothing, but if i just put:
grep [qwertyuiop] /opt/~~~~~~/data/web2
it matches words with letters from the top row. Can anybody tell me why it isn't working? Thank you all for your time.
you're super close.
With grep you want to use the -x flag to match the whole line.
grep -x '[qwertyuiop]\+' /usr/share/dict/american-english
then a simple escaped + to match multiple characters.
if you want to avoid the -x you can take your original approach like so:
grep '^[qwertyuiop]\+$' /usr/share/dict/american-english
With an escape and some quotes it works marvelously, although i think the -x is more idiomatic, as some other people have commented, you can also get away with using -e although that can have some unintended consequences. I recommend man grep which gives a nice overview.
I don't think grep recognizes ^ $ or + on it's own. You have to use grep -e or egrep to use special characters like that
This question already has answers here:
matching a line with a literal asterisk "*" in grep
(6 answers)
Closed 2 years ago.
I'm trying to use grep files in a Linux directory searching for lines which contain the string **Post. The * character is a wildcard, and I can't figure out how to make it literal for this search. For example \*\*Post doesn't work. What's the proper way of escaping the * character so it can be used literally in this case?
I tested with a file containing the following text:
**Post
*Post
Post
And I would like to grep only the one with **Post
My command is the following
grep -irn "\*\*Post"
The double quote is important.
The result of the command is
a.txt:1:**Post
While the following command
grep -irn "\*Post"
outputs
a.txt:1:**Post
a.txt:2:*Post
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I need to create a regex which contains at least 1 special character, and at least 1 number with alphabets.
You may try the following pattern:
^(?=.*[0-9])(?=.*[^A-Za-z0-9])(?=.*[A-Za-z]).*$
Explanation:
(?=.*[0-9]) assert one number present
(?=.*[^A-Za-z0-9]) assert one special character present
(?=.*[A-Za-z]) assert one alpha present
Note that I have defined a special character as being anything not alphanumeric. If instead you have a list of special characters, then you can modify the middle lookahead in my pattern.
This question already has answers here:
What are <-- Ruby Strings called? And how do I insert variables in them?
(3 answers)
Closed 6 years ago.
I'm working on Rails. In my code base, I see a line that using Arel::SqlLiteral like this:
result = Arel::Nodes::SqlLiteral.new(<<-SQL
CASE WHEN condition1 THEN calculation1
WHEN condition2 THEN calculation2
WHEN condition3 THEN calculation3
ELSE default_calculation END
SQL)
I understand what this code piece do. The thing I don't understand is its grammar, at this point:
Arel::Nodes::SqlLiteral.new(<<-SQL
...
SQL
)
So in ruby, what is the grammar of <<- follow by name, and then at last block we call that name.
thanks
The keyword you're looking for is "Heredoc".
https://ruby-doc.org/core-2.2.0/doc/syntax/literals_rdoc.html#label-Here+Documents
It's mainly used to prettify large texts and common practice for shells/shellscripts. The marker on top indicates the beginning of a heredoc and the marker on bottom (which must not be indented unless you place a “-” before the opening marker) specifies the end.
This question already has answers here:
How to use awk to print lines where a field matches a specific string?
(6 answers)
Closed 6 years ago.
In my case I want to grep lines which have number 4 in fourth column. Fourth column is last column. Columns are separated with space. I have this command:
grep -P '^([^\s]*\s){3}4
But that greps lines which contains nubers which strats with 4 like for example: 45, 4768, but I want that this works only for number 4.
That just doesn't work:
grep -P '^([^\s]*\s){3}4\n
awk '{print $4}' | grep ^4$
I personally think its more clear if you use awk to get the column, then you can add a $ which matches the end of the line.
You also may need a caret to match only lines beginning with and ending with a 4.