Unix C++
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Doubt in grep command

+8
akalya
hansi
Nazneennazim
SakeenaHarris
anand
Christopher
Maithreyi
lalitha
12 posters

Page 1 of 3 1, 2, 3  Next

Go down

Doubt in grep command Empty Re: Doubt in grep command

Post by lalitha Fri Mar 12, 2010 6:54 pm

can u tell me an example wher v use * and . in grep

lalitha

Posts : 14
Points : 16
Join date : 2010-03-08

Back to top Go down

Doubt in grep command Empty Re: Doubt in grep command

Post by Maithreyi Fri Mar 12, 2010 7:03 pm

ls | grep "ab."

Will print all files starting with ab and having one another character, say
abc
abd
ab0
abf

Maithreyi

Posts : 76
Points : 142
Join date : 2010-03-03
Age : 36
Location : Haldia

Back to top Go down

Doubt in grep command Empty Re: Doubt in grep command

Post by Maithreyi Fri Mar 12, 2010 7:03 pm

ls | grep "ab*"

will give files with ab and anynumber of characters
say abcdef, abcd,abc,abd,ab0

Maithreyi

Posts : 76
Points : 142
Join date : 2010-03-03
Age : 36
Location : Haldia

Back to top Go down

Doubt in grep command Empty Re: Doubt in grep command

Post by lalitha Fri Mar 12, 2010 7:05 pm

oh okie... thansk maithu Smile

lalitha

Posts : 14
Points : 16
Join date : 2010-03-08

Back to top Go down

Doubt in grep command Empty Re: Doubt in grep command

Post by Christopher Fri Mar 12, 2010 7:06 pm

if u want to discuss about other topics, create separate thread.
Christopher
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 35

https://unixcpp.forumotion.com

Back to top Go down

Doubt in grep command Empty Re: Doubt in grep command

Post by lalitha Fri Mar 12, 2010 7:11 pm

[@oracleclient ~]$ cat>ram
ali
amri
ajay
amit
anil
[@oracleclient ~]$ grep "am." ram
amri
amit
[@oracleclient ~]$ grep "aj*" ram
ali
amri
ajay
amit
anil
[@oracleclient ~]$ grep "aj." ram
ajay
wy did this happen

lalitha

Posts : 14
Points : 16
Join date : 2010-03-08

Back to top Go down

Doubt in grep command Empty Re: Doubt in grep command

Post by lalitha Fri Mar 12, 2010 7:14 pm

oh sryc hris... i jus had an accompanyin doubt

lalitha

Posts : 14
Points : 16
Join date : 2010-03-08

Back to top Go down

Doubt in grep command Empty Re: Doubt in grep command

Post by anand Fri Mar 12, 2010 7:22 pm

actually the * here means like zero or more occurence of the particular alphabet.....
so in aj* it ll print all the words startin with a or aj or ajj

where j can zero or more occurrences
anand
anand

Posts : 55
Points : 70
Join date : 2010-02-26
Age : 36

Back to top Go down

Doubt in grep command Empty Re: Doubt in grep command

Post by lalitha Mon Mar 15, 2010 10:55 am

oh got t Smile

lalitha

Posts : 14
Points : 16
Join date : 2010-03-08

Back to top Go down

Doubt in grep command Empty Re: Doubt in grep command

Post by SakeenaHarris Mon Mar 15, 2010 11:36 am

why did "aj." print ajay... dot means any one charac more,doe
sn't it?

SakeenaHarris

Posts : 7
Points : 13
Join date : 2010-03-08

Back to top Go down

Doubt in grep command Empty Re: Doubt in grep command

Post by lalitha Mon Mar 15, 2010 11:41 am

i think aj. will print all entries startin wit aj...

lalitha

Posts : 14
Points : 16
Join date : 2010-03-08

Back to top Go down

Doubt in grep command Empty Re: Doubt in grep command

Post by Christopher Mon Mar 15, 2010 11:42 am

when u r using it in grep, it means anyline which contains "ab+any one character". ajay has (ab+a), so obviously it ll display. Suppose if u want to search for a complete word, use

Code:
grep -w 'aj.' <filename>

It ll not display 'ajay'
Christopher
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 35

https://unixcpp.forumotion.com

Back to top Go down

Doubt in grep command Empty Re: Doubt in grep command

Post by lalitha Mon Mar 15, 2010 11:46 am

[@oracleclient ~]$ grep "j." ram
ajay
[@oracleclient ~]$ grep "n." ram
anil
[@oracleclient ~]$ grep "m." ram
amri
amit

lalitha

Posts : 14
Points : 16
Join date : 2010-03-08

Back to top Go down

Doubt in grep command Empty Re: Doubt in grep command

Post by lalitha Mon Mar 15, 2010 11:48 am

wat does this dot exactly do??
if my file content is this way
[@oracleclient ~]$ cat ram
ali
amri
ajay
amit
anil
aj
an
still t prints this op
[@oracleclient ~]$ grep "j." ram
ajay
[@oracleclient ~]$ grep "aj." ram
ajay
wats the logic

lalitha

Posts : 14
Points : 16
Join date : 2010-03-08

Back to top Go down

Doubt in grep command Empty Re: Doubt in grep command

Post by Christopher Mon Mar 15, 2010 11:51 am

by default it wont search for complete word. Even if a part of a word matches the search pattern, it ll be displayed.

