Regular expression doesn't work in grep - regex

Why grep doesn't match "COL1,COL2,COL3," with this regexp as expected but "COL1,COL2,COL3,COL4,COL5,COL6,"? It matches correctly in text editor but not using grep, am I missing any special escaping or..? (using OS X Lion)
The text:
COL1,COL2,COL3,COL4,COL5,COL6,COL7,COL8,COL9
The command:
grep -E --color=auto '^([^,]*,){3}' file.csv
Grep version:
grep (GNU grep) 2.5.1

Your command:
grep -E --color=auto '^([^,]*,){3}' file.csv
will only color the string COL1,COL2,COL3, differently but if you want that string in output then use -o option like this:
grep -E -o '^([^,]*,){3}'

Related

Regex doesn't work in grep

Try search for filenames in file.txt with this regexp: ([\w.-]+)[.]\w+/gm
On regexr.com it works good, but when I try to find them with grep with this command I get nothing:
grep -E "([\w.-]+)[.]\w+/gm" file.txt
What am I doing wrong?
Input:
hello.py fasdfasdf
fadsfsdf
f
file.docx fsdfasdf
fadsfsdf.fds
FILE.mp3
Output:
hello.py
file.docx
fadsfsdf.fds
FILE.mp3
\w is a Perl extension; either use the -P option with grep (if supported), or use a standard regular expression instead:
grep -E '([[:alpha:].-]+)[.][[:alpha:]]+/gm' file.text

grep with extended regex over multiple lines

I'm trying to get a pattern over multiple lines. I would like to ensure the line I'm looking for ends in \r\n and that there is specific text that comes after it at some point. The two problems I've had are I often get unmatched parenthesis in groupings or I get a positive match when there is none. Here are two simple examples.
echo -e -n "ab\r\ncd" | grep -U -c -z -E $'(\r\n)+.*TEST'
grep: Unmatched ( or \(
What exactly is unmatched there? I don't get it.
echo -e -n "ab\r\ncd" | grep -U -c -z -E $'\r\n.*TEST'
1
There is no TEST in the string, so why does this return a count of 1 for matches?
I'm using grep (GNU grep) 2.16 on Ubuntu 14. Thanks
Instead of -E you can use -P for PCRE support in gnu grep to use advanced regex like this:
echo -ne "ab\r\ncd" | ggrep -UczP '\r\n.*TEST'
0
echo -ne "ab\r\ncd" | ggrep -UczP '\r\n.*cd'
1
grep -E matches only in single line input.

Regular expression in Unix using or operator

I'm trying to print,using grep, lines which contains vasile or line which contains ion . This is command but it doesn't work:
grep (vasile|ion) test.txt
I don't need this :
grep vasile test.txt | grep ion test.txt
try,
terminal$ grep -e vasile -e ion test.txt
Other way using OR operator | in grep
terminal$ grep 'vasile\|ion' test.txt
If you use awk, you can do:
awk '/vasile|ion/' test.txt
awk '/vasile/ || /ion/' test.txt
Try alternation with Grep's extended regex option:
grep -E 'vasile |ion' file
This should work with all Posix greps. \| is a GNU extension to BRE..

regex for grep for screen resolution of the form 1280x720

I am trying to give grep a regex pattern for screen resolution(e.g. 1280x720) as following
[0-9]{3,}x[0-9]{3,}
but it doesn't seem to be working.
The following works but that doesn't translate to the above one.
[0-9][0-9][0-9][0-9]*x[0-9][0-9][0-9][0-9]*
That is an ERE (extended regular expression), grep uses BREs (basic regular expressions by default. You can either escape the {}:
grep '[0-9]\{3,\}x[0-9]\{3,\}'
or tell grep to interpret it as an ERE:
grep -E '[0-9]{3,}x[0-9]{3,}'
Your regex seems fine for bash :
[[ 1280x720 =~ [0-9]{3,}x[0-9]{3,} ]] && echo OK
OK
If you want to use grep :
$ cat B
640x480
$
$ grep -c "[0-9]\{3,\}x[0-9]\{3,\}" B
1
$
$ grep --version
grep (GNU grep) 2.14

help with grep [[:alpha:]]* -o

file.txt contains:
##w##
##wew##
using mac 10.6, bash shell, the command:
cat file.txt | grep [[:alpha:]]* -o
outputs nothing. I'm trying to extract the text inside the hash signs. What am i doing wrong?
(Note that it is better practice in this instance to pass the filename as an argument to grep instead of piping the output of cat to grep: grep PATTERN file instead of cat file | grep PATTERN.)
What shell are you using to execute this command? I suspect that your problem is that the shell is interpreting the asterisk as a wildcard and trying to glob files.
Try quoting your pattern, e.g. grep '[[:alpha:]]*' -o file.txt.
I've noticed that this works fine with the version of grep that's on my Linux machine, but the grep on my Mac requires the command grep -E '[[:alpha:]]+' -o file.txt.
sed 's/#//g' file.txt
/SCRIPTS [31]> cat file.txt
##w##
##wew##
/SCRIPTS [32]> sed 's/#//g' file.txt
w
wew
if you have bash >3.1
while read -r line
do
case "$line" in
*"#"* )
if [[ $line =~ "^#+(.*)##+$" ]];then
echo ${BASH_REMATCH[1]}
fi
esac
done <"file"