LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 03-08-2013, 05:05 AM   #1
vasu.dc
LQ Newbie
 
Registered: Mar 2013
Posts: 3

Rep: Reputation: Disabled
Help with grep


hi,

I have zipped files with naming having date.i wanted to search for text inside these files but when i search for the ls -l | grep [01-25]
its not giving files untill date 25 feb.can you please suggest how to do a ls -l | grep so that i will get file up to particular date ?

Thanks in Advance.

-rw-r--r-- 1 abc abc 37078862 Feb 1 00:02 app_info.2013-02-01.gz
-rw-r--r-- 1 abc abc 35471491 Feb 2 00:03 app_info.2013-02-02.gz
-rw-r--r-- 1 abc abc 19543573 Feb 3 00:01 app_info.2013-02-03.gz
-rw-r--r-- 1 abc abc 18585595 Feb 4 00:00 app_info.2013-02-04.gz
-rw-r--r-- 1 abc abc 38664707 Feb 5 00:03 app_info.2013-02-05.gz
-rw-r--r-- 1 abc abc 36879179 Feb 6 00:03 app_info.2013-02-06.gz
-rw-r--r-- 1 abc abc 37295560 Feb 7 00:06 app_info.2013-02-07.gz
-rw-r--r-- 1 abc abc 37128476 Feb 8 00:03 app_info.2013-02-08.gz
-rw-r--r-- 1 abc abc 34766336 Feb 9 00:03 app_info.2013-02-09.gz
-rw-r--r-- 1 abc abc 18285163 Feb 10 00:00 app_info.2013-02-10.gz
-rw-r--r-- 1 abc abc 16640646 Feb 11 00:01 app_info.2013-02-11.gz
-rw-r--r-- 1 abc abc 39581054 Feb 12 23:50 app_info.2013-02-12.gz
-rw-r--r-- 1 abc abc 38944135 Feb 13 23:50 app_info.2013-02-13.gz
-rw-r--r-- 1 abc abc 36750165 Feb 14 23:50 app_info.2013-02-14.gz
-rw-r--r-- 1 abc abc 35433769 Feb 15 23:50 app_info.2013-02-15.gz
-rw-r--r-- 1 abc abc 18497085 Feb 16 23:50 app_info.2013-02-16.gz
-rw-r--r-- 1 abc abc 20975963 Feb 17 23:50 app_info.2013-02-17.gz
-rw-r--r-- 1 abc abc 43045197 Feb 18 23:50 app_info.2013-02-18.gz
-rw-r--r-- 1 abc abc 38340962 Feb 19 23:50 app_info.2013-02-19.gz
-rw-r--r-- 1 abc abc 37609717 Feb 20 23:50 app_info.2013-02-20.gz
-rw-r--r-- 1 abc abc 38025994 Feb 21 23:50 app_info.2013-02-21.gz
-rw-r--r-- 1 abc abc 36587754 Feb 22 23:50 app_info.2013-02-22.gz
-rw-r--r-- 1 abc abc 18153026 Feb 23 23:50 app_info.2013-02-23.gz
-rw-r--r-- 1 abc abc 17589326 Feb 24 23:50 app_info.2013-02-24.gz
-rw-r--r-- 1 abc abc 41002618 Feb 25 23:51 app_info.2013-02-25.gz
-rw-r--r-- 1 abc abc 38500342 Feb 26 23:50 app_info.2013-02-26.gz
-rw-r--r-- 1 abc abc 37035193 Feb 27 23:50 app_info.2013-02-27.gz
-rw-r--r-- 1 abc abc 34341629 Feb 28 23:50 app_info.2013-02-28.gz

Last edited by vasu.dc; 03-08-2013 at 05:06 AM.
 
Old 03-08-2013, 05:11 AM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
These seems simple zipped files. You can use zcat command to view content of these files without extracting them:
Code:
~$ man zcat
~$ zcat filename.gz
~$ zcat filename.gz | grep <string>
 
Old 03-08-2013, 06:06 AM   #3
vasu.dc
LQ Newbie
 
Registered: Mar 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by shivaa View Post
These seems simple zipped files. You can use zcat command to view content of these files without extracting them:
Code:
~$ man zcat
~$ zcat filename.gz
~$ zcat filename.gz | grep <string>
Shiva,

