Sponsored Content
Top Forums Shell Programming and Scripting Delete files based on specific MMDDYYYY pattern in filename Post 302545506 by shankar1dada on Monday 8th of August 2011 03:23:12 PM
Old 08-08-2011
Delete files based on specific MMDDYYYY pattern in filename

Hi Unix gurus,

I am trying to remove the filenames based on MMDDYYYY in the physical name as such so that the directory always has the recent 3 files based on MMDDYYYY. "HHMM" is just dummy in this case. You wont have two files with different HHMM on the same day.


For example in a directory, I have files like
Code:
OPEN_INV_01012011_1345.xls
OPEN_INV_01022011_1230.xls
OPEN_INV_01032011_1145.xls
OPEN_INV_01042011_2456.xls
OPEN_INV_01012011_3456.txt
OPEN_INV_01022011_1134.txt
OPEN_INV_01032011_0812.txt
OPEN_INV_01042011_3467.txt

When I run the script it should delete the OPEN_INV_01012011_1345.xls and OPEN_INV_01012011_3456.txt

Note that the before the file extension, we always have "MMDDYYYY_HHMM"

I am using the following script:
This is what I am trying to do;

Code:
#!/usr/bin/ksh

archivedir=/opt/data/files/archive

typeset -i MAX_ARCHIVE_COUNT
typeset -i archive_file_count
typeset -i remove_archive_count

cd $archivedir
for archpref in $(ls | sed 's/[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\.[^.]*$//' | sort | uniq)
do
  archive_file_count=$(ls -1t ${archpref}* | wc -l)
  MAX_ARCHIVE_COUNT=3
  remove_archive_count=${archive_file_count}-${MAX_ARCHIVE_COUNT}
  if [ ${remove_archive_count} -gt 0 ]
  then
# List the files in date order (most recent first), suppress the first 3, and delete the rest
    rm $(ls -1rt | tail -${remove_archive_count})
  fi
done



Any help is greatly appreciated.

Thanks

Last edited by Scott; 08-09-2011 at 01:04 PM.. Reason: Added code tags
 

10 More Discussions You Might Find Interesting

1. HP-UX

How to delete specific pattern in a file with SED?

I have one file which is having content as following... 0513468211,,,,20091208,084005,5,,2,3699310, 0206554475,,,,20090327,123634,85,,2,15615533 0206554475,,,,20090327,134431,554,,2,7246177 0103000300,,,,20090523,115501,89,,2,3869929 0736454328,,,,20091208,084005,75,,2,3699546... (7 Replies)
Discussion started by: ganesh.mandlik
7 Replies

2. Shell Programming and Scripting

Trimming sequences based on specific pattern

My files look like this And I need to cut the sequences at the last "A" found in the following 'pattern' -highlighted for easier identification, the pattern is the actual file is not highlighted. The expected result should look like this Thus, all the sequences would end with AGCCCTA... (2 Replies)
Discussion started by: Xterra
2 Replies

3. Shell Programming and Scripting

how to delete lines from a file which starts with a specific pattern

I need to delete those lines from a file, which starts with 45. How to do it? (3 Replies)
Discussion started by: mady135
3 Replies

4. Shell Programming and Scripting

Delete multiple lines starting with a specific pattern

Hi, just tried some script, awk, sed for the last 2 hours and now need help. Let's say I have a huge file of 800,000 lines like this : It's a tedious job to look through it, I'd like to remove those useless lines in it as there's a few thousands : Or to be even more precise : if line1 =... (6 Replies)
Discussion started by: Zurd
6 Replies

5. Shell Programming and Scripting

Delete line based on count of specific character

I'm looking for what I hope might be a one liner along these lines: sed '/a line with more than 3 pipes in it/d' I know how to get the pipe count in a string and store it in a variable, but I'm greedy enough to hope that it's possible via regex in the /.../d context. Am I asking too much? ... (5 Replies)
Discussion started by: tiggyboo
5 Replies

6. Shell Programming and Scripting

Search for duplicates and delete but remain the first one based on a specific pattern

Hi all, I have been trying to delete duplicates based on a certain pattern but failed to make it works. There are more than 1 pattern which are duplicated but i just want to remove 1 pattern only and remain the rest. I cannot use awk '!x++' inputfile.txt or sed '/pattern/d' or use uniq and sort... (7 Replies)
Discussion started by: redse171
7 Replies

7. Shell Programming and Scripting

Delete specific lines from files based on another file

I have some text files in a folder named ff as follows. I need to delete the lines (in-place editing)in these files based on another file aa.txt. 32bm.txt: 249 253 A P - 0 0 8 0, 0.0 6,-1.4 0, 0.0 2,-0.4 -0.287 25.6-102.0 -74.4 161.1 37.1 13.3 10.9 250... (2 Replies)
Discussion started by: aden
2 Replies

8. UNIX for Dummies Questions & Answers

How to find a file based on pattern & return the filename if found?

Hi all, I am a newbie here. I have this requirement to find a file based on a pattern then return the filename if found. I created a script based on online tutorials. Though, I am stuck & really appreciate if anyone can have a quick look & point me to the right direction? #Script starts... (10 Replies)
Discussion started by: buster_t
10 Replies

9. Shell Programming and Scripting

Delete lines that contain a pattern from specific line to the end.

Gents, I am trying to delete all lines which start with "H" character, but keeping the fist header. Example In the input file I will delete all lines starting from line 8 which contents character "H" to the end of the file. I try sed '8,10000{/^H/d;}' file But as don't know the end... (1 Reply)
Discussion started by: jiam912
1 Replies

10. Shell Programming and Scripting

Delete lines based on pattern match

BASH in Solaris 10 I have a log file like below. Whenever the pattern ORA-39083 is encountered, I want to delete the line which has this pattern and 3 lines below it. $ cat someLogfile.txt ORA-39083: Object type OBJECT_GRANT failed to create with error: ORA-01917: user or role 'CMPA' does... (4 Replies)
Discussion started by: kraljic
4 Replies
HEXBIN(L)																 HEXBIN(L)

NAME
hexbin - Macintosh file de-binhexer SYNOPSIS
hexbin [ - options ] [ files ] DESCRIPTION
hexbin takes the text files specified in files (or standard input if none is specified) and converts them subject to the options specified. OPTIONS
In the absence of any options, hexbin takes the specified files and silently converts them into MacBinary format, giving the output files ".bin" extensions and placing them in the current working directory. -3 Write files in fork format (.info, .data and .rsrc files.) -f As -3, but empty data and rsrc files are not created. -r Write resource forks only (.rsrc files.) -d Write data forks only (.data files.) -u As -d, but the codes for CR and LF are interchanged, the filename extension is .text. -U As -u, but there is no filename extension. -a Write files in AppleShare format. This option is only valid if the program is compiled with support for some form of AppleShare. The current directory must be a valid AppleShare folder. -s Write extracted files to standard output in MacBinary format. -l List every file extracted (and every directory/folder created etc.) -v Like -l, but more verbose. When this option is specified all lines skipped because they do not belong to the hexified format are listed (implies -l.) -i Do not convert, give information only (implies -l.) -c Do not check whether the hexified lines have equal size. Normally the hexifiers gives text files with equal length line size, hexbin uses this in its heuristics to determine whether a line must be skipped. There are however hexified files that do not con- form to that pattern. If this option is specified hexbin will in general be unable to detect whether a line is garbage or not, so you have to remove the garbage by hand. -n name Gives the Unix base file name for the converted files. For files hexified with BinHex 4.0 or compatible hexifiers this flag is not needed; hexbin will determine the Unix file name based on the Mac file name. For files in dl, hex or hcx format this parameter may be needed as these formats do not include the Mac filename. Normally hexbin will in those cases base the Unix file name on the text file name, but that can be overruled with this parameter. -V Gives the patchlevel of the program, and other information. Other options are ignored and the program quits immediately. -H Give short information about the options. Other options are ignored and the program quits immediately. BUGS
As this is a beta release, there may still be some problems. SEE ALSO
macutil(1) AUTHOR
Dik T. Winter, CWI, Amsterdam, The Netherlands (dik@cwi.nl) Parts of the code are based on codes from: ahm (?), Darin Adler, Jim Budler, Dave Johnson, Dan LaLiberte, Jeff Meyer, Guido van Rossum. 3rd Berkeley Distribution October 22, 1992 HEXBIN(L)
All times are GMT -4. The time now is 04:57 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy