Dynamically creating file names from portions of another file name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Dynamically creating file names from portions of another file name
# 1  
Old 05-18-2013
Dynamically creating file names from portions of another file name

Not sure how to do the following, but any help would be appreciated.
Has to be done using C shell (sorry about that).

I have about 300 files that I need this done for, but I am only going to give one example. I will just need to know how to execute your solution through some type of loop to get all three files.

I have a file named aaa.ccyymmdd.dat.gz.

I need to copy that file to a new name, but take the ccyymmdd out of the original filename and put it into the name of the new filename.

Final output would be xyz.ccyymmdd.dat.gz.

Example:

I have files:
Code:
aaa.20110101.dat.gz
aaa.20110401.dat.gz
aaa.20110701.dat.gz
aaa.20111001.dat.gz

and need to run something that will ls or list all the files, loop through them, copy the file over BUT name it:

Code:
xyz.20110101.dat.gz
xyz.20110401.dat.gz
xyz.20110701.dat.gz
xyz.20111001.dat.gz

MY BIGGEST CONCERN IS HOW TO SETUP THE LOOP.

Any help would be greatly appreciated.
Thanks.

Last edited by Scrutinizer; 05-18-2013 at 12:47 PM.. Reason: code tags
# 2  
Old 05-18-2013
awk is not shell dependent.

Code:
ls | awk -F. '{printf "mv "$0;$1="xyz";print " "$0}' OFS=. > rename.sh

you can verify the file and after that use

source rename.sh in the same directory.
# 3  
Old 05-18-2013
Try this ->
Code:
foreach i ( `ls` )
echo cp $i $i:s/aaa/xyz/
end

cp aaa.20110101.dat.gz xyz.20110101.dat.gz
cp aaa.20110401.dat.gz xyz.20110401.dat.gz
cp aaa.20110701.dat.gz xyz.20110701.dat.gz
cp aaa.20111001.dat.gz xyz.20111001.dat.gz


Last edited by xbin; 05-18-2013 at 02:26 PM.. Reason: changed mv to cp
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Match Fields between two files, print portions of each file together when matched in ([g]awk)'

I've written an awk script to compare two fields in two different files and then print portions of each file on the same line when matched. It works reasonably well, but every now and again, I notice some errors and cannot seem to figure out what the issue may be and am turning to you for help. ... (2 Replies)
Discussion started by: jvoot
2 Replies

2. UNIX for Beginners Questions & Answers

Match Strings between two files, print portions of each file together when matched ([g]awk)

I have two files and desire to use the strings from $1 of file 1 (file1.txt) as search criteria to find matches in $2 of file 2 (file2.txt). If matches are found I want to output the entire line of file 2 (file2.txt) followed by fields $2-$11 of file 1 (file1.txt). I can find the matches, I cannot... (7 Replies)
Discussion started by: jvoot
7 Replies

3. Shell Programming and Scripting

Exclude certain file names while selectingData files coming in different names in a file name called

Data files coming in different names in a file name called process.txt. 1. shipments_yyyymmdd.gz 2 Order_yyyymmdd.gz 3. Invoice_yyyymmdd.gz 4. globalorder_yyyymmdd.gz The process needs to discard all the below files and only process two of the 4 file names available ... (1 Reply)
Discussion started by: dsravanam
1 Replies

4. Shell Programming and Scripting

Shell Script to Dynamically Extract file content based on Parameters from a pdf file

Hi Guru's, I am new to shell scripting. I have a unique requirement: The system generates a single pdf(/tmp/ABC.pdf) file with Invoices for Multiple Customers, the format is something like this: Page1 >> Customer 1 >>Invoice1 + invoice 2 >> Page1 end Page2 >> Customer 2 >>Invoice 3 + Invoice 4... (3 Replies)
Discussion started by: DIps
3 Replies

5. Shell Programming and Scripting

Split File by Pattern with File Names in Source File... Awk?

Hi all, I'm pretty new to Shell scripting and I need some help to split a source text file into multiple files. The source has a row with pattern where the file needs to be split, and the pattern row also contains the file name of the destination for that specific piece. Here is an example: ... (2 Replies)
Discussion started by: cul8er
2 Replies

6. Shell Programming and Scripting

Shell script variable names created dynamically

Hi, I'm trying to use a config file to define frequencies for checking log files. If the config file contains a frequency it will be used else a default value. The format of the config file (and hence the environment variable) is FREQ_log_logname=value A test shell script as below:... (2 Replies)
Discussion started by: u671296
2 Replies

7. Shell Programming and Scripting

Creating array containing file names

I am wondering how I can save the file names (stored in $file or $fnames) in array which I can access with an index. alias MATH 'set \!:1 = `echo "\!:3-$" | bc -l`' set narg = $#argv while ($iarg < $narg) MATH iarg = $iarg + 1 set arg = $argv set opt = ` echo $arg | awk... (1 Reply)
Discussion started by: kristinu
1 Replies

8. Shell Programming and Scripting

Searching for file names in a directory while ignoring certain file names

Sun Solaris Unix Question Haven't been able to find any solution for this situation. Let's just say the file names listed below exist in a directory. I want the find command to find all files in this directory but at the same time I want to eliminate certain file names or files with certain... (2 Replies)
Discussion started by: 2reperry
2 Replies

9. Shell Programming and Scripting

Reading file names from a file and executing the relative file from shell script

Hi How can i dynamically read files names from a list file and execute them from a single shell script. Please help its urgent Thanks in Advance (4 Replies)
Discussion started by: anushilrai
4 Replies

10. Shell Programming and Scripting

How to set dynamically keys names in perl %hash

hello I have loop , in this loop im picking names , this names I want to be keys in %hash but I don't know how to set in every loop entertain different key in the %hash (1 Reply)
Discussion started by: umen
1 Replies
Login or Register to Ask a Question