i am not asking how to view zipped files.what i want is how to use grep in the ls -l | grep [01-25] so that i get only file names out till 25fth feb.
 
Old 03-08-2013, 06:57 AM   #4
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Then what you're using isn't correct. Instead use:
Code:
~$ ls -la | egrep "app.info-2013-02-[0-1][1-9]|app.info-2013-02-2[0-5]"

Last edited by shivaa; 03-08-2013 at 07:49 AM. Reason: Command updated
 
Old 03-08-2013, 07:01 AM   #5
mandyapenguin
Member
 
Registered: Nov 2011
Location: India
Distribution: RedHat, Cent OS, Fedora, Debian, Ubuntu
Posts: 106

Rep: Reputation: Disabled
Then skip those files(Feb 26th, 27th & 28th) and grep only those what you are expecting.
Code:
ls -l | egrep -vw 'app_info.2013-02-26.gz|app_info.2013-02-27.gz|app_info.2013-02-28.gz'
Or to print only files
Code:
ls -l | egrep -vw 'app_info.2013-02-26.gz|app_info.2013-02-27.gz|app_info.2013-02-28.gz' | awk '{ print $NF }'

Last edited by mandyapenguin; 03-08-2013 at 07:08 AM.
 
Old 03-08-2013, 07:08 AM   #6
blnl
Member
 
Registered: Nov 2009
Location: The Netherlands
Distribution: Fedora
Posts: 65

Rep: Reputation: 0
Try this
Code:
ls -l | grep -v "Feb 2[6-9]"
or
Code:
ls -l | grep -v "02-2[6-9]"
 
Old 03-08-2013, 07:21 AM   #7
mandyapenguin
Member
 
Registered: Nov 2011
Location: India
Distribution: RedHat, Cent OS, Fedora, Debian, Ubuntu
Posts: 106

Rep: Reputation: Disabled
Or even this.
Code:
ls | head -n25
or
Code:
ls -rt | head -n25
 
Old 03-08-2013, 09:16 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Or use find as in:
Code:
$ touch -t 201302252359 /tmp/pivot
$ find . -name app_info.\*.gz -not -newer /tmp/pivot -ls | sort -k11
The grey part is only to test the results.
 
Old 03-09-2013, 10:59 AM   #9
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
The main problem here is that parsing ls for filenames or metadata is always fraught with difficulty. There's rarely any need for it anyway.

For simple name matching you can usually use simple globbing patterns to get the files you want. And bash's extended globbing will give you even more control.


For more advanced matches, as in this case, you should usually use find, as colucix demonstrated just above.

Speaking of which, recent versions of gnu find have a new -newerXY option, eliminating the need for the external reference file.

Code:
find . -name 'app_info.*.gz' -not -newermt 2013-02-26 -exec bash -c "zcat '{}' | grep <searchstring>" \;
Note the use of the separate bash subshell in -exec to handle the complex command. It might be better to create a separate shell script for it instead.

Here are a couple of other related links too:

How can I find the latest (newest, earliest, oldest) file in a directory?
http://mywiki.wooledge.org/BashFAQ/003

How can I get the newest (or oldest) file from a directory?
http://mywiki.wooledge.org/BashFAQ/099

How do I check whether my file was modified in a certain month or date range?
http://mywiki.wooledge.org/BashFAQ/103
 
Old 03-11-2013, 02:12 AM   #10
vasu.dc
LQ Newbie
 
Registered: Mar 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thumbs up [SOLVED] Linux - Newbie

Thanks all for the help/inputs.using $ ls -la | egrep "app.info-2013-02-[0-1][1-9]|app.info-2013-02-2[0-5]" works for me

although other gurus comments have helped me how to look at "find" in a other way.

Thanks once agian.
 
  


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
Creating an alias in ksh that uses grep and includes 'grep -v grep' doug248 Linux - Newbie 2 08-05-2012 03:07 PM
grep has no effect - does not grep anything in this for loopa LinuxChiq Linux - Newbie 2 12-01-2011 10:03 PM
[SOLVED] Grep -p for Linux, Trying to grep a paragraph. ohijames Linux - Newbie 5 07-22-2010 03:09 PM
Trying to understand pipes - Can't pipe output from tail -f to grep then grep again lostjohnny Linux - Newbie 15 03-12-2009 11:31 PM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 12:38 PM


All times are GMT -5. The time now is 08:41 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration