LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 01-16-2013, 09:50 PM   #1
Winanjaya
Member
 
Registered: Sep 2003
Posts: 209

Rep: Reputation: 32
list filtered file name


Dear All,

I have list of .WAV files in a directory as below example,

out-98630654-3001-20130116-173003-1358332203.43491.WAV
out-98630654-5000-20130116-074952-1358297392.40213.WAV
out-9873732-3003-20130116-141020-1358320220.42368.WAV
out-98817777-3004-20130116-145220-1358322740.42646.WAV
out-98817777-3001-20130116-091621-1358302581.40686.WAV
out-98817777-3006-20130116-082038-1358299238.40374.WAV


how to list files which belong to 3001 only (like below) ? ..

out-98630654-3001-20130116-173003-1358332203.43491.WAV
out-98817777-3001-20130116-091621-1358302581.40686.WAV

please help

Thanks & Regards
 
Old 01-16-2013, 09:53 PM   #2
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,130
Blog Entries: 2

Rep: Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825
Code:
ls *-3001-*
should do the job.
 
1 members found this post helpful.
Old 01-18-2013, 08:18 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
You don't even need ls, if you just want a list of files. Since the globbing pattern is expanded by the shell before the command is run, ls does nothing except print them out. In which case you might as well just use the built in echo or printf. Use the latter if you want the output formatted on separate lines.

Code:
echo *-3001-*
printf '%s\n' *-3001-*
You only need to call on ls for its advanced listing features such as -l.


BTW, beware of the classic parsing ls problem too. If you want to do something with the files, just use the globbing pattern in a for loop, or else store the results in an array first, and use that.

Code:
for fname in *-3001-* ; do
  <some command with "$fname">
done

array=( *-3001-* )

printf '%s\n' "${array[@]}"

for fname in "${array[@]}"; do
  <some command with "$fname">
done

PS: please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.
 
Old 01-18-2013, 09:10 AM   #4
alieblice
Member
 
Registered: Jul 2011
Posts: 80

Rep: Reputation: Disabled
this will work too

Code:
$ dir | grep /*-3001-/*
i didn't aware of that usage for echo . thanks for the tip.
 
Old 01-18-2013, 01:17 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
dir is just a synonym for ls. They're the same command. The parsingls link I gave above applies to grepping it as well. i.e. it won't correctly handle filenames that include embedded newlines.

In addition, you'd need to use the -1 option ("one", not "ell") to get the output in one-file-per-line order before grep could work on it correctly, and grep uses regular expressions for pattern matching, not globbing.

Code:
ls -1 | grep '-3001-'
Unlike globs, which have to match the entire string, regexes are unbounded by default. They can match a substring anywhere on the line. So just using the fixed text alone will do the job (fixed text strings count as proper regex patterns).

For what it's worth though, the regex version of '*' is '.*', so this is equivalent:

Code:
ls -1 | grep '.*-3001-.*'
Finally, you need to use backslashes to escape reserved characters, not forward slashes. Quotes are usually easier to read though.

Last edited by David the H.; 01-18-2013 at 01:24 PM. Reason: fixt link + some clarification
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Nautilus file manager List file background problem rng Linux - General 5 08-22-2012 08:22 PM
[SOLVED] AWK parsing a CVS file with a seperate list file alexwely Programming 7 03-22-2012 09:51 AM
[SOLVED] splitting of comma separated values file (horizontal) into a list (vertical ) list Donoughue Linux - Newbie 3 10-20-2011 02:18 PM
how to pick random file name from a list of filenames in a text file. pdklinux79 Linux - Newbie 9 06-20-2008 03:46 PM
Need to find if a matching file exist from a list of possible file names wit_273 Linux - General 5 10-25-2007 10:47 AM


All times are GMT -5. The time now is 06:39 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration