Finding out if there is text in a file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding out if there is text in a file.
# 1  
Old 09-12-2004
Finding out if there is text in a file.

I have a script that when run creates 4 other text files. Sometimes a couple of these text files are empty. So what I am trying to do is find out if there is text in the file and if not delete the file.

What I have so far is the following script. I am just not sure how to return a true value in the grep. In Applescript it is easy but this shell scripting I am just starting out with.

#!/bin/sh

#testgrep-used to find a pattern in text file and if does not #exist then deletes file.


if grep "search_pattern" file_to_search
then rm file_to_search
exit 0
fi

Any help will be much appreciated and I am glad that I found these forums.

Thank You...
# 2  
Old 09-12-2004
What is moving you from the applescript world into shell programming?

A very simple way to accomplish what you are describing is:

find /path/to/files -empty -exec rm -f {}\;

Cheers,

Keith
# 3  
Old 09-12-2004
Quote:
I am just not sure how to return a true value in the grep.
With Just a little modification

grep "search_pattern" file_to_search
if [ $? -eq 0 ]
then rm file_to_search
exit 0
fi
Smilie
# 4  
Old 09-12-2004
Thanks for the repies so quickly, but a couple of things happen.

When I execute this

find /path/to/files -empty -exec rm -f {}\;

and I put in the /path/to/file I get an error of -exec: no terminating ";" although there is one there.

Also when I put this in a script all by itself and fill in the appropriate areas nothing happens, it just returns to the next line

grep "search_pattern" file_to_search
if [ $? -eq 0 ]
then rm file_to_search
exit 0
fi

Could you please explain the second line.


Quote:
What is moving you from the applescript world into shell programming?
Experimentation and sometimes it takes the Finder forever to do some things. In applescript you have to open applications most of the time to get the work done but with shell scripting and a text delimited file it is amazing how fast it is to use awk and/or sed (I still have not figured all of those out yet) to get the same job done. But I still use applescript most of the time because I have not figured out how to do my work in a shell script, such as taking 10000-11000 pictures and making a copy, cropping the copy to a certain size, making a thumbnail, renaming the thumbnail, apply a watermark to the cropped image, and putting each file into a separate directory, etc. all in a matter of about 8 minutes.
# 5  
Old 09-12-2004
Proper spacing is important in the command I showed you.

As for your next question, most commands have a return value associated with them. 'grep' is going to return the number of times a pattern is matched, thus is 0 it found no matches. Are you wanting to remove empty files or files that don't have a pattern you want in them?

Cheers,

Keith
# 6  
Old 09-12-2004
If the file does not contain the search pattern I would like to delete the file.
# 7  
Old 09-12-2004
Quote:
Could you please explain the second line

grep "search_pattern" file_to_search
if [ $? -eq 0 ]
then rm file_to_search
exit 0
fi
The second line checks if the command on the first line
executed properly(--grep found the pattern in the file)
and if an exit value of 0 is returned(i.e. command executed properly) it then removes the file <file_to_search>

Cheers!!

Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Finding pattern in a text file and returning a part of the word

Dear All, assume that we have a text file or a folder of files, I want to find this pattern followers*.csv in the text file , and get * as the output. There are different matches and * means every character. Thank you in advance. Best, David (1 Reply)
Discussion started by: davidfreed
1 Replies

2. Shell Programming and Scripting

Finding a text file from a group of zip files without unzipping

HI , There are more than 100 zip files in a directory and i wanted to see if there is a max1157.txt file in any of the zip files without actually unzipping them. Could you please help. Thanks in Advance. Karthik. (6 Replies)
Discussion started by: karthikk0508
6 Replies

3. Shell Programming and Scripting

Finding the second last column value from a text file

Can any one tell me how to get the second last column value from the text file, which has different record size for each record. I know how to get the last column using awk and print statements, but I am unable to get the second last column value from the file. (4 Replies)
Discussion started by: naveen_sangam
4 Replies

4. Shell Programming and Scripting

Finding the last column value from a text file

Hi, I need to find out the last column value from a text file which is delimited by a tab. The issue here is the last column# for each record can be different i.,e, 1st record can have the last column as 15 and the second record can have the last column as "17". I have to search a string... (3 Replies)
Discussion started by: naveen_sangam
3 Replies

5. Shell Programming and Scripting

Finding a string in a text file and posting part of the line

What would be the most succinct way of doing this (preferably in 1 line, maybe 2): searching the first 10 characters of every line in a text file for a specific string, and if it was found, print out characters 11-20 of the line on which the string was found. In this case, it's known that there... (13 Replies)
Discussion started by: busdude
13 Replies

6. Shell Programming and Scripting

Help with finding text in file

Hello, Please help me with this. I have two files. file1.txt and file2.txt File1 contains text as below txt1 txt2 txt3 txt4 txt5 txt6 txt7 txt8 File2 contains text as below $1 $2 $3 $4 $5 $6 $7 $8 $9 ----------------------------- txt1 txt2 - - - 0 2 8 -*- txt0 txt7 - - - 1 4 8... (3 Replies)
Discussion started by: tenderfoot
3 Replies

7. Shell Programming and Scripting

add newline in file after finding specific text

Hi All, I am tring to insert a newline with "/" in a text file whenever there is the text "end;" right now I have inside file: . . end; I want to have: . . end; / I tried doing the following within the file :g/^end;/s//end; \/ / (4 Replies)
Discussion started by: jxh461
4 Replies

8. UNIX for Dummies Questions & Answers

finding text in a file

How do i find text string in a file if i have no idea where the file is? What I am trying to do is find an email address in a file and have no idea where the file is. Thanks (5 Replies)
Discussion started by: §ynic
5 Replies

9. UNIX for Dummies Questions & Answers

finding text inside file

Hi everyone I have a small problem i cant find a soloution to... I'm using digital unix and have to find out all the files which have a certain string inside them and i dont know how to do it. *This search is done as root. *All files from '/' to the last directory should be searched for... (3 Replies)
Discussion started by: dindan100
3 Replies

10. Filesystems, Disks and Memory

finding out the pid for a busy text file

Can some one please tell me how to find out the proccess ID that is holding up a file. I am attempting to remove a file and I am getting a message stating that it is busy. i.e rm filename filename: 777 mode ? (y/n) y rm: filename not removed. Text file busy Thanks in advance. (1 Reply)
Discussion started by: jxh461
1 Replies
Login or Register to Ask a Question