all files-search and delete


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting all files-search and delete
# 1  
Old 05-02-2012
all files-search and delete

I have n number of files like
Code:
custpoten_123.txt
custpoten_456.txt
custpoten_789.txt
custpoten_765.txt

from above all files i want to serach for a particular number and it should delete the whole line.i have a code for search and delete.
code is
Code:
#!/usr/bin/sh
srcfile=/ushhquest/data001/SrcFiles/viv.txt
tgtfile=/ushhquest/data001/SrcFiles/output.txt
awk -F, '$6 == 5 {next}1' $srcfile > $tgtfile
rm -f $srcfile
mv $tgtfile $srcfile
echo "completed"

now above code can search only for one file,i want to search and delete for all above files starting with name custpoten_*.txt in the particular folder.

Can anyone plz tell the code for search and delete all the files in a folder.

Last edited by Franklin52; 05-02-2012 at 09:38 AM.. Reason: Please use code tags
# 2  
Old 05-02-2012
This was not checked in command line, so try it first, before you use it.

Code:
#!/usr/bin/sh
MY_PATH="/ushhquest/data001/SrcFiles/"
MY_TMP=`mktmp`

for srcfile in ${MY_PATH}/*.txt ; do 
awk -F, '$6 == 5 {next}1' ${srcfile} > $TMP_FILE
cat $TMP_FILE > $srcfile
echo -n "."
done

rm -f ${TMP_FILE}
echo -e "***\tDONE\t***"

# 3  
Old 05-02-2012
Bug Just add one while loop.

Code:
ls  custpoten_*.txt  > sample_txt_files

while read line
do
  srcfile="/ushhquest/data001/SrcFiles/"$line
  tgtfile=/ushhquest/data001/SrcFiles/output.txt
  awk -F, '$6 == 5 {next}1' $srcfile >> $tgtfile
  rm -f $srcfile
  mv $tgtfile $srcfile
done<sample_txt_files
echo "completed"


Last edited by Franklin52; 05-02-2012 at 10:36 AM.. Reason: Please use code tags
This User Gave Thanks to pamu For This Post:
# 4  
Old 05-02-2012
why we are using my_tmp in the script,moreover folder may contain the files which is not starting with custpoten.

---------- Post updated at 08:09 AM ---------- Previous update was at 08:06 AM ----------

Quote:
Originally Posted by pamu
ls custpoten_*.txt > sample_txt_files

while read line
do
srcfile="/ushhquest/data001/SrcFiles/"$line
tgtfile=/ushhquest/data001/SrcFiles/output.txt
awk -F, '$6 == 5 {next}1' $srcfile >> $tgtfile
rm -f $srcfile
mv $tgtfile $srcfile
done<sample_txt_files
echo "completed"

wat is $line and why we we are using it..
# 5  
Old 05-02-2012
ls custpoten_*.txt > sample_txt_files ## this will create one list of your required .txt files.

and $line indicates .txt file name.

In while loop we operate on one file at one time.

Let me know if u still have more doubt ?
# 6  
Old 05-02-2012
Quote:
Originally Posted by pamu
ls custpoten_*.txt > sample_txt_files ## this will create one list of your required .txt files.
This is a useless use of ls *, where the ls technically serves no function at all -- it finds no files, it's only used to repeat input the shell has already found ( *, etc, are processed in the shell, not in ls ).

You can use the shell builtin printf instead:
Code:
printf "%s\n" custpoten_*.txt > sample_txt_files

# 7  
Old 05-02-2012
its working thanks..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script needed to delete to the list of files in a directory based on last created & delete them

Hi My directory structure is as below. dir1, dir2, dir3 I have the list of files to be deleted in the below path as below. /staging/retain_for_2years/Cleanup/log $ ls -lrt total 0 drwxr-xr-x 2 nobody nobody 256 Mar 01 16:15 01-MAR-2015_SPDBS2 drwxr-xr-x 2 root ... (2 Replies)
Discussion started by: prasadn
2 Replies

2. Linux

How can I search and delete mail?

Hi, I'd like to delete emails on one of my Linux boxes using a criteria and I don't really know where to start. Any suggestions? The emails I'd like to delete have all the following sub-string: checkdefunct.sh.sh I have more than 187.000 emails and 90% of them I'd like to get rid of. ... (1 Reply)
Discussion started by: fabiogilr
1 Replies

3. Shell Programming and Scripting

Find command to search and delete files older than 1 days at a desired location

Hello All, Can someone please help me out in creating the find command to search and delete files older than 1 days at a desired location. Thanks in advance for your help. (3 Replies)
Discussion started by: Pandee
3 Replies

4. Shell Programming and Scripting

Search and delete

A file(example viv.txt) contains a n number of rows like 1,2,3,4,5,6,7,8 11,22,33,44,55,66,77,88 1,3,9,7,8,5,2,6,4 Requirement: If number "5" exist in the sixth position then it should delete the whole line.like this it should check for all rows in a file(viv.txt) . please help on this. (5 Replies)
Discussion started by: katakamvivek
5 Replies

5. SuSE

Search all files based on first and in all listed files search the second patterns

Hello Linux Masters, I am not a linux expert therefore i need help from linux gurus. Well i have a requirement where i need to search all files based on first patterns and after seraching all files then serach second pattern in all files which i have extracted based on first pattern.... (1 Reply)
Discussion started by: Black-Linux
1 Replies

6. Shell Programming and Scripting

Search and delete

Gurus I have a CSV containing 60K records.Each row has 8 columns. On some rows ,for the 7th column ,i find word 'UnknownState(898914497)' repeated many times. e.g <N_HOST> <tcp> <*> <*> <*> <*> ... (1 Reply)
Discussion started by: ak835
1 Replies

7. Shell Programming and Scripting

Search directories for files with zero sizes to delete

Hi, I am trying to write a script that will use ls on a directory and list the files one at a time and their size. If the size is 0 i want it to ask me if I want to delete it (yes or no). If I say yes, I want it to delete but it won't know what the file name is just from running from the script.... (2 Replies)
Discussion started by: akeenabawa
2 Replies

8. UNIX for Advanced & Expert Users

Search files with specfic extention and later search content

Hi, I would appriciate if somebody can help me figure out how to search for all the *.xml file under a specific directory and subdirectroies (/home/username) and later search of content "<start>" inside the xml file returned by search. -Lovin.V (2 Replies)
Discussion started by: lovi_v
2 Replies

9. Shell Programming and Scripting

Search text and delete

After searching for a specified string, I would like to delete couple of rows from the file and continue searching. Basically, I would like to search through a text file that holds logs with date-time stamp in them and then clean up the file if the log entry is more than 2 days old. Example log... (0 Replies)
Discussion started by: new2shell
0 Replies

10. Shell Programming and Scripting

search and delete a line

Folks, I have a set of list that contains file names. I want to search through each of the list and delete any file that is found in the list. eg. LIST_A contains: aaa bbb ccc ddd eeee LIST_B contains: aab aac adb aed assuming I want to... (6 Replies)
Discussion started by: odogbolu98
6 Replies
Login or Register to Ask a Question