renaming file from text in a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers renaming file from text in a file
# 1  
Old 05-15-2009
renaming file from text in a file

Hello all,

I've read a couple different posts on this subject but couldn't quite get what I'm trying to do to work. I have a file and the last line always ends with the text below (without the "s), except the number 294 will be different each time. I want to rename this file to the number that appears on that line. e.g. rename testfile to 294 (without an extension) . Any suggestions or help would be much appreciated! Thanks.


"Status against revision: 294"
# 2  
Old 05-15-2009
I forgot to add that I'm in a linux environment. Thanks.
# 3  
Old 05-15-2009
you can use tail to get the last line. then pipe to awk or cut with delimiter ":" and get the second field. that would be your number. store the result in a variable, then rename using mv. this is actually very simple, so try your best. if not sure, look at the man page or any relevant shell scripting resource for info.
# 4  
Old 05-15-2009
Code:
sed -n '$s/.*[^0-9]\(.*\)/mv "myFile" "\1"/p' myFile | sh

# 5  
Old 05-17-2009
Try...
Code:
awk 'END{system("mv " FILENAME " " FILENAME "." $NF)}' testfile

# 6  
Old 05-18-2009
Rename

Just use move command 'mv' to change name of the file either with grep or sed.
# 7  
Old 05-18-2009
Renaming file

Thanks for all the suggestions. I ended up getting the results I wanted by doing the below. Thanks again.


filename=`tail temp | grep : | cut -b '16,17,18,29,30,31'`
mv temp /var/www/migrations/${filename}-`date +%m.%d.%y-%H:%M:%S`
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. Windows & DOS: Issues & Discussions

Renaming part of a text file ?

I have several files that are named like this "DotP_D14 - Verknüpfung" They all have the " - Verknüpfung" in common. I'd like to rename all of them to get rid of that last part. Is this possible with DOS on windows ? (1 Reply)
Discussion started by: pasc
1 Replies

3. Shell Programming and Scripting

Renaming file and check for the renamed file existence

Hi Am trying to move a file from one name to another When I do "ls" to check for the moved filename I can see the file but when I try the same with a script am unable.. I think am doing some pretty silly error.. please help.. toMove=`ls | grep -E "partition.+"` mv $toMove partition._org... (7 Replies)
Discussion started by: Priya Amaresh
7 Replies

4. UNIX for Dummies Questions & Answers

Extracting lines from a text file based on another text file with line numbers

Hi, I am trying to extract lines from a text file given a text file containing line numbers to be extracted from the first file. How do I go about doing this? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

5. UNIX for Dummies Questions & Answers

Converting a text file with irregular spacing into a space delimited text file?

I have a text file with irregular spacing between values which makes it really difficult to manipulate. Is there an easy way to convert it into a space delimited text file so that all the spaces, double spaces, triple spaces, tabs between numbers are converted into spaces. The file looks like this:... (5 Replies)
Discussion started by: evelibertine
5 Replies

6. Shell Programming and Scripting

Renaming a file and keep the extension of file as it is

Hi, I am trying to rename multiple filenames in a UNIX directory matching a specific condition. i=0 for file in `ls $filename` do echo "$file" #echo $i i=`expr $i + 1` mv $src_dir/${file} $src_dir/${file}_${i} done But this code renames the filenames and... (9 Replies)
Discussion started by: chetancrsp18
9 Replies

7. Shell Programming and Scripting

search text file in file if this file contains necessary text (awk,grep)

Hello friends! Help me pls to write correct awk and grep statements for my task: I have got files with name filename.txt It has such structure: Start of file FROM: address@domen.com (12...890) abc DATE: 11/23/2009 on Std SUBJECT: any subject End of file So, I must check, if this file... (4 Replies)
Discussion started by: candyme
4 Replies

8. Shell Programming and Scripting

File renaming from list of names contained in another file

I have to rename a large number of files so that the name of each file corresponds to a code number that is given side by side in a list (textfile). The list contains in column A the filename of the actual files to be renamed and in column B the name (a client code, 9 digits) that has to be... (7 Replies)
Discussion started by: netfreighter
7 Replies

9. Shell Programming and Scripting

Renaming a file use another file as a sequence calling a shl

have this shl that will FTP a file from the a directory in windows to UNIX, It get the name of the file stored in this variable $UpLoadFileName then put in the local directory LocalDir="${MPATH}/xxxxx/dat_files" that part seems to be working, but then I need to take that file and rename, I am using... (3 Replies)
Discussion started by: rechever
3 Replies

10. Shell Programming and Scripting

how can I bcp out a table into a text file including the header row in the text file

Hi All, I need to BCP out a table into a text file along with the table headers. Normal BCP out command only bulk copies the data, and not the headers. I am using the following command: bcp database1..table1 out file1.dat -c -t\| -b1000 -A8192 -Uuser -Ppassword -efile.dat.err Regards,... (0 Replies)
Discussion started by: shilpa_acc
0 Replies
Login or Register to Ask a Question