Rename file knowing the first 7 carachters

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Rename file knowing the first 7 carachters
# 1  
Old 03-26-2008
Rename file knowing the first 7 carachters

Hi, people.

I need some help with this:

i have this file " PROVEDP_???_yyyymmdd " , and i want to rename to this
" IN_PROV_yyyy???.dat " .

The " ??? " is the Month , but the file could be created on April, but the name coulb be March, for example.So i need to grab the 3 caracters

Because the creation of the file is dynamic only i know is the first 8 caracheters " PROVEDP_ " .


PROVEDP_JAN_20080205 ( this file was created at 20080205 ) i need to convert to IN_PROV_200801.dat

Could someone help me on this ?


# 2  
Old 03-28-2008
This will do most of it:

for F in $(\ls -1 PROVEDP_*); do echo $F; mv $F $(echo $F | perl -pe 's/^PROVEDP_(\w{3})_(\d{4}).*$/IN_PROV_$2$1.dat/'); done

It lists all files and then renames them. For example, it will rename PROVEDP_JAN_20080205 to IN_PROV_2008JAN.dat.

However, it doesn't convert JAN to 01, as in your example. You initially said the output needed to contain the "???" you retrieved from the beginning of the file, but then, in your sample output, you showed "01" in the position which would have contained JAN.

So, if that was a typo, then you're done. If you still need it to be converted, then it'll be a little more work but not difficult for you to do.

Notes: The backslash before the ls command forces it to ignore any aliases, so you don't end up trying to process the file's date, owner, etc. The structure is a basic for loop. The section containing the echo and Perl statement within the $() structure returns the output of that section (actually a subshell) to the current command line as a simple string.

# 3  
Old 03-28-2008
awk 'BEGIN {
m["JAN"] = "01"
m["FEB"] = "02"
m["MAR"] = "03"
m["APR"] = "04"
m["MAY"] = "05"
m["JUN"] = "06"
m["JUL"] = "07"
m["AUG"] = "08"
m["SEP"] = "09"
m["OCT"] = "10"
m["NOV"] = "11"
m["DEC"] = "12"
sep = "_"
new = "IN_PROV"
ext = ".dat"
for (i=1; i<ARGC; i++) {
  split(ARGV[i], t, sep)
  print "mv", ARGV[i], new sep substr(t[3],1,4) m[t[2]] ext

Use nawk or /usr/xpg4/bin/awk on Solaris.
If you get the correct result, pass the output to the shell via pipe.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Knowing the boot files

Hi, I have experienced a situation whereby after rebooting the server, we were not able to bring it up. After further troubleshooting it was found that it was an issue with the "boot" image/file permission. Have you experienced such an issue? I was not the one who did the troubleshooting... (1 Reply)
Discussion started by: anaigini45
1 Replies

2. Shell Programming and Scripting

Curl to download file from after following location without knowing the file name/extens

This question could be specific to the site In the past, I've been able to download a file following location using cURL but there is something about that's different and can't figure out how to get it to work. I tried the following directly in the terminal with no... (5 Replies)
Discussion started by: MoonD
5 Replies

3. Shell Programming and Scripting

As knowing which version has the awk?

friends How do I know which version of awk using my linux? (2 Replies)
Discussion started by: tricampeon81
2 Replies

4. UNIX for Dummies Questions & Answers

Knowing when a different program modifies a file

so i was testing something on a test box running linux. i manually vi'ed the /var/log/messages file. and i noticed, the file immediately stopped being updated. it wasn't until i restarted the syslog process that events started being recorded in it again. so that tells me, the syslog process... (20 Replies)
Discussion started by: SkySmart
20 Replies

5. Shell Programming and Scripting

how to use the filehandle stored in a variable without knowing its file association

how to use the filehandle stored in a variable without knowing its file association i.e. the filename code my $logFH = $connObj->get('logFH'); infoPrint("Variable is of type IO \n") if(UNIVERSAL::isa($logFH, 'IO')); infoPrint("$logFH\n"); output == INFO :: Variable is of type... (0 Replies)
Discussion started by: rrd1986
0 Replies

6. Shell Programming and Scripting

Knowing whether the file has completely SFTP ed

Hi.. Can Anyone out there help me? I need to write a script to convert a file in EDCIDC format to CSV The files will be transfered through sftp to the box. Is there a way to check the file has finished being transfered or still transfering. so that my conversion task will be performed after... (3 Replies)
Discussion started by: ramukandada
3 Replies

7. Solaris

Knowing global zone

Hi All, I was asked these question long time back, Is there any way to know frm non-global zone to know which is the global zone it belongs? As per my knowledge there is no way and i have looked for these topic for a quite a while and researched on it .I just want to be sure if i am... (1 Reply)
Discussion started by: sahil_shine
1 Replies

8. UNIX for Advanced & Expert Users

knowing progress while reading a file

Hi, I am parsing a very big file say 10 MB. It 'll take more than an hour ..I want to know the progress say in % .Is there any way to do that??? or (Is there any way to know which line of the file I am in ) (2 Replies)
Discussion started by: sakthi.abdullah
2 Replies

9. AIX

Knowing when is the user id expiring

HI, Do we have a command in AIX which will let us know when is the user id password getting expired!! Any advice will be of great help!! Thanks, Siddharth (0 Replies)
Discussion started by: siddhhuu
0 Replies

10. UNIX for Dummies Questions & Answers

problem knowing disk space

i am a fresh person learning sco open server 5.5:confused: how should i know that how much space is occupied & how much is free on a particular partition?please help (1 Reply)
Discussion started by: buntty
1 Replies
Login or Register to Ask a Question

Featured Tech Videos