using linux grep with look ahead regexp - regex

I have string in txt file
cat list.txt
userone#ex.com, usertwo#ex.com, userthree#ex.com
and i want to print every user login w/o #ex.com each new line and try to use regexp with linux grep
grep -oe '[a-z](?=#ex.com,)' list.txt
but nothing happens, why? It will be like:
userone
usertwo
userthree
Thanks.

Without grep -P, you can use grep + cut:
grep -oE '[^# ]+#ex\.com' list.txt | cut -d# -f1
userone
usertwo
userthree
With gnu grep:
grep -oP '[^# ]+(?=#ex\.com)' list.txt
userone
usertwo
userthree

Related

Grep first line which contain a date

I'm trying to fetch the first line in a log file which contain a date.
Here is an example of the log file :
SOME
LOG
2021-1-1 21:50:19.0|LOG|DESC1
2021-1-4 21:50:19.0|LOG|DESC2
2021-1-5 21:50:19.0|LOG|DESC3
2021-1-5 21:50:19.0|LOG|DESC4
In this context I need to get the following line:
2021-1-1 21:50:19.0|LOG|DESC1
An other log file example :
SOME
LOG
21-1-3 21:50:19.0|LOG|DESC1
21-1-3 21:50:19.0|LOG|DESC2
21-1-4 21:50:19.0|LOG|DESC3
21-1-5 21:50:19.0|LOG|DESC4
I need to fetch :
21-1-3 21:50:19.0|LOG|DESC1
At the moment I tried the following command :
cat /path/to/file | grep "$(date +"%Y-%m-%d")" | tail -1
cat /path/to/file | grep "$(date +"%-Y-%-m-%-d")" | tail -1
cat /path/to/file | grep -E "[0-9]+-[0-9]+-[0-9]" | tail -1
In case you are ok with awk, could you please try following. This will find the matched regex first line and exit from program, which will be faster since its NOT reading whole Input_file.
awk '
/^[0-9]{2}([0-9]{2})?-[0-9]{1,2}-[0-9]{1,2} [0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]+/{
print
exit
}' Input_file
Using sed, being not too concerned about exactly how many digits are present:
sed -En '/^[0-9]+-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+[.][0-9]+[|]/ {p; q}' file
$ grep -m1 '^[0-9]' file1
2021-1-1 21:50:19.0|LOG|DESC1
$ grep -m1 '^[0-9]' file2
21-1-3 21:50:19.0|LOG|DESC1
If that's not all you need then edit your question to provide more truly representative sample input/output.
A simple grep with -m 1 (to exit after finding first match):
grep -m1 -E '^([0-9]+-){2}[0-9]+ ([0-9]{2}:){2}[0-9]+\.[0-9]+' file1
2021-1-1 21:50:19.0|LOG|DESC1
grep -m1 -E '^([0-9]+-){2}[0-9]+ ([0-9]{2}:){2}[0-9]+\.[0-9]+' file2
21-1-3 21:50:19.0|LOG|DESC1
This sed works with either GNU or POSIX sed:
sed -nE '/^[[:digit:]]{2,4}-[[:digit:]]{1,2}-[[:digit:]]{1,2}/{p;q;}' file
But awk, with the same BRE, is probably better:
awk '/^[[:digit:]]{2,4}-[[:digit:]]{1,2}-[[:digit:]]{1,2}/{print; exit}' file

Using grep regex to select to first hyphen

echo "Linux/DEB/mainbinary-0.1.20190424165331-0-armdef.deb" | grep -oE "([^\/]+$)"
This prints just the filename, without the directory structure, but I cannot manage to print just mainbinary from that string. Suggestions?
And a sed alternative to PS.'s great grep -oP
echo "Linux/DEB/mainbinary-0.1.20190424165331-0-armdef.deb" |sed -r 's#^.*/([^-]+).*#\1#'
mainbinary
echo "Linux/DEB/mainbinary-0.1.20190424165331-0-armdef.deb" |grep -oP '.*/\K[^-]+'
mainbinary
This will scan till last / and ignore everything to its left and keep moving until - (excluding)
With any awk in any shell on any UNIX machine:
$ echo "Linux/DEB/mainbinary-0.1.20190424165331-0-armdef.deb" | awk -F'[/-]' '{print $3}'
mainbinary

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..

What is the Unix command to display all lines of a file with two certain strings

Basically, I have a file that I want to search and display only the lines that have the strings 'abc' and 'vhg'. What is the Unix command for this?
You can use grep for it:
grep abc file.txt | grep vhg
OR
you can use awk:
awk '/abc/ && /vhg/' file.txt
One more way with grep:
grep .*abc.*vhg file.txt
Use the grep command.
grep 'word1\|word2\|word3' /path/to/file
Example:
grep 'abc\|vhg' filename
Since a sed solution has not yet been given:
sed -n '/abc/{ /vhg/p; }'

Detect Russian characters with grep

I'm trying to detect Russian characters with grep, but what I have at the moment does not appear to be doing anything:
echo "Ёё" | grep -Eo "/[А-Яа-яЁё]/u"
No output is returned. Is there anything I have to do to tell grep to return the output?
there is no output because grep is looking for pattern /yourletters/u
try this:
echo "Ёё" | grep -Eo "[А-Яа-яЁё]*"
test here:
kent$ echo "Ёё" | grep -Eo "[А-Яа-яЁё]*"
Ёё