grep "j." ram -> ajay
grep "aj." ram -> ajay
Christopher
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 35

https://unixcpp.forumotion.com

Back to top Go down

Doubt in grep command Empty Re: Doubt in grep command

Post by Nazneennazim Mon Mar 15, 2010 11:59 am

oracleclient march]$ grep -i -e 'fruit' | -f list1 list2
-bash: -f: command not found

Followed the syntax given, but getting an error

Nazneennazim

Posts : 10
Points : 16
Join date : 2010-03-01

Back to top Go down

Doubt in grep command Empty Re: Doubt in grep command

Post by Nazneennazim Mon Mar 15, 2010 12:00 pm

-f is used to mention more than one file to search for the pattern from.
Am i right or wrong?

Nazneennazim

Posts : 10
Points : 16
Join date : 2010-03-01

Back to top Go down

Doubt in grep command Empty Re: Doubt in grep command

Post by hansi Mon Mar 15, 2010 12:17 pm

grep "xy." <filename>,,,,,,,,vl print al d lines starting frm 'xy' containing ny no of char
grep "xy*" <filename>,,,,,,,,vl print al d lines containing 'x' anywhere in d line,,,it has nthng 2 do wid 'y'


$ cat > grepdemo.txt

iftthen
ifelse
found
print
hault
answer
$ grep "if." grepdemo.txt

iftthen
ifelse
$ grep "fi." grepdemo.txt

$ grep "fi*" grepdemo.txt

iftthen
ifelse
found
$ grep "ia*" grepdemo.txt

iftthen
ifelse
print

hansi

Posts : 6
Points : 8
Join date : 2010-03-03
Age : 37
Location : u.p.

Back to top Go down

Doubt in grep command Empty Re: Doubt in grep command

Post by Christopher Mon Mar 15, 2010 12:34 pm

-f is used to search for multiple patterns. Also search patterns are stored in separate file.

Example

Code:
$ cat 1.txt
orange yellow red
red green black
black white yellow
blue pink cyan
red green blue
white black cyan

$ cat search.txt
black
cyan

$ grep -f search.txt 1.txt
red green black
black white yellow
blue pink cyan
white black cyan

Instead of giving multiple patterns as arguments using -e switch, u can save it in a separate file and pass tat file as an argument to grep command using -f switch.
Christopher
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 35

https://unixcpp.forumotion.com

Back to top Go down

Doubt in grep command Empty Re: Doubt in grep command

Post by Nazneennazim Mon Mar 15, 2010 12:46 pm

Thank you.

Nazneennazim

Posts : 10
Points : 16
Join date : 2010-03-01

Back to top Go down

Doubt in grep command Empty Re: Doubt in grep command

Post by akalya Mon Mar 15, 2010 4:49 pm

wat is the difference between ...

grep [a-z] filename and grep [!a-z] filename ???

[335802@oracleclient ~]$ cat list1
mango
orange
pineapple
lemons
[335802@oracleclient ~]$ grep [a-z] list1
mango
orange
pineapple
lemons
[335802@oracleclient ~]$ grep [!a-z] list1
mango
orange
pineapple
lemons

akalya

Posts : 70
Points : 86
Join date : 2010-03-04

Back to top Go down

Doubt in grep command Empty Re: Doubt in grep command

Post by Christopher Mon Mar 15, 2010 4:55 pm

'!' doesn't have any significance. it ll be considered jus like a character.

Code:
$ cat 1.txt
orange yellow red
red blue green
white black cyan
cyan pink orange
red! blue! green!
BLUE! WHITE! BLACK!

$ grep '[a-z]' 1.txt
orange yellow red
red blue green
white black cyan
cyan pink orange
red! blue! green!

$ grep '[!a-z]' 1.txt
orange yellow red
red blue green
white black cyan
cyan pink orange
red! blue! green!
BLUE! WHITE! BLACK!

$ grep '[!]' 1.txt
red! blue! green!
BLUE! WHITE! BLACK!

hope u can notice the difference.
Christopher
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 35

https://unixcpp.forumotion.com

Back to top Go down

Doubt in grep command Empty Re: Doubt in grep command

Post by Maithreyi Mon Mar 15, 2010 4:59 pm

! doesn't seem to have a significance in grep.However, ^ means negation.

Suppose cat >> list1
1234

now grep [a-z] list1
mango
orange
pineapple
lemons

grep [!a-z] list1
mango
orange
pineapple
lemons

but,
grep [^a-z] list1
1234

Maithreyi

Posts : 76
Points : 142
Join date : 2010-03-03
Age : 36
Location : Haldia

Back to top Go down

Doubt in grep command Empty Re: Doubt in grep command

Post by Christopher Mon Mar 15, 2010 5:02 pm

Yes. ^ inside square bracket means 'negation' and ^ outside square bracket means 'starts with'.
Christopher
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 35

https://unixcpp.forumotion.com

Back to top Go down

Doubt in grep command Empty Re: Doubt in grep command

Post by akalya Mon Mar 15, 2010 5:04 pm

jus like j* denotes 0 or more occurence...how do i specify 1 or more occurence in a grep command???

akalya

Posts : 70
Points : 86
Join date : 2010-03-04

Back to top Go down

Doubt in grep command Empty Re: Doubt in grep command

Post by Sponsored content


Sponsored content


Back to top Go down

Page 1 of 3 1, 2, 3  Next

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum