1822818 Members
4258 Online
109645 Solutions
New Discussion юеВ

question on grep

 
navin
Super Advisor

question on grep

Hi ,
what options should i use to grep a pattern in the file ,the entries in the file looks like below

df,gh,ww:user name:home dir
gh:user name :home dir
ww,gh:username:home dir

i need to grep a entry that does have only a gh in the first column

any idea , much appreciated
Learning ...
5 REPLIES 5
Matti_Kurkela
Honored Contributor

Re: question on grep

I assume the "columns" (rather, fields) are separated by colons (':')?

"Only gh in the first field" is equal to
+ gh + .

So, the command would be:

grep '^gh:' somefile

MK
MK
Steven Schweda
Honored Contributor

Re: question on grep

man grep
man regexp

dyi # echo 'gh:user' | grep '^gh'
gh:user
dyi # echo 'df,gh,ww:user' | grep '^gh'
dyi #
James R. Ferguson
Acclaimed Contributor

Re: question on grep

Hi:

If the columns are delimited with ":" delimiters, you could use:

# awk -F: '$1~/gh/ {print}' file

Regards!

...JRF...
Raj D.
Honored Contributor

Re: question on grep

Navin,

- You can do it with awk:

$ awk -F ":" '{if ($1 ~ /gh/ ) print $0}' file_name





Example:
$ cat file_name

df,gh,ww:user name:home dir
gh:user name :home dir
ww,gh:username:home dir
ww,fh:username:home dir
ww,dh:username:home dir



$ awk -F ":" '{if ($1 ~ /gh/ ) print $0}' filename
df,gh,ww:user name:home dir
gh:user name :home dir
ww,gh:username:home dir




Cheers,
Raj.
" If u think u can , If u think u cannot , - You are always Right . "
Steven Schweda
Honored Contributor

Re: question on grep

> I assume [...]

> If the columns [...]

As usual, a more precise definition of the
problem might reduce the guesswork, and lead
to more accurate answers.

> [...] a gh in the first column

Define "column". Here, "g" is "in the first
column". "h" is in the second column. Are
you looking for "gh" which begins in column
one, or what, exactly?

Are you worried about columns, or
colon-separated fields, or what, exactly?