How can I grep for a string that contains multiple consecutive dashes?
With -+
you are saying: multiple -
. But this is not understood automatically by grep
. You need to tell it that +
has a special meaning.
You can do it by using an extended regex -E
:
grep -E -- "-+" file
or by escaping the +
:
grep -- "-\+" file
Test
$ cat a
---0 [58ms, 100%, 100%]
hell
$ grep -E -- "-+" a
---0 [58ms, 100%, 100%]
$ grep -- "-\+" a
---0 [58ms, 100%, 100%]
From man grep
:
REGULAR EXPRESSIONS
Basic vs Extended Regular Expressions
In basic regular expressions the meta-characters ?, +, {, |,
(, and ) lose their special meaning; instead use the backslashed
versions \?, \+, \{, \|, \(, and \).
How can I grep for a string that begins with a dash/hyphen?
Use:
grep -- -X
Documentation
Related: What does a bare double dash mean? (thanks to nutty about natty).
grep regex solution for finding 3 or more dashes in a string
this will print all directory names with 3 or more dashes
find . -type d -exec sh -c '[ $(echo {} | grep -o - | wc -l) -ge 3 ]' \; -print
How to extract text between first 2 dashes in the string using sed or grep in shell
You can use awk
keeping in mind that in Makefile the $
char in awk
command must be doubled:
url=$(shell echo 'feature/test-111-test-test' | awk -F'-' '{gsub(/\//, "-", $$1);print $$1"-"$$2"-"}')
echo "$url"
# => feature-test-111-
See the online demo. Here, -F'-'
sets the field delimiter as -
, gsub(/\//, "-", $1)
replaces /
with -
in Field 1 and print $1"-"$2"-"
prints the value of -
-separated Field 1 and 2.
Or, with a regex as a field delimiter:
url=$(shell echo 'feature/test-111-test-test' | awk -F'[-/]' '{print $$1"-"$$2"-"$$3"-"}')
echo "$url"
# => feature-test-111-
The -F'[-/]'
option sets the field separator to -
and /
.
The '{print $1"-"$2"-"$3"-"}'
part prints the first, second and third value with a separating hyphen.
See the online demo.
Make grep search patterns starting with dash
Just use \\-f
$ ls --help | grep \\-f
-f do not sort, enable -aU, disable -ls --color
--file-type likewise, except do not append '*'
--format=WORD across -x, commas -m, horizontal -x, long -l,
--full-time like -l --time-style=full-iso
--group-directories-first
file-type (--file-type), classify (-F)
Regex for line containing one or more spaces or dashes
Something like that should work:
grep -E -- ' |\-' file.txt
Explanation:
-E
: to interpret patterns as extended regular expressions--
: to signify the end of command options' |\-'
: the line contains either a space or a dash
How to grep for two words existing on the same line?
Why do you pass -c
? That will just show the number of matches. Similarly, there is no reason to use -r
. I suggest you read man grep
.
To grep for 2 words existing on the same line, simply do:
grep "word1" FILE | grep "word2"
grep "word1" FILE
will print all lines that have word1 in them from FILE, and then grep "word2"
will print the lines that have word2 in them. Hence, if you combine these using a pipe, it will show lines containing both word1 and word2.
If you just want a count of how many lines had the 2 words on the same line, do:
grep "word1" FILE | grep -c "word2"
Also, to address your question why does it get stuck : in grep -c "word1"
, you did not specify a file. Therefore, grep
expects input from stdin
, which is why it seems to hang. You can press Ctrl+D to send an EOF (end-of-file) so that it quits.
How to FGREP 2 dashes '--'?
If you want to fgrep all files that end with .sql
then use
fgrep -ircl --include=*.sql -- -- *
or (note the comma in {sql,}
:
fgrep -ircl --include=*.{sql,} -- -- *
If you want to fgrep more than one type of extension, then use something like
fgrep -ircl --include=*.{sql,txt} -- -- *
As others have already mentioned, the first --
tells fgrep to stop looking for flags and options. The second --
is the fixed-string pattern.
Grep output adds extra dashes and newlines
grep -A1 -f query_list.txt initial_file.fasta | sed '/^--/d' > final.fasta
or
grep -A1 -f query_list.txt initial_file.fasta | grep -v '^--' > final.fasta
Related Topics
How to Setup Virtualhosts to Point Two Ports on Same Ip to Different Servernames
Jmeter:Difference Between Jmeter.Sh and Jmeter Without Extension File in Jmeter
Udp Server Giving Segmentation Fault
Bash Script to Mkdir on Each Line of a File That Has Been Split by a Delimiter
.Dat Attachment Instead of Text Using Mailx in Redhat Linux
Moving a Git Repo to Another Server
Ubuntu Linux: Netbeans Cannot Detect Git or Cordova Installed
Is It Correct to Use The Rc.Local File to Start a Program When The System Starts
Oracle Query - Ora-01652: Unable to Extend Temp Segment But Only in Some Versions of Sql*Plus
Notify Gpio Interrupt to User Space from a Kernel Module
How to Find Out The User of Parent Shell Inside a Child Shell
Why Does High-Memory Not Exist for 64-Bit Cpu
Munin Dynamic Graph Zoom (Dynazoom) Not Working (Centos6,Nginx,PHP-Fpm)
Downgrade R Version and R Package Bioconductor
How to Get The Interrupt Vector Number on Linux