Sponsored Content
Top Forums Shell Programming and Scripting Remove the last 15 characters of a filename with respect to leave file extension Post 302881321 by Don Cragun on Saturday 28th of December 2013 02:58:18 AM
Old 12-28-2013
The following shell and awk script can search for files matching your pattern in a given directory (by uncommenting the ls command on the 3rd line of the script), in a file hierarchy rooted in one or more given directories (by uncommenting the find command on the 5th line of the script), or by piping a list of pathnames to be processed into the script on standard input. This script will not work if any pathname to be processed contains a <newline> character or a <double-quote> character; other than that it should handle just about regular file you throw at it. This turned out to be more complex than I expected when I first started. (Mostly getting the args right when printing commands to print informational messages.) It uses ls, find, or some other source you provide to get a list of pathnames to be processed, awk to massage the last filename in each pathname and create mv statements to be executed by a shell, and a shell to actually rename the files. (Currently it just prints the mv commands to be executed. When you have convinced yourself that it does what you want it to do, remove the echo marked in red to actually move the files.
Code:
#!/bin/ksh
# Uncomment the following line to process files in the current directory:
# ls *-[[]YT-f22][[]???????????].* |
# Uncomment the following line to process files in and under the current directory:
# find . name '*-[[]YT-f22][[]???????????].*' |
awk '
{       # Save the directory name and remove it from the input line:
        if(match($0, /.*\//)) {
                dir = substr($0, 1, RLENGTH)
                $0 = substr($0, RLENGTH + 1)
        } else  dir = ""
        if(!match($0, /-[[]YT[-]f22][[]...........]/)) {
                printf("printf \"unexpected filename: \\\"%%s\\\" skipped\\n\" \"%s\"\n",
                        dir $0)
                next
        }
        # Save the filename extension and the base filename
        base = substr($0, 1, RSTART - 1)
        ext = substr($0, RSTART + RLENGTH)
        # Split base on strings of non-alphanumeric characters:
        n = split(base, ans, /[^[:alnum:]]*/)
        # Construct the new pathname:
        new = dir ans[1]
        for(i = 2; i <= n; i++) 
                new = new "-" ans[i]
        new = new ext
        printf("echo mv \"%s\" \"%s\"\n", dir $0, new)
}' | ksh

I tested it with bash and ksh. It should also work with an old Bourne shell or any other shell that recognizes basic Bourne shell syntax.

If you want to run this on a Solaris/SunOS system, use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of the default /usr/bin/awk.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to only remove parenthesis and : leave the rest

Hi all, I'm stuck on this last part...am running a simple script under AIX to extract NetView host IP addresses. The line below returns the IP address in parenthesis with a trailing colon, i.e. ping -c 1 $name |grep \( | awk '{ print $3 }' --------> returns (a.b.c.d): How can I only... (10 Replies)
Discussion started by: livinthedream
10 Replies

2. Shell Programming and Scripting

remove file extension

Hi ALL, I'm new to this forum. Thanks and congrats to all for their great efforts building this site simply superb for all unix administrators. My requirement is to remove extensions of the files in the current directory. I'm doing it using below script which is working but i think it is... (12 Replies)
Discussion started by: prvnrk
12 Replies

3. 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

4. Shell Programming and Scripting

sed script to remove nth characters from end of filename

Hi all, I have this basic script to remove, in this case 9 characters from the end of a file name. This is what I have so far, for file in *.mov do newname=`echo $file | sed 's/\(.*\)........./\1/' ` mv "$file" "$newname" done The problem is that it removes the file extension as well.... (2 Replies)
Discussion started by: Monkey Dean
2 Replies

5. Shell Programming and Scripting

Remove filename is text file

Hello, I got files full path in a text file like that /main/k/kdelibs/kdelibs4c2a_3.5.10.dfsg.1-2ubuntu7_i386.deb /main/k/kdelibs-experimental/libknotificationitem-dev_4.3.2-0ubuntu1_i386.deb /main/k/kdemultimedia/dragonplayer_4.3.2-0ubuntu1_i386.deb... (13 Replies)
Discussion started by: davidkhan
13 Replies

6. Shell Programming and Scripting

[Solved] remove file extension

Hi, I have some files with some extension e.g. abc.xml.REMOVE,xyz.xml,efg.xml.REMOVE . I have to remove the .REMOVE extension. I can display it using the below script but cannot rename it. ls -l|sed 's/\.REMOVE//' How can I rename this? Thanks in advance (7 Replies)
Discussion started by: babom
7 Replies

7. 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

8. Shell Programming and Scripting

Remove the file except with particular extension

Hi all i am new for the shell scripting can any one help me with my requirments . i want to delete file older than 21 days everything works fine but in that dir i got the files with should not be deleted with particular extension like (.info):confused:here is the script i wrote .can anyone... (5 Replies)
Discussion started by: vikatakavi
5 Replies

9. 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

10. Shell Programming and Scripting

How to remove filename from output file?

Hello, I am trying to print searched multiple keywords in multiple files. It is almost okay with the code but the code puts filename in front of each line. How may I get rid of it? -grep -A1 'word1' *.txt | grep -A1 'word2' | grep -A1 'word3' I expect: Real outcome: How may I... (3 Replies)
Discussion started by: baris35
3 Replies
ID3_REMOVE_TAG(3)							 1							 ID3_REMOVE_TAG(3)

id3_remove_tag - Remove an existing ID3 tag

SYNOPSIS
bool id3_remove_tag (string $filename, [int $version = ID3_V1_0]) DESCRIPTION
id3_remove_tag(3) is used to remove the information stored of an ID3 tag. PARAMETERS
o $filename - The path to the MP3 file Instead of a filename you may also pass a valid stream resource o $version - Allows you to specify the version of the tag as MP3 files may contain both, version 1.x and version 2.x tags. RETURN VALUES
Returns TRUE on success or FALSE on failure. EXAMPLES
Example #1 id3_remove_tag(3) example <?php $result = id3_remove_tag( "path/to/example.mp3", ID3_V1_0 ); if ($result === true) { echo "Tag successfully removed "; } ?> If the file is writable and contained a 1.0 tag, this will output: Tag successfully removed NOTES
Note Currently id3_remove_tag(3) only supports version 1.0 and 1.1. If you choose to remove a 1.0 tag and the file contains a 1.1 tag, this tag will be removed, as v1.1 is only an extension of 1.0. SEE ALSO
id3_set_tag(3), id3_get_tag(3), id3_get_version(3). PHP Documentation Group ID3_REMOVE_TAG(3)
All times are GMT -4. The time now is 10:37 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy