How to delete files with no records?

 
Thread Tools Search this Thread
Special Forums UNIX and Linux Applications How to delete files with no records?
# 1  
Old 01-26-2010
How to delete files with no records?

Hi,

I have a file whose size is not zero but it has no records and another which has records. I want to delete all the files that have no records in it (even if size > 0). How do I do it?
I have tried the below option

Code:
#!/bin/ksh
 temp1 = $(wc -l < INVX102C.sf)
 if [ ${temp1} -gt 0]; then
 echo "Data file"
 else
 echo "Empty file"
 fi
 EOJ

But this gives me error as "test: ] missing"
Please advise how can I get my result.

Last edited by Scott; 01-26-2010 at 03:03 PM.. Reason: Please use code tags
# 2  
Old 01-26-2010
1.You have space between temp1 variable and = sign.
2. You need define temp1 as an integer: typeset -i temp1=0
3. you need space between 0 and ]
Code below:
Code:
#!/bin/ksh
typeset -i temp1=0
temp1=$(wc -l < digit.ksh)
if [ ${temp1} -gt 0 ]; then
       echo "Data file"
else
       echo "Empty file"
fi



---------- Post updated at 01:11 PM ---------- Previous update was at 01:01 PM ----------

or just simply check if file great than zero:
Code:
if [ -s INVX102C.sf ]; then
     echo "Data file"
else 
     echo "Empty fle"
fi


Last edited by Scott; 01-26-2010 at 03:20 PM.. Reason: Please use code tags
# 3  
Old 01-26-2010
ok. It worked.. I tried the following commad:
Code:
if [ $(wc -w < INVX102C.sf) -eq 0 ]; then
   echo "INVX102C empty"
   rm INVX102C.sf
else
   echo "File not empty"
fi

It gave me File empty for the one with no records.

Thanks!!!

---------- Post updated at 02:15 PM ---------- Previous update was at 02:14 PM ----------

-s does not work here because the file has a size greater than 0 even when it is empty.
The other one worked.
Thanks!!!

Last edited by Scott; 01-26-2010 at 03:20 PM.. Reason: Code tags, PLEASE!
# 4  
Old 01-26-2010
you are welcome
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Delete records that do not belong to that day

i have a requirement to delete records that do not belong to that day. For example in a file that came on July 31st ,2018 there are records that belong to Aug 1st,2018 as well and I want to find and delete those records. I want to delete anything with 01-Aug-2018. I have several files like that. I... (6 Replies)
Discussion started by: Priya
6 Replies

2. Shell Programming and Scripting

Delete db records from shell script

Hello Mates, I would request your help in a shell script, simply I need to delete some matching db table records (rows) to ones in a given file: ------------------------------ #!/bin/bash SQL="delete from numberlist where msidn='';" MYSQL_USER="<your-user>"... (4 Replies)
Discussion started by: EAGL€
4 Replies

3. Shell Programming and Scripting

Delete records by string and position

How do i delete the records when we have string "abcd" at position 21 to 24. The file is huge in size. Any help is appreciated. Sample data given below eiojfcdf oijjfdm ijdabcd234vffmv eojfirr jfdiopem ijaabb456irkmd aodniiuielc uejdocvoabcd957wpdjf (4 Replies)
Discussion started by: zooby
4 Replies

4. Shell Programming and Scripting

Delete the records from table

Hi, Can any one help me... the records are not deleting when I run the below script. But if I issue the same delete command manually, the records are getting deleted. script: #!/bin/ksh USAGE_STRING="USAGE $0 " if then echo "SORRY you need to be user 'mqm'. Only 'mqm' has... (5 Replies)
Discussion started by: zxcjggu708
5 Replies

5. Shell Programming and Scripting

Delete records within a file upon a condition

Hi Friends, I have the following file, cat input chr1 1000 2000 chr1 600 699 chr1 701 1000 chr1 600 1710 chr2 900 1800 Now, I would like to see the difference of Record1.Col2 - Record2.Col2 Record1.Col2 - Record2.Col3 Record1.Col3 - Record2.Col2 Record1.Col3 - Record2.Col3 ... (1 Reply)
Discussion started by: jacobs.smith
1 Replies

6. Shell Programming and Scripting

Compare two files with different number of records and output only the Extra records from file1

Hi Freinds , I have 2 files . File 1 |nag|HYd|1|Che |esw|Gun|2|hyd |pra|bhe|3|hyd |omu|hei|4|bnsj |uer|oeri|5|uery File 2 |nag|HYd|1|Che |esw|Gun|2|hyd |uer|oi|3|uery output : (9 Replies)
Discussion started by: i150371485
9 Replies

7. Shell Programming and Scripting

how to delete records with the given line numbers

I have a file which has about 10000 records and I need to delete about 50 records from the file. I know line numbers and am using sed '134,1357,......d' filename > new file. It does not seem to be working. Please Advice (5 Replies)
Discussion started by: mad_man12
5 Replies

8. UNIX for Advanced & Expert Users

delete records using line number(NR)

Hai I have a flat file which contains more than 6 crore lines or records. I want to delete only one line, using line number. For example I want to delete 414556 th line . How to do this using sed or awk command. thanks (3 Replies)
Discussion started by: tkbharani
3 Replies

9. Shell Programming and Scripting

delete records from a file

I have a big file with "|" delimiter. I want to delete all the records that have 'abc' in the 2nd field. How can i do that? I am not abe to open it in VI that is why i need to do it from outside. Please suggest (6 Replies)
Discussion started by: dsravan
6 Replies

10. Shell Programming and Scripting

Hai delete the records

Hai all i want to grep the particular pattern and move the mathing records to torget file and delete the matching recodrs from source file. patterns are position based, like 13413432,aaaaaaa,dsfdddddd,34234324,22224555 13413432,aaaaaaa,dsfdddddd,12234324,11222455 i want to move the... (1 Reply)
Discussion started by: readycpbala
1 Replies
Login or Register to Ask a Question