LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-22-2015, 09:55 AM   #1
publicLearner
LQ Newbie
 
Registered: Mar 2015
Location: Washington, DC
Distribution: Mac 10.10 Yosemite
Posts: 24

Rep: Reputation: Disabled
grep exact word - to find file which start with some word


Hi Team,
I'm stuck with a small thing of grepping file names which have exact start word, or start with a specified word.

Quote:
g="19_S22_L001_R1_001.fastq.gz"
Code:
echo $g | grep  -w "19"
This gives me no output

The reason I'm trying to grep so, is I'm dealing with ~150+ files, and I'll have files with same pattern but different starts say 1 or 19

Code:
echo $g | grep "^19"
and

Code:
echo $g | grep "^1"
will give me same results.

Linux version:

Linux version 2.6.32-504.12.2.el6.x86_64 (mockbuild@x86-027.build.eng.bos.redhat.com) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-9) (GCC) ) #1 SMP Sun Feb 1 12:14:02 EST 2015


How do I overcome this barrier?

Kindly help

Last edited by publicLearner; 07-22-2015 at 09:57 AM.
 
Old 07-22-2015, 10:00 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
underscore (_) is considered a letter; what does this yield
Code:
echo $g | grep "^19_"
echo $g | grep "^1_"
 
1 members found this post helpful.
Old 07-22-2015, 10:04 AM   #3
publicLearner
LQ Newbie
 
Registered: Mar 2015
Location: Washington, DC
Distribution: Mac 10.10 Yosemite
Posts: 24

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by schneidz View Post
underscore (_) is considered a letter; what does this yield
Code:
echo $g | grep "^19_"
echo $g | grep "^1_"
Hi schneidz,

Thanks for your prompt reply.
Code:
echo $g | grep "^19_"
This works.!

Last edited by publicLearner; 07-22-2015 at 10:05 AM.
 
Old 07-22-2015, 10:20 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I am a little confused ... you talk about finding file names but are using grep? Wouldn't you just use find?
Code:
find path -name '19*'
 
Old 07-22-2015, 11:33 AM   #5
publicLearner
LQ Newbie
 
Registered: Mar 2015
Location: Washington, DC
Distribution: Mac 10.10 Yosemite
Posts: 24

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
I am a little confused ... you talk about finding file names but are using grep? Wouldn't you just use find?
Code:
find path -name '19*'
Hi grail,
Thanks for your attention to my query and putting awesome question.

Output from below command:
Code:
find -type f -name "1*"
shall be same as with:

Code:
find -type f -name "19*"
provided I've files present with 19 and 1 both in designated directory.
 
Old 07-22-2015, 11:35 AM   #6
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
sigh, what does this yeild:
Code:
find -type f -name "1_*"
find -type f -name "19_*"
 
1 members found this post helpful.
Old 07-22-2015, 12:19 PM   #7
publicLearner
LQ Newbie
 
Registered: Mar 2015
Location: Washington, DC
Distribution: Mac 10.10 Yosemite
Posts: 24

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by schneidz View Post
sigh, what does this yeild:
Code:
find -type f -name "1_*"
find -type f -name "19_*"
It yields correct output. Thanks.

PS: This is so embarrassing for me.
 
Old 07-22-2015, 05:54 PM   #8
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
"locate" is much less resource intensive, and supports regex - may also be an option.
 
Old 07-23-2015, 03:12 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Don't be embarrassed ... no one here was a guru the first time they used linux
 
  


Reply

Tags
grep, linux



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to grep for an exact word? Arpitha10 Linux - Newbie 8 12-16-2012 06:23 AM
[SOLVED] Find/Replace shell script that replaces word with other word in text, filenames yanom Programming 8 09-12-2012 12:29 AM
Find/grep/wc command to find matching files, print filename and word count dbasch Linux - Newbie 10 09-14-2009 05:55 PM
command for grep the exact word nagendrar Linux - Newbie 7 04-29-2009 02:33 PM
[SOLVED] find a word in a file, and change a word beneath it ?? vikas027 Programming 10 02-14-2008 09:46 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration