Visit Our UNIX and Linux User Community

rename files Ax based on strings found in files Bx

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting rename files Ax based on strings found in files Bx
# 8  
Old 10-07-2009
Hello, فارس الأحزان!

In case you forgot to read the forum rules, here is quick copy.


(1) No flames, shouting (all caps), sarcasm, bullying, profanity or arrogant posts.

(2) No negative comments about others or impolite remarks. Be patient.

(3) Refrain from idle chatter that does not contribute to the knowledge base. This does not apply to the forums in The Unix Lounge which are for off-topic discussions.

(4) Do not 'bump up' questions if they are not answered promptly. No duplicate or cross-posting and do not report a post or send a private message where your goal is to get an answer more quickly.

(5) Search the forums database with your keywords before asking.

(6) Do not post classroom or homework problems.

(7) No job postings from headhunters or recruiters except in The Unix Forums Job Board. See How to Post to The UNIX Forums Job Board for information on using the Job Board.

(8) No BSD vs. Linux vs. Windows or similar threads.

(9) Edit your posts if you see spelling or grammar errors (don't write in cyberchat or cyberpunk style). English only.

(10) Don't post your email address and ask for an email reply. Don't send a private message with a technical question. The forums are for the benefit of all, so all Q&A should take place in the forums.

(11) Post questions with descriptive subjects. For example, do not post questions with subjects like "Help Me!", "Urgent!!" or "Doubt". Post subjects like "Execution Problems with Cron" or "Help with Backup Shell Script".

(12) These are not hacker boards so hacker related posts will be promptly deleted or moderated.

(13) The forum administrators reserve the right to prune, move or edit posts that do not adhere to the rules or are technically inaccurate.

(14) The forum administrators reserve the right to remove users or change their posting status to read only without notice if any rules are not followed.

(15) No smoking in the forums.

The UNIX and Linux Forums
# 9  
Old 10-08-2009

Thanks a lot! I'm going to try to adapt your code...

# 10  
Old 10-08-2009
@steadyonabix thanks a lot for your code!

I tried to adapt your code but I always get only one file named ".SPL" even when I change the keywords to a single letter for testing purposes...
Seems like I'm doing something the wrong way.

Additional background information: I currently have to use cygwin with bash on WinXP...
Therefore I amended the code as listed at the end of this post.
The keywords are b v i w a c s b v i w a c s (at least when I'm looking at them in bash) and A d o b e

the result I'm looking for would be smuAB462691_200908148 19 27 296 - Bericht.SPL

I've attached two files (one spl and one original shd) for illustration. It would be great if you could take a short look at them.

Thanks again for your help!

current code:
ls *.SHD | while read FILE_NAME
        if [[ -w ${FILE_NAME%.*}.SPL ]] ## Ignore any file with no .spl file
                NEW_FILE_NAME=$( awk ' BEGIN {
                        ## Spaces in target strings ????
                        first = "b v i w a c s   b v i w a c s"   
                        last = "A d o b e"
                ( $0 ~ first ) && ( $0 ~ last ) {
                        startm = index( $0, first ) + length( first )
                        endm = index( $0, last ) - 1
                        file_name = substr( $0, startm, endm - startm )
                } END {
                        ## Remove spaces from new file name
                        gsub( / /, "", file_name )              
                        print file_name
                } ' $FILE_NAME).SPL

                ## Copy. change cp to mv if required
                cp ${FILE_NAME%.*}.SPL $NEW_FILE_NAME           
                echo "File ${FILE_NAME} has no matching .spl file"

Last edited by inCH; 10-08-2009 at 08:29 AM..
# 11  
Old 10-08-2009

I am going out tonight so will not be able to look at this before tomorrow.

I don't use Cygwin so can't promise much.

There is no substitute for experimenting with it yourself though in the meantime.

Good luck
# 12  
Old 10-09-2009
Bug Solution

Hi Inch

Well this turned out to be more interesting than I originally thought it would be: -

When I got your file I found it would not behave with awk or sed or any of the usual utilities so I took a look at its internal structure by doing an octal dump of the contents.
Here is the part containing your start string with the individual letters highlighted in bold: -

TX5XN:/home/brad/forum/inch>od -c FP00000.SHD | pg                                                                                                                   

0003440   H   & 375 032 001 002  \0  \0   b  \0   v  \0   i  \0   w  \0
0003460   a  \0   c  \0   s  \0  \0  \0   b  \0   v  \0   i  \0   w  \0
0003500   a  \0   c  \0   s  \0  \0  \0   s  \0   m  \0   u  \0   A  \0

As you can see each letter is delimited by a number 0 or null, so anything like awk or sed will fail as they look for string based files, not null delimited chars.
This is why your original paste of the file contained so many unprintable characters and spaces between each letter of your target string.

Having established the problem it is now a simple fix, just remove the nulls prior to manipulating the strings: -

TX5XN:/home/brad/forum/inch>tr -d "\000" < FP00000.SHD | strings
Adobe PDF
bviwacsbviwacssmuAB462691_200908148 19 27 296 - BerichtAdobe PDFAdobe PDF ConverterWinPrintNT EMF 1.008\\PC267IWACS

So adding the stripping of the nulls to the tool gives us the ability to correctly process the string we want to turn into a file name: -

TX5XN:/home/brad/forum/inch>ls -l
total 168
-rw-r--r-- 1 brad root  2080 2009-08-14 20:10 FP00000.SHD
-rw-r--r-- 1 brad root 75368 2009-10-08 11:46 FP00000.SPL
-rwxrwxrwx 1 brad root   696 2009-10-09 17:48 inch
-rw-r--r-- 1 brad root 75368 2009-10-09 17:48 smuAB462691_2009081481927296-Berich.SPL

Here is the modified code: -

ls *.SHD | while read FILE_NAME
    if [[ -w ${FILE_NAME%.*}.SPL ]]    ## Ignore any file with no .SPL file
        NEW_FILE_NAME=$( tr -d "\000" < $FILE_NAME | strings | nawk ' BEGIN {
            ## Spaces in target strings ????
            first = "viwacsbviwacs"
            last = "Adobe"
        ( $0 ~ first ) && ( $0 ~ last ) {
            startm = index( $0, first ) + length( first )
            endm = index( $0, last ) - 1
            file_name = substr( $0, startm, endm - startm )
        } END {
            ## Remove spaces from new file name
            gsub( / /, "", file_name )    
            print file_name
        } ' ).SPL

        ## Copy. change cp to mv if required
        cp ${FILE_NAME%.*}.SPL $NEW_FILE_NAME
        echo "File ${FILE_NAME} has no matching .SPL file"

Note there is no real error checking for existing files etc, I will leave you to add that yourself.
You should also note it needs to be run in the target directory so you will need to modify it if you want to handle multiple directories etc.
Until you add this kind of validation I would copy the files into a work directory and process them there first to avoid any unfortunate mishaps or lost data.

Hope it is usefull.........

Last edited by steadyonabix; 10-09-2009 at 02:39 PM.. Reason: Format text
# 13  
Old 10-12-2009
cool - thanks a lot!
Now it's perfectly working.

Previous Thread | Next Thread
Test Your Knowledge in Computers #638
Difficulty: Easy
The original iPhone had 16GB of memory.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to pass strings from a list of strings from another file and create multiple files?

Hello Everyone , Iam a newbie to shell programming and iam reaching out if anyone can help in this :- I have two files 1) Insert.txt 2) partition_list.txt insert.txt looks like this :- insert into emp1 partition (partition_name) (a1, b2, c4, s6, d8) select a1, b2, c4, (2 Replies)
Discussion started by: nubie2linux
2 Replies

2. UNIX for Beginners Questions & Answers

Rename files based on simple text file

Hello! New here although not completely new to Unix. I wonder how I could rename files based on the data found in a simple textfile. It goes like this: I have 4 files 1 ldfgkkfjslkdfjsldkfjsf.wav 2 nndsdflksdjf.wav 3 sdflksjdf jjsdflsdfl.wav 4 dkadsdddd.wav Textfile.txt looks like... (14 Replies)
Discussion started by: Oortone
14 Replies

3. Shell Programming and Scripting

SBATCH trinity for multiple files and rename/move the output files

Hey guys, I have wrote the following script to apply a module named "trinity" on my files. (it takes two input files and spit a trinity.fasta as output) #!/bin/bash -l #SBATCH -p node #SBATCH -A <projectID> #SBATCH -n 16 #SBATCH -t 7-00:00:00 #SBATCH --mem=128GB #SBATCH --mail-type=ALL... (1 Reply)
Discussion started by: @man
1 Replies

4. Shell Programming and Scripting

Rename files based on name in text file

Hello, I have a text file "file.list" with the contents below. file1 filename1 file2 filename2 file3 filename3 file1, file2 and file3 are files existing in the same directory as the text file file.list. I want to rename file1 to filename1, file2 to filename2, as show in the text... (1 Reply)
Discussion started by: james2009
1 Replies

5. UNIX for Dummies Questions & Answers

Rename files based on a list

Hi, I have a directory with a lot of files like this: a.bam b.bam c.bam I like to rename these files based on a list where the name of the files in the first column will be replasced by the names in the second column. Here is my list which is a tab-delimited text file: a x b y c ... (4 Replies)
Discussion started by: a_bahreini
4 Replies

6. Shell Programming and Scripting

Script to unzip files and Rename the Output-files

Hi all, I have a many folders with zipped files in them. The zipped files are txt files from different folders. The txt files have the same names. If i try to find . -type f -name "*.zip" -exec cp -R {} /myhome/ZIP \; it fails since the ZIP files from different folders have the same names and... (2 Replies)
Discussion started by: pmkenya
2 Replies

7. Shell Programming and Scripting

Finding/replacing strings in some files based on a file

Hi, We have a file (e.g. a .csv file, but could be any other format), with 2 columns: the old value and the new value. We need to modify all the files within the current directory (including subdirectories), so find and replace the contents found in the first column within the file, with the... (9 Replies)
Discussion started by: Talkabout
9 Replies

8. UNIX for Dummies Questions & Answers

rename files based on their respective directory name

I have a number of files in directories labeled like this: /Data/tr_gray/tr_DTI/dti_FA.nii.gz (the brackets here represent a range of number that the files are labeled with) I need to rename each dti_FA.nii.gz file according to the name of the folder it resides in. For example, the file ... (3 Replies)
Discussion started by: tk0034
3 Replies

9. Shell Programming and Scripting

How to strip ^M at end of each files for all files found in current directory

I am trying to use a loop to strip of the funny character ^M at the end of all lines in each file found in current directory and I have used the following in a script: find . -type f -name '*.txt' | while read file do echo "stripping ^M from ..." ex - "$file" > $tempfile %s/^M//g wq! # mv... (4 Replies)
Discussion started by: bisip99
4 Replies

10. Shell Programming and Scripting

Rename files/directories based on their name

i have hundreds of directories that have to be renamed. the directory structure is fairly uniform which makes the scripting a little simpler. suppose i have many directories like this */*/*/*abc* (in other words i have similar directory names 3 dirs deep that all contain the pattern abc in... (8 Replies)
Discussion started by: quantumechanix
8 Replies

Featured Tech Videos