Filter last 2 characters of a filename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Filter last 2 characters of a filename
# 1  
Old 03-06-2012
Filter last 2 characters of a filename

I have following script to 'archive' some logfiles:
Code:
for APPHOME in `cat $HOME/archive/apps.cfg`
do
        . $APPHOME/archive/parms.cfg

        LOGFILES=$(grep "^LOGFILE=" $APPHOME/archive/parms.cfg)

        for I in $LOGFILES
        do
                LOGPATH=$(echo $I |awk -F'=' '{ print $2 }')
                FILENAME=$(basename $LOGPATH)
                DIR=$(dirname $LOGPATH)
                cp -p $LOGPATH $LOGPATH.$DATE
                cp /dev/null $LOGPATH
                find $DIR -mtime +7 -name "$FILENAME.????????????" -print -exec gzip {} \;
                find $DIR -mtime +21 -name "$FILENAME.????????????.gz" -print -exec mv {} $ARCHIVEDIR \;
        done

done

Problem is that some logfiles already got 'logrotated' (and I don't have access to that mechanism).
I can include the .0 .1 ... files in the logfile list, but they shouldn't be emptied, otherwise I don't have anything else then empty files in the archive
So I need to filter these files to exclude them from the cp -p $LOGPATH $LOGPATH.$DATE
So I need to know how I can test if the last 2 characters have the .0 (.1 .2 ...) extention.

Last edited by Scott; 03-06-2012 at 04:15 AM.. Reason: Please use code tags
# 2  
Old 03-06-2012
Quote:
Originally Posted by oliware
So I need to know how I can test if the last 2 characters have the .0 (.1 .2 ...) extention.
Code:
x="logfile.1"
if [ ".${x##*.}" == ".1" ]
then
    echo "extension is .1"
fi

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 03-06-2012
Quote:
Originally Posted by balajesuri
Code:
x="logfile.1"
if [ ".${x##*.}" == ".1" ]
then
    echo "extension is .1"
fi


If this is used with arbitrary file names, an explicit test against the value 1 is required.

If the value of x is 1, the parameter expansion will return 1 (since there's nothing for it to delete from the value) and the prepending of the dot then makes the test succeed when it should fail.

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

How can I keep certain characters from appearing in a filename?

hi i know this is irrelevant to the question above but i was wondering how to pt a restriction in the filename in linux. I want that it is impossible to add numbers into the filename, help will be rely great , thanx! This posting was initially given as a reply to an unrelated thread. It has been... (2 Replies)
Discussion started by: samirboss
2 Replies

2. UNIX for Advanced & Expert Users

Filter special characters

I have a text file that has these special characters. ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^E How would I go about removing them? They come from a c program. I have to use a for loop like this or my outputs gets messed it. I can't use the %s option or my output won't be in the way I need it.... (2 Replies)
Discussion started by: cokedude
2 Replies

3. Shell Programming and Scripting

Remove the last 9 characters of a filename

Hi All! Please can someone help, I have a dir with the following files: ~-rw-r--r-- 1 emmuser users 2087361 Oct 16 15:50 MPGGSN02_20131007234519_24291.20131007 -rw-r--r-- 1 emmuser users 2086837 Oct 16 15:50 MPGGSN02_20131007233529_24272.20131007 -rw-r--r-- 1 emmuser ... (7 Replies)
Discussion started by: fretagi
7 Replies

4. Shell Programming and Scripting

Filename rename with characters of file

Hi, I need a bit of help. I've used awk to get the first 7 characters of a file - awk '{print substr($0,0,7)}' test.csv How do I now take this variable to rename test.csv to variable.csv ? Any help or advice would be greatly appreciated! (2 Replies)
Discussion started by: sianm
2 Replies

5. Shell Programming and Scripting

removing a range of characters in a filename

hi, I have quite a bunch of files with annoyingly long filenames. I wanted to cut the range of characters from 9-18 and just retain the first 8 characters and the .extension. any suggestion how to do it. thanks much. original filename: 20000105_20000105_20100503.nc.asc output filename:... (4 Replies)
Discussion started by: ida1215
4 Replies

6. UNIX for Dummies Questions & Answers

Help with Removing extra characters in Filename

Hi, It's my first time here... anyways, I have a simple problem with these filenames. This is probably too easy for you guys: ABC_20101.2A.2010_01 ABD_20103.2E.2010_04 ABE_20107.2R.2010_08 Expected Output: ABC_20101 ABD_20103 ABE_20107 The only pattern available are the ff: 1) All... (9 Replies)
Discussion started by: Joule
9 Replies

7. Shell Programming and Scripting

replace and add characters in filename

I have files named like ABAB09s099E1AAV1.pdf and ABAB09s099E2AAV1.pdf in a directory. I need to add _Lop in the end of the name, like ABAB09s099E1AAV1_Lop.pdf for all files. For files with E2 in the name I also have to replace 99 with 88 in the filename, that would be ABAB09s088E2AAV1_Lop.pdf ... (3 Replies)
Discussion started by: hakkar
3 Replies

8. Shell Programming and Scripting

How to filter only comments while reading a file including line break characters.

How do I filter only comments and still keep Line breaks at the end of the line!? This is one of the common tasks we all do,, How can we do this in a right way..!? I try to ignore empty lines and commented lines using following approach. test.sh # \040 --> SPACE character octal... (17 Replies)
Discussion started by: kchinnam
17 Replies

9. Shell Programming and Scripting

remove special characters from filename recursively

hi: i have several thousand files from users and of course they use all kind of characters on filenames. I have things like: My special report (1999 ) Lisa & Jack's work.doc crazy. How do I remove all this characters in the current dir and subdirs too? Thanks. (3 Replies)
Discussion started by: jason7
3 Replies

10. UNIX for Dummies Questions & Answers

Strange Characters in Filename

Hi folks. None of the conventional methods are working for my dilemma: I have a file in my root directory that has a name comprised of strange characters. When I do an ls, it just hangs at that file until I do a Cntrl-C. rm ./filename & rm \filename do not work. I am entering the... (4 Replies)
Discussion started by: kristy
4 Replies
Login or Register to Ask a Question