Automatically correct a File Name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Automatically correct a File Name
# 8  
Old 05-07-2015
Hi RudiC

Like every word in the string needs to be validated?
# 9  
Old 05-07-2015
Every item that you deem necessary for the correct syntax of your file names; words, underscore count, string length, whatever...

Last edited by RudiC; 05-07-2015 at 08:14 AM..
# 10  
Old 05-07-2015
Thanks Rudi...

Hi Don

Can you please throw some more light on the code which u shared. Its working but removing all the rest of the files. Can the "*" in the for loop be replaced with anything else?
# 11  
Old 05-07-2015
Chatwithsaurav,

You simply have not provided us with sufficient information about the legal names and formats of your file for us to assist you at present.

The examples you provide are just random examples. Unless you can rigidly formulate your requirements and communicate them, nobody can help you.
# 12  
Old 05-07-2015
Hi Murphy

Well the thing is that I need to "rename" two files in the correct format.
The correct format are : "advext_OK_0315_mem_rule_firings.txt" and "advext_OK_0315_rule_thirdparty.txt"
Now the challenge is that when the user places them in a predefined directory, he/she places it with a different name. Example : "ad_OK_0315_me_rul_firin.txt" or "adv_0315_OK_m_rule_fir.txt" or "adv_OK_1503_m_r_firing.txt" or "ad_OK_1214_r_third.txt" or "OK_0315_ad_r_thirdparty.txt" .... etc...

The unix shell script will "validate" and "eliminate" these errors and rename them in the correct format. Also the script should accept input parameters in
places where there is plan name and run qual. Plan name is : OK (There are other plan names as well) and Run Qual is : 0315 (MMYY format - This will change everymonth)
# 13  
Old 05-07-2015
Making assumptions on what the user wanted is always critical to be truely the same.
Specialy regarding the deletion/moving/renaming of files, i'd only assume on creating new files/names.

Then, as RudiC proposed, run a validity check, like
  1. Does the input exist as file
  2. Does any of the input string parts (without the _'s) match any of the existing files
  3. Let the user select from the shortest (compare each list vs the others, still valid) list of existing files matching the users input the most.
  4. Use the select file (variable) for the existing process.

One could turn around the approach... (imo safer approach)
  1. Get a list of valid string parts by scanning/parsing the existing file(name)s.
  2. Ask the user part by part which string he wants to add, generate a filename-string from those selections.
  3. Check if the filename-string exists (or how many files match the current string), if not, loop again.
  4. Use the generated file(name-string).

hth

EDIT - Question:
Why, other than a simple char-typo-mix, would they write a different formated string.
You could show an example input (to the user, before they enter the input).

Last edited by sea; 05-07-2015 at 09:30 AM..
# 14  
Old 05-07-2015
Quote:
Originally Posted by chatwithsaurav
Thanks Rudi...
Hi Don

Can you please throw some more light on the code which u shared. Its working but removing all the rest of the files. Can the "*" in the for loop be replaced with anything else?
Did you not read my post?

The first line of that post in bold red text warned you not to run those examples. The last line of that post explicitly said that the second script would destroy all but one of the files in that directory. What it does is correct the name of every regular file in your current directory with a name ending in .txt to the name that you said the user should have named the file in the first place.

As many of us have repeatedly stated: Unless you can define what constitutes a valid name and specify how to map an invalid name to the "correct" valid name, that script is the best we can do (and IT IS NOT SUITABLE FOR ANY USE other than to show that we don't have enough information to do what you seem to want).

All that you have really told us is that you have a directory that contains some files. Some of the filenames are bad. There is one good name (advext_OK_2015_mem_rule_firings.txt). There may be other good names. Fix the bad filenames.

Our crystal balls aren't that good.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

LS command does not list the correct file

Hi, I am logged into as root & inside the home directory of another user. ls -a # ls -laq total 44 drwx------ 4 user1 adm 4096 Nov 23 05:10 . drwxr-xr-x. 12 root root 4096 Nov 22 13:05 .. -rw-r--r-- 1 user1 adm 18 Nov 22 13:05 .bash_logout -rw-r--r-- 1 user1 adm 193 Nov... (2 Replies)
Discussion started by: mohtashims
2 Replies

2. Shell Programming and Scripting

Extract the correct format of file Name

All, Objective : Extract the correct format of a file name. Please share the script command which will extract the correct format of the file after untarring. Example : If the file is of .csv format then extract filename.csv if the file is having .CSV then extract the same.CSV if the file... (1 Reply)
Discussion started by: Mahesh G
1 Replies

3. UNIX for Dummies Questions & Answers

After Ftp'ing file to destination how to check the file if it is in correct ASCII and not corrupted

Hi Folks, While transferring file from FTP software like Filezilla the files gets corrupted. Is there any way I can check if the recently transferred file is in ASCII and not corrupted. I have tried using file -i filename command which does tell if the file character set is ASCII or binary... (6 Replies)
Discussion started by: Khan28
6 Replies

4. Shell Programming and Scripting

Opening a file in vi and automatically save and quit this file using shell script

Hi friends, In my shell script, I want to open a file using vi editor. After opening the file in vi, I want to save and quit this file automatically.... all through shell script. the code segment is: ------------------------------------------------------------ cd ~/netfpga/projects/scone/sw/... (2 Replies)
Discussion started by: sachinteotia
2 Replies

5. UNIX for Advanced & Expert Users

Not getting correct uptime in messages file

The following notice I am getting in /var/adm/messages file Dec 9 06:17:24 hostname lw8: Main, up 476 days 04:42:23, Memory 9,742,808 Dec 9 10:17:25 hostname lw8: Main, up 476 days 08:42:23, Memory 10,179,864 But my actual uptime is :: 11:55am up 25 day(s), 10:38, 3 users, ... (5 Replies)
Discussion started by: brij123
5 Replies

6. Linux

how to automatically create a file?

how can i automatically create a file on Linux? like a process that searches for the file and if the file does not exist, it automatically makes the file (3 Replies)
Discussion started by: roozis
3 Replies

7. Shell Programming and Scripting

Getting the correct identifier in the output file

Hi All I do have a file like this: 1 1 12 26 289 3.2e-027 GCGTATGGCGGC 2 12 26 215 6.7e+006 TTCCACCTTTTG 3 9 26 175 8.9e+016 GCGGTAACT 4 20 26 232 1.7e+013 TTTTTATTTTTTTTTTTTCC 5 ... (6 Replies)
Discussion started by: Lucky Ali
6 Replies

8. Programming

How to get the correct exception file/line.

The below code throws error in the line number 32 where the function is defined. But How to find the line where the function is called. That is I want to throw the error at the line number 43 (as here the function is called). The code is: #include <iostream> #include <string>... (9 Replies)
Discussion started by: SamRoj
9 Replies

9. Shell Programming and Scripting

Open file with correct application

Can someone please help me with my bourne shell script. I am a struggling newbie. I need create a script that will read an argument from a command line, access a config file with application file types, and open the file with the correct application. The file needs to be able to handle file... (3 Replies)
Discussion started by: 3dtiger
3 Replies

10. UNIX for Advanced & Expert Users

Correct format in a log file

I'm trying to read the output of a .sql script (simple insert and commit oracle pl/slq script) to a log file using a shell script. My problem is I end up with a log file that looks like this: sd12@phenix97:/detain/sd12/logs > cat 20071205_detain_20071206.log 12320496 rows created. Commit... (11 Replies)
Discussion started by: sd12
11 Replies
Login or Register to Ask a Question