removing files with certain number in the first row


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting removing files with certain number in the first row
# 8  
Old 10-05-2009
What shell are you using? I don't get these errors (with ksh/bash).

bash code:
  1. ls | while read file
  2. do
  3.   [ "$(sed -n "/^0.00/p;q" $file 2>/dev/null)" ] && echo rm -f $file
  4. done
(remove the echo after testing)

Anything which expands the file list as arguments to the program (like grep ... *; rm $(...) is potentially problematic if there are too many files. The -i option of xargs is not POSIX standard.

Last edited by Scott; 10-05-2009 at 06:16 PM..
# 9  
Old 10-05-2009
I tried

set A = (ls | xargs -i grep -l ^0.00 {})
rm -f $A

but it didn't workSmilie

---------- Post updated at 04:27 PM ---------- Previous update was at 04:24 PM ----------

I am using C shell!
# 10  
Old 10-05-2009
Why are you using C Shell?

99.9% of the answers you get here will be bash or ksh based.

Can't you just add

Code:
#!/bin/bash

or

Code:
#!/bin/ksh

to the first line of your script?!

Would make life simpler for everyone!
# 11  
Old 10-05-2009
It helps if you scrape the screen output so I can see the actual error message.

Keep in mind this was written in the korn shell, if you use a different shell you may want to break it down and check the syntax.

I should mention somewhat belatedly that this will delete all files containing any line beginning with 0.00, not just the first. So you will need to modify it if you want to narrow the scope of the files deleted.

With regard to breaking down the code: -

rm -f $(ls | xargs -i grep -l ^0.00 {})

rm -f expects a filename as an argument so we generate the list of file names in the sub shell: -

ls | xargs -i grep -l ^0.00 {}

The ls command is piped into xargs which feeds the file names into grep one at a time in the position marked by {}. The -l option for grep tells it to just output file names; as this is all done in the subshell the names of the files containing the pattern are fed back to the rm statement.

Experiment and try it a chunk at a time till you get it working, then modify it to head each file instead.

Good luck
# 12  
Old 10-05-2009
Scottn, your code perfectly finds the file but doesn't remove it

Thanks steady for your comments
# 13  
Old 10-05-2009
Did you remove the echo? It will echo the rm command only. If you're happy with the result, remove the echo.
# 14  
Old 10-05-2009
This will go through the list of files and delete the appropriate ones..

Code:
ls | awk '{getline line < $0; close($0); if(line ~ /^0\.00/){ print "removing " $0;system("rm " $0)} }'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Select and copy .csv files based on row and column number

Dear UNIX experts, I'm a command line novice working on a Macintosh computer (Bash shell) and have neither found advice that is pertinent to my problem on the internet nor in this forum. I have hundreds of .csv files in a directory. Now I would like to copy the subset of files that contains... (8 Replies)
Discussion started by: rcsapo
8 Replies

2. Shell Programming and Scripting

Removing large number of temp files

Hi All, I am having a situation now to delete a huge number of temp files created during run times approx. 16700+ files. We have never imagined that we will get this this much big list of files during run time. It worked fine for lesser no of files in the list. But when list is huge we are... (7 Replies)
Discussion started by: mad man
7 Replies

3. Shell Programming and Scripting

Removing carriage returns from multiple lines in multiple files of different number of columns

Hello Gurus, I have a multiple pipe separated files which have records going over multiple Lines. End of line separator is \n and records going over multiple lines have <CR> as separator. below is example from one file. 1|ABC DEF|100|10 2|PQ RS T|200|20 3| UVWXYZ|300|30 4| GHIJKL|400|40... (7 Replies)
Discussion started by: dJHa
7 Replies

4. Shell Programming and Scripting

Paste many files with different row number in one file

Hi everybody I am trying to merge many files in one files using paste or pr command but I am not able to resolve this issue. The file are tab delimited like these: 1 4721519 4723118 1 5022468 5024918 1 7093519 7098118 2 19736573 19741172 2 21907973 21910572... (4 Replies)
Discussion started by: giuliangiuseppe
4 Replies

5. Shell Programming and Scripting

Get row number from file1 and print that row of file2

Hi. How can we print those rows of file2 which are mentioned in file1. first character of file1 is a row number.. for eg file1 1:abc 3:ghi 6:pqr file2 a abc b def c ghi d jkl e mno f pqr ... (6 Replies)
Discussion started by: Abhiraj Singh
6 Replies

6. UNIX for Dummies Questions & Answers

Finding row number along with length of row

I have a fixed length file and I want to find out row number along with row length. I have a program that give me the line length if it satisfy the condition; but i would like to add row number as well? How do I do that? while IFS= read -r line; do if ; then echo ${line} echo... (8 Replies)
Discussion started by: princetd001
8 Replies

7. Shell Programming and Scripting

The difference between end number in the early row and the start number in the next

Hi Power User, I'm trying to compute this kind of text file format: file1: jakarta 100 150 jakarta 170 210 beijing 220 250 beijing 260 280 beijing 290 320 new_york 330 350 new_york 370 420 tokyo 430 470 tokyo 480 ... (2 Replies)
Discussion started by: anjas
2 Replies

8. Shell Programming and Scripting

awk command : row by row merging of two files

I want to write a scrpit to merge files row wise (actually concatinating) main.txt X Y Z file 1 A B C file 2 1 2 3 now i want the script to check if the file1 is empty or not, if empty then make it like A B C 1 2 3 again to check if second file is empty if not do as done... (0 Replies)
Discussion started by: shashi792
0 Replies

9. Shell Programming and Scripting

how to add the number of row and count number of rows

Hi experts a have a very large file and I need to add two columns: the first one numbering the incidence of records and the another with the total count The input file: 21 2341 A 21 2341 A 21 2341 A 21 2341 C 21 2341 C 21 2341 C 21 2341 C 21 4567 A 21 4567 A 21 4567 C ... (6 Replies)
Discussion started by: juelillo
6 Replies

10. Shell Programming and Scripting

removing a delimiter at the start of row

I Have this code while do column1=":`cat /home/test_inter.txt|head -${iCount1}|tail -1|cut -d "," -f2`" columnA=$columnA$column1 iCount1=`expr ${iCount1} + 1` done echo $columnA (2 Replies)
Discussion started by: nvuradi
2 Replies
Login or Register to Ask a Question