LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-12-2008, 07:59 AM   #1
raghu123
Member
 
Registered: May 2008
Posts: 34

Rep: Reputation: 15
delete all files that are "5 lined" ---urgent---


Dear all,

i want to delete all files in my directory that have 5 lines ......
(means all files that are 5 lined, but not empty files)

i used for loop inside wic i gav wc -l and then if it is 5 then remove it......but some silly syntax error.....
i knw that we can use sed for this.....

Kindly suggest and give the proper code......
 
Old 08-12-2008, 08:15 AM   #2
burschik
Member
 
Registered: Jul 2008
Posts: 159

Rep: Reputation: 31
Code:
wc -l * | sed -n 's/ \+5 //p' | xargs rm
, for example
 
Old 08-12-2008, 11:07 PM   #3
raghu123
Member
 
Registered: May 2008
Posts: 34

Original Poster
Rep: Reputation: 15
regards!!!!

thanks for the command......cud u plz suggest a command to find all files in the directory having 5 lines .....


Quote:
Originally Posted by burschik View Post
Code:
wc -l * | sed -n 's/ \+5 //p' | xargs rm
, for example
 
Old 08-12-2008, 11:22 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
My guess is that you want a loop with the general form:

for filename in `ls`; do <put the test here>; done ##the test will use $filename

If the directory has directories in it, then you would also need a test statement to ignore them.
 
Old 08-13-2008, 12:29 AM   #5
kenoshi
Member
 
Registered: Sep 2007
Location: SF Bay Area, CA
Distribution: CentOS, SLES 10+, RHEL 3+, Debian Sarge
Posts: 159

Rep: Reputation: 32
burschik already did.

Just remove the | xargs rm in his example.
 
Old 08-13-2008, 12:41 AM   #6
raghu123
Member
 
Registered: May 2008
Posts: 34

Original Poster
Rep: Reputation: 15
Bug ........

i am using that sed command wc -l * | sed -n 's/ \+5 //p'
but my entries are around 3000.....so its giving arguement list too long......
i als tried givin it in loop......

so kindly suggest
 
Old 08-13-2008, 12:57 AM   #7
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
find . -type f | xargs wc -l | sed -n 's/ \+5 //p'

My wc does not output a + char in front of values, so I use:

find . -type f | xargs wc -l | sed -n 's/ 5 //p'
 
Old 08-13-2008, 01:05 AM   #8
raghu123
Member
 
Registered: May 2008
Posts: 34

Original Poster
Rep: Reputation: 15
its working fine!!!

Thanq......its working good

Quote:
Originally Posted by Mr. C. View Post
find . -type f | xargs wc -l | sed -n 's/ \+5 //p'

My wc does not output a + char in front of values, so I use:

find . -type f | xargs wc -l | sed -n 's/ 5 //p'
 
Old 08-13-2008, 01:09 AM   #9
kenoshi
Member
 
Registered: Sep 2007
Location: SF Bay Area, CA
Distribution: CentOS, SLES 10+, RHEL 3+, Debian Sarge
Posts: 159

Rep: Reputation: 32
I thought he used \+5 because of BRE to get rid of multiple spaces? To use s/ +5 //p he would need to use -r flag correct?
 
Old 08-13-2008, 01:13 AM   #10
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Oh, right! I've forgotten about that silly notation. My sed's old, I'm afraid, and doesn't support that. Thanks for the reminder.

In extended REs, I can do:

$ echo 'a b' | sed -E 's/ +//'
ab

but not either of:
$ echo 'a b' | sed 's/ \+//'
a b
$ echo 'a b' | sed 's/ +//'
a b

Last edited by Mr. C.; 08-13-2008 at 01:18 AM.
 
Old 08-13-2008, 01:27 AM   #11
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Ok, I've let myself get a bit confused. I thought I recall \+, but I must be mistaken. BREs have no support for the + char (1 or more chars). Extended REs provide + support, and no \ is required.

The 5 char is simple the output of wc, btw., not part of any metacharacter or quantifier. The sample code in post #2 does not work for me on BSD or Linux.
 
Old 08-13-2008, 02:04 AM   #12
raghu123
Member
 
Registered: May 2008
Posts: 34

Original Poster
Rep: Reputation: 15
everthinng s fine, but i am gettin bugs due to the number of files I have.....

Dear burschik, Mr.C, kenoshi....
i acknowledge your help, but for finding the 5lined files and then removing them, i hav around 3000 files to check and its priting a bug like arguement list too long......so i am using a loop to gind and remove....
plz suggest the proper code for this....
find . -type f | xargs wc -l | sed -n 's/ 5 //p' is working fine...but to remove the same, how??

in loop??
 
Old 08-13-2008, 05:58 AM   #13
raghu123
Member
 
Registered: May 2008
Posts: 34

Original Poster
Rep: Reputation: 15
got it!!!

find . -type f | xargs wc -l | sed -n 's/ 5 //p' | xargs rm -f

is the proper code!!!
 
Old 08-13-2008, 07:36 AM   #14
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Most excellent!
 
Old 08-13-2008, 12:49 PM   #15
kenoshi
Member
 
Registered: Sep 2007
Location: SF Bay Area, CA
Distribution: CentOS, SLES 10+, RHEL 3+, Debian Sarge
Posts: 159

Rep: Reputation: 32
Correct me if I'm wrong...I thought with BREs meta characters such as . * ^ $ doesn't require an escape, while all others do...and EREs you don't need to escape any of the metas.

Anyway, grats raghu123, nothing like figuring things out yourself
 
  


Reply



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
LXer: Using "shred" to Securely Delete Files LXer Syndicated Linux News 0 02-27-2008 06:50 PM
How to recover "Shift Delete" deleted files? gimmee Fedora 1 07-05-2007 05:07 AM
Cannot delete files from hard drive "read only" help! cooknet Linux - Newbie 16 06-06-2007 06:49 PM
delete all files starting with "new" adnanm Linux - Newbie 2 01-18-2007 06:01 AM
Delete a bunch of files with "find" chess Slackware 3 01-28-2006 12:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:27 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
Open Source Consulting | Domain Registration