How to erase files with line number less than 100


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to erase files with line number less than 100
# 1  
Old 05-12-2008
How to erase files with line number less than 100

I am trying to delete all the files with line number less than 100 in a directory. Thank you.
# 2  
Old 05-12-2008
Well, first, the obligatory question: is this a homework question? If so, we can't help you, have a read of the:

https://www.unix.com/unix-dummies-que...om-forums.html

Assuming that it's not, what is the real world problem of your question?

Regards
# 3  
Old 05-12-2008
Re

Thank you for your reply. This is not a homework question. I need to send a lot of files to an R program to calculate some features for each file. But since R is so slow and files with line numbers less than 100 is meaningless anyway, I decided to erase those unqualifed and send the rest to the R program. Thank you very much.
# 4  
Old 05-12-2008
One way -
Code:
#!/bin/ksh
find /path/to/files -type f |\
while read file
do
     wc -l | read linecount junk
     if [[ $linecount -lt 100 ] ; then
           rm -f $file
     fi
done

Make sure you have a good backup first.
# 5  
Old 05-12-2008
To develop this piecemeal, here's a simple way to print the line count of each file:

Code:
wc -l *

To print only those files which have fewer than 100 lines, use grep or awk to remove those with a line count over 100:

Code:
wc -l * | awk '$1 < 100'

(The default action of awk is to print anything matching the condition. We are using awk instead of grep because of developments below.)

To actually delete those files instead of just print them, extend the awk script to only print the file name (presuming there will be no file names with whitespace in them):

Code:
wc -l * | awk '$1 < 100 { print $2 }' | xargs rm

You might want to try the intermediate stages before deploying this, just to verify that it works for you.

You could use awk or Perl all the way if you have a lot of large files; you'd obviously only really need to read the first 100 lines to see if a file is large enough.
# 6  
Old 05-12-2008
Thanks so much.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare two files, then outputs line number

I have two files, "ranked.txt" and "sorted.txt". Sorted.txt is a smaller subset from ranked.txt that is sorted in alpha order. However ranked.txt preserves the ranking of words I would like to keep. How do I check the rank of every word in sorted.txt when matched to the original ranked.txt? I... (8 Replies)
Discussion started by: pxalpine
8 Replies

2. Shell Programming and Scripting

How to erase junk Chars coming in only the first line

I have a file containing few thousands of lines. when I do cat on it , i find it having two special Chars at the start of first line alone as shown down here. ÿþHDR|20111024|01 If i delete this line and do a cat on file , the current first line is shown to have the same special Chars. ... (3 Replies)
Discussion started by: subramanian2008
3 Replies

3. UNIX for Dummies Questions & Answers

Erase line of text file one by one

Hi, I have a BASH script where I would like to identify all the lines of a text file that match specific pattern, and then erase them one by one. The ultimate goal will be to identify all the lines matching the pattern, and then for each line identified prompt the user whether or not to erase... (3 Replies)
Discussion started by: msb65
3 Replies

4. Shell Programming and Scripting

How To Erase a line is a row is empty?

Hi! i've been reading you guys for some time, now there is something I couldn't find here, I'm trying to purge some data for my thesis but my measurements have some gaps in the third columns. The solution is simple, -Erase those lines where the third column is empty ¿How? example... (1 Reply)
Discussion started by: AriasFco
1 Replies

5. UNIX for Dummies Questions & Answers

How to read contents of a file from a given line number upto line number again specified by user

Hello Everyone. I am trying to display contains of a file from a specific line to a specific line(let say, from line number 3 to line number 5). For this I got the shell script as shown below: if ; then if ; then tail +$1 $3 | head -n $2 else ... (5 Replies)
Discussion started by: grc
5 Replies

6. Shell Programming and Scripting

add number in lines line by line in different files

I have a set of log files that are in the following format ======= set_1 ======== counter : 315 counter2: 204597 counter3: 290582 ======= set_2 ======== counter : 315 counter2: 204597 counter3: 290582 ======= set_3 ======== counter : 315 counter2: 204597 counter3: 290582 Is... (6 Replies)
Discussion started by: grandguest
6 Replies

7. Shell Programming and Scripting

how to get the data from line number 1 to line number 100 of a file

Hi Everybody, I am trying to write a script that will get some perticuler data from a file and redirect to a file. My Question is, I have a Very huge file,In that file I have my required data is started from 25th line and it will ends in 100th line. I know the line numbers, I need to get all... (9 Replies)
Discussion started by: Anji
9 Replies

8. Shell Programming and Scripting

Adding a columnfrom a specifit line number to a specific line number

Hi, I have a huge file & I want to add a specific text in column. But I want to add this text from a specific line number to a specific line number & another text in to another range of line numbers. To be more specific: lets say my file has 1000 lines & 4 Columns. I want to add text "Hello"... (2 Replies)
Discussion started by: Ezy
2 Replies

9. UNIX for Advanced & Expert Users

how to erase files from a tape used previously on Windows operating system

hi all Please may you help. I want to put my unix application backup files using tar cv8 * from a specific folder e.g /u1/sage With new tapes SONY 4mm-DL 90m i can do it with no problem at all. Now I have run out of tapes and I need to use the same kind but they were once used to back... (5 Replies)
Discussion started by: isaac
5 Replies

10. Shell Programming and Scripting

Erase files and directory

I have a set of files created under different name, but they all have the same directory name at some point. I will like to delete all the files and directories after the common name: Here is an example: techs\fins\results\oaks\bigs tech2\gihs\results\gears\picks ... (3 Replies)
Discussion started by: odogbolu98
3 Replies
Login or Register to Ask a Question