Difference between '.*' and .* in grep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Difference between '.*' and .* in grep
# 1  
Old 01-15-2010
Difference between '.*' and .* in grep

Hi

What is the difference between grep '.*' xx and grep .* xx?
What grep .* xx command will do where xx is file name.

Thanks

Sas
# 2  
Old 01-15-2010
They have completely different meanings
Code:
grep '.*' xx

means print any line from the file xx that contains 0 or more characters. It is equivalent to
Code:
grep '' xx

or just:
Code:
cat xx

Code:
grep .* xx

will only work if there is exactly one file present in the directory that starts with a . and that contains no spaces of special characters. The command will then grep that file name in the file xx and print the lines that contain the part of the file name after the dot preceded by any character.

Neither are useful commands.
# 3  
Old 01-15-2010
Thanks for your reply..

My xx file contains

Code:
[/home/trg_ux/saswata]cat xx

total 32

-rw-r-----   1 trg_ux     users            0 Jan 15 11:06 xx

-rw-r-----   1 trg_ux     users         1118 Jan 15 10:53 yy

-rw-r-----   1 trg_ux     users           29 Jan 14 15:34 datelst.lst

-rw-r-----   1 trg_ux     users          378 Jan 14 14:56 path.lst

-rw-r-----   1 trg_ux     users           84 Jan 14 14:35 dept.lst

-rw-r-----   1 trg_ux     users          248 Jan 14 14:19 emp.lst

-rw-r-----   1 trg_ux     users            3 Jan 13 11:26 devnull

-rwxr-x---   1 trg_ux     users          403 Jan 13 11:25 prob14.15.sh

-rwxr-x---   1 trg_ux     users          139 Jan 13 11:07 LF

drwxr-x---   2 trg_ux     users         1024 Jan 12 18:24 test1

-rwxr-x---   1 trg_ux     users          139 Jan 12 18:22 lf

-rw-r-----   1 trg_ux     users            0 Jan 12 17:31 tr

drwxr-x---   2 trg_ux     users         1024 Jan 11 18:04 test2

-rwxr-x---   1 trg_ux     users          187 Jan 11 18:03 prob15.11.sh

-rw-r-----   1 trg_ux     users          215 Jan 11 13:35 filenames

-rwxr-x---   1 trg_ux     users          159 Jan 11 13:32 prob14.10.sh

-rwxr-x---   1 trg_ux     users          135 Jan 11 11:26 prob14.sh

when i run command it shows following output.

Code:
[/home/trg_ux/saswata]grep .* xx
..:, ¸D*°dä¤lTÐ0MQ.cshrcMR.exrcMS.loginM.profile
                                                .sh_history@hanilniGã
.ksh_history_history@biswajit@kkrishna@lTRAINER@ëmahuya@{       mentoringeRtPriyankaRÉ007hhnRö
                                                                                              Ddatedisp.shR÷øfunc.shtfiRømain.shRüxmanish@`saswatas_nGHÀtestprmyerromyfilesmyfilexyztt2a?can.file1tfile2eexaple_01xexamplecabexamNT.historyexam1tSMemp.shPGsatyakiloexam3janshtraining_kshmylistdead.letterumitraining_ksh.gza1.txt.bkparunavaa2.txt.bkpanupamdipushbidhantsa3.txt.bkp.dipabc.txt.b¼ ¼abhi.txt.bkpchhi.txt.bkpmanls.txt.bkpnmanwc.txt.bkptluna.txtunixtrparo.txt.bkpproggma.txt.bkpsatabdi.txt.bkpsky.txt.bkpsneha.txt.bkpsh.shasgn.shassgn.sha.txtllldate.shMousumibid_deb_suvTest_pp1.shgrouphimadri_adhiraj_shoumikgoutam_tuhinA14mousumi_joydeepA14.txtprob1.shJOYDEEP_MOUSUMIdeb1.txtA16.txtBidhan_Debajyoti_Suvrajitsss.txtA14.txt.bkpA16.txt.bkpa.txt.bkpdeb1.txt.bkpluna.txt.bkpsss.txt.bkpsnehatanushreejoydeepHello.jav¼ ¼Sneha_TestAnupamAvijitArunavaArnabsneha_tanushree_satabdi_poulomi_joydeep_mousmitanushree_sneha_satabdi_poulomi_joydeep_mousmisneha_satabdi_tanushree_poulomi_mousmi_joydeeppoulomi_sneha_tanushree_satabdi_joydeep_mousmiAnupam_Avijit_Arunava_Arnabass2.cppass2.oa.outmydirapratimDBCankKSllDBC1mykkchinkiiichidiff.sh1.txt2.txtvishaldTC++rutmpsttmrctry1Test.cpptemperature.cppfresher_janjanabhi
.sas:xx
xx:
xx:total 32
xx:-rw-r-----   1 trg_ux     users            0 Jan 15 11:06 xx
xx:-rw-r-----   1 trg_ux     users         1118 Jan 15 10:53 yy
xx:-rw-r-----   1 trg_ux     users           29 Jan 14 15:34 datelst.lst
xx:-rw-r-----   1 trg_ux     users          378 Jan 14 14:56 path.lst
xx:-rw-r-----   1 trg_ux     users           84 Jan 14 14:35 dept.lst
xx:-rw-r-----   1 trg_ux     users          248 Jan 14 14:19 emp.lst
xx:-rw-r-----   1 trg_ux     users            3 Jan 13 11:26 devnull
xx:-rwxr-x---   1 trg_ux     users          403 Jan 13 11:25 prob14.15.sh
xx:-rwxr-x---   1 trg_ux     users          139 Jan 13 11:07 LF
xx:drwxr-x---   2 trg_ux     users         1024 Jan 12 18:24 test1
xx:-rwxr-x---   1 trg_ux     users          139 Jan 12 18:22 lf
xx:-rw-r-----   1 trg_ux     users            0 Jan 12 17:31 tr
xx:drwxr-x---   2 trg_ux     users         1024 Jan 11 18:04 test2
xx:-rwxr-x---   1 trg_ux     users          187 Jan 11 18:03 prob15.11.sh
xx:-rw-r-----   1 trg_ux     users          215 Jan 11 13:35 filenames
xx:-rwxr-x---   1 trg_ux     users          159 Jan 11 13:32 prob14.10.sh
xx:-rwxr-x---   1 trg_ux     users          135 Jan 11 11:26 prob14.sh
[/home/trg_ux/saswata]PuTTY

Can you tell me why such a output is displaying

Last edited by pludi; 01-15-2010 at 04:32 AM.. Reason: code tags, please...
# 4  
Old 01-15-2010
You are also grepping inside files / directories that start with a dot. It is not a useful command. You should not use it. What are you trying to accomplish?
# 5  
Old 01-15-2010
Or explained anther way:

.* will glob - be expanded to a list of files that have a leading dot in the filename

'.*' is a regular expression when used as an argument to grep

* is a shell metacharacter, '*' is just an asterisk character which has to be interpreted by a program other than the shell if it is going to have a special meaning.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to calculate difference of split and sum the difference

In the awk I am trying to subtract the difference $3-$2 of each matching $4 before the first _ (underscore) and print that value in $13. I think the awk will do that, but added comments. What I am not sure off is how to add a line or lines that will add sum each matching $13 value and put it in... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Inconsistent `ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l`

i have this line of code that looks for the same file if it is currently running and returns the count. `ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l` basically it is assigned to a variable ISRUNNING=`ps -eaf -o args | grep -i sfs_pcard_load_file.ksh |... (6 Replies)
Discussion started by: wtolentino
6 Replies

3. UNIX for Dummies Questions & Answers

Difference between code page used with grep and vi

Not certain the title is phrased correctly but here's the issue: I need to scrape data out of a pdf file. I ftp the file from windows to unix(solaris 10) as a binary file. Open the file in vi and search for a string, Matrix, but no joy. Then I cat the same file, pipe it to grep 'Matrix' and the... (3 Replies)
Discussion started by: rj0
3 Replies

4. Programming

what is the main difference between difference between using nonatomic lseek and O_APPEND

I think both write at the end of the file ...... but is there a sharp difference between those 2 instruction ..... thank you this is my 3rd question today forgive me :D (1 Reply)
Discussion started by: fwrlfo
1 Replies

5. UNIX for Dummies Questions & Answers

Bash - CLI - grep - Passing result to grep through pipe

Hello. I want to get all modules which are loaded and which name are exactly 2 characters long and not more than 2 characters and begin with "nv" lsmod | (e)grep '^nv???????????? I want to get all modules which are loaded and which name begin with "nv" and are 2 to 7 characters long ... (1 Reply)
Discussion started by: jcdole
1 Replies

6. Shell Programming and Scripting

Difference between 2 grep - commands

Hi, I need to know the difference between this commands: grep * *search* grep "*" *search* As far as i know does the 2nd command search for files which have a name with *search* and greps then all which have chars from a-z in the file content. But was does the first command?? Best... (1 Reply)
Discussion started by: xus
1 Replies

7. UNIX for Dummies Questions & Answers

Difference between grep, egrep & grep -i

Hi All, Please i need to know the difference between grep, egrep & grep -i when used to serach through a file. My platform is SunOS 5.9 & i'm using the korn shell. Regards, - divroro12 - (2 Replies)
Discussion started by: divroro12
2 Replies

8. UNIX Desktop Questions & Answers

Difference grep, egrep and fgrep

Hi All, Can anyone please explain me the difference between grep, egrep and fgrep with examples. I am new to unix environment.. Your help is highly appreciated. Regards, ravi (2 Replies)
Discussion started by: ravind27
2 Replies

9. Shell Programming and Scripting

MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else

Hi Guys, I need to set the value of $7 to zero in case $7 is NULL. I've tried the below command but doesn't work. Any ideas. thanks guys. MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else { print $7}}' ` Harby. (4 Replies)
Discussion started by: hariza
4 Replies

10. UNIX for Dummies Questions & Answers

What is the main difference between grep and sed

What is the main difference between grep and sed (1 Reply)
Discussion started by: ss4u
1 Replies
Login or Register to Ask a Question