Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep a particular content before a period

Tags:

linux

awk

I am trying to read/grep a particular word or content that is before a period (.).

e.g. file1 has abinaya.ashok and I want to grep whatever is before the period (.) without hardcoding anything.

if I try

grep \.\ file1  

it gives abinaya.ashok. I've tried: grep\*\.\ file1

it doesn't give anything.Can we find it using grep commands or should we do it only using awk command? Any thoughts?

like image 422
javalearner Avatar asked Sep 01 '25 23:09

javalearner


1 Answers

Using GNU grep for PCRE regex (for non-greedy and positive look-ahead), you can do:

echo 'abinaya.ashok' | grep -oP '.*?(?=\.)'
abinaya

Using awk:

echo 'abinaya.ashok' | awk -F\. '{print $1}'
abinaya
like image 111
jaypal singh Avatar answered Sep 03 '25 14:09

jaypal singh