Taking part of one file name and putting it into a another file name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Taking part of one file name and putting it into a another file name
# 1  
Old 05-16-2013
Taking part of one file name and putting it into a 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 data.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 newdata.ccyymmdd.dat.gz.


Example:

I have
Code:
data.20100101.dat.gz
data.20100301.dat.gz
data.20100701.dat.gz
data.20101001.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:
newdata.20100101.dat.gz
newdata.20100301.dat.gz
newdata.20100701.dat.gz
newdata.20101001.dat.gz

Any help would be greatly appreciated.
Thanks.

Last edited by joeyg; 05-16-2013 at 12:07 PM.. Reason: Please wrap commands and data inside CodeTags - easier to read
# 2  
Old 05-16-2013
Does this help?

Code:
$ ls sample*.*
sample.txt  sample1.txt  sample10.txt  sample2.txt  sample3.txt  sample30.txt  sample31.txt  sample32.txt  sample4.txt


$ ls sample*.* | while read myfile; do cp $myfile "my"$myfile ; done


$ ls my*.*
mysample.txt   mysample10.txt  mysample3.txt   mysample31.txt  mysample4.txt
mysample1.txt  mysample2.txt   mysample30.txt  mysample32.txt

# 3  
Old 05-16-2013
This solution is great, but I think I might not have explained it correctly. i don't need the entire file name copied over, but just a portion. In my original example, there is a node in the file name that is a date, I just need the take that date and plug it into the new file name.

I will use part of your example, but modify it slightly.
Code:
ls sample*.*
sample.20100101.txt  sample1.20110101.txt 
sample10 20120101.txt  sample2.20130101.txt  

My output needs to be:
Code:
example.20100101.txt
example.20110101.txt
example.20120101.txt
example.20130101.txt

I am not sure how to get to/extract/cut out that date and how to put it in the new file name.

Thanks.

Last edited by Franklin52; 05-17-2013 at 03:21 AM.. Reason: Code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search pattern in a file taking input from another file

Hi, Below is my requirement File1: svasjsdhvassdvasdhhgvasddhvasdhasdjhvasdjsahvasdjvdasjdvvsadjhv vdjvsdjasvdasdjbasdjbasdjhasbdasjhdbjheasbdasjdsajhbjasbjasbhddjb svfsdhgvfdshgvfsdhfvsdadhfvsajhvasjdhvsajhdvsadjvhasjhdvjhsadjahs File2: sdh hgv I need a command such that... (8 Replies)
Discussion started by: imrandec85
8 Replies

2. Shell Programming and Scripting

Taking nth column and putting its value in n+1 column using awk

Hello Members, Need your expert opinion how to tackle below. I have an input file that looks like below: USS|AWCC|AFGAW|93|70 USSAA|Roshan TDCA|AFGTD|93|72,79 ALB|Vodafone|ALBVF|355|69 ALGEE|Wataniya (Nedjma)|DZAWT|213|50,550 I like output file in below format: ... (7 Replies)
Discussion started by: umarsatti
7 Replies

3. Shell Programming and Scripting

Taking information from a postgres sql query and putting it into a shell script array

I have a postgres sql statement that is the following: select age from students; which gives me the entries: Age --- 10 15 13 12 9 14 10 which is about 7 rows of data. Now what I would like to do with this is use a shell script to create an array age. As a results... (3 Replies)
Discussion started by: JSNY
3 Replies

4. UNIX for Dummies Questions & Answers

Putting file name inside file

I have a bunch of files with unique names. Inside each file are either 1 or more than 1 rows. I would like the name of the file to appear inside the file itself, once per row (except for the first row which is a header). For example: ls dog.1 cat.1 goat.1 tree.1 cat dog.1 ... (4 Replies)
Discussion started by: verse123
4 Replies

5. UNIX for Dummies Questions & Answers

Taking word count from file and printing in file

hi, i am having a file which contains the below content, i need to take the word count of if and print the file name also inputfile.txt file_name1.txt,type_name1.txt file_name2.txt,type_name2.txt i would need the word count of the files like this if file_name*.txt then wc -l... (10 Replies)
Discussion started by: rohit_shinez
10 Replies

6. Shell Programming and Scripting

Installing a bin file by taking input from a properties file

I need to install a bin file in UNIX which requires user interaction for giving some information like user id , path, sid etc. All these information is stored in a properties file in the same location. So if i give ./file.bin -f propfile.properties will it install the file taking all the... (1 Reply)
Discussion started by: coolmohere
1 Replies

7. Shell Programming and Scripting

How to ftp multiple files by taking the file name from a input file.

Hi, I'm working on a script which has to copy multiple files from one server to another server. The list of files that are to be copied is present in a file say input.txt. vi input.txt abc.c welcome.c new.c welcome1.c for ftp'ing a single file say 'new.c' the following code... (2 Replies)
Discussion started by: i.srini89
2 Replies

8. Shell Programming and Scripting

deleting from one file and putting into another file

Hi I have a file that is organized like this. Basically the name is indicated by >name, then on a newline there is a sequence of letters. The letters are ASKL however sometimes there are non ASKL's like U's, G's or O's. Basically what I want to do is cut out the UUUU and record them. So heres... (4 Replies)
Discussion started by: gisele_l
4 Replies

9. Shell Programming and Scripting

Read data from one file and putting in new file ??

Hello All, I have a file which contain data something like this: CELL 2 TEST AND DIAGNOSTIC UNIT 2 CELL 2, CDM 1 CBR 1 TRANSMIT PORT (TXPORT) 1 CELL 2, CDM 1 CBR 2 TRANSMIT PORT (TXPORT) 1 CELL 2, CDM 1 CBR 3 TRANSMIT PORT (TXPORT) 1 CELL 2, CDM 1 CBR 1 TRANSMIT PORT... (21 Replies)
Discussion started by: wakhan
21 Replies

10. Shell Programming and Scripting

taking a part from file name

hi i have a file with name "CT_Thread_Trace.384.20080728-121217.log" i need 384 , can any one help thanks Satya (5 Replies)
Discussion started by: Satyak
5 Replies
Login or Register to Ask a Question