Rename file from data in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Rename file from data in file
# 1  
Old 03-19-2010
Rename file from data in file

Smilie
I have this script to read a file from a directory,
for FILE in `ls -1 ../dir` ;do
echo Renaming $FILE


with a filename TRO029274465.txt now i want to rename this file to a certain string found inside this textfile.

For example this
TRO029274465.txt contains
H20090812982009081211021025301000000012689925
T200908129820000105700668330000000126899250000000214600004
...

i want to get the string in 9-16
"9820090812"
so that my new file name would be 20090812.98

Been trying on some commands but it seems that i dont get the right format for this one.
pls help. I'm a linux newbieSmilie

Thanks,
-dying octuposSmilie
# 2  
Old 03-19-2010
Quote:
Originally Posted by octupos
Smilie
I have this script to read a file from a directory,
for FILE in `ls -1 ../dir` ;do
echo Renaming $FILE


with a filename TRO029274465.txt now i want to rename this file to a certain string found inside this textfile.

For example this
TRO029274465.txt contains
H20090812982009081211021025301000000012689925
T200908129820000105700668330000000126899250000000214600004
...

i want to get the string in 9-16
"9820090812"
so that my new file name would be 20090812.98

Been trying on some commands but it seems that i dont get the right format for this one.
pls help. I'm a linux newbieSmilie

Thanks,
-dying octuposSmilie
Welcome.

First, be careful with the construct of

Code:
for FILE in `ls -1 ../dir`

The shell (default) will break the file names if they contain spaces, tabs, etc. It is also not necessary.

Instead, use:

Code:
for FILE in ../dir/*

You do not specify where in the file the string in question is? Top line in the file? Does the the string need to be greped?

More detail needed...
# 3  
Old 03-19-2010
Yeap the string[9-16] in the top of the line. And no it doesn't need to be grepped.
My apologies.
# 4  
Old 03-20-2010
Code:
for file in `ls dir`
do
    echo $file
    var=`head -1 dir/$file |cut -c 10-19 | sed -r 's/([0-9]{2})([0-9]+$)/\2.\1'/`
    echo $var;
    `mv A/$file dir/$var`
done

Here dir is the directory name. So you can give your directory.
# 5  
Old 03-20-2010
hi drewk,
i already found a command for this one:

name=`cut -c1 12-19 $FILE`

but my problem now is it works only with those file who only have one line or row.
but my file contains many rowsSmilie

-dying octuposSmilie

---------- Post updated at 10:37 PM ---------- Previous update was at 10:20 PM ----------

Quote:
Originally Posted by murugaperumal
Code:
for file in `ls dir`
do
    echo $file
    var=`head -1 dir/$file |cut -c 10-19 | sed -r 's/([0-9]{2})([0-9]+$)/\2.\1'/`
    echo $var;
    `mv A/$file dir/$var`
done

Here dir is the directory name. So you can give your directory.

OMG!! this works very fine!!

thank you very much murugaperumal
# 6  
Old 03-20-2010
If this is bash, there's no need for a command, just use parameter expansion..
Code:
${string:11:8} and ${string:9:2}

First get the string into a variable. You may want to use "string=$(head -1 yourfile)", I just did it differently as an example. Then the parameter expansion is used to cut out portions of the variable using two numbers to locate the substring you want. In the first one, it cuts out 11 characters from the left and leaves in the next 8. In the second one, it cuts out 9 and leaves the next two. Put a dot between them and you have the filename you need.

Code:
# string=H20090812982009081211021025301000000012689925
# echo $string
H20090812982009081211021025301000000012689925
# echo ${string:11:8}.${string:9:2}
20090812.98
#

Your code may be something like this:
Code:
NEWNAME=$(head -1 $FILE)
mv $FILE ${NEWNAME:11:8}.${NEWNAME:9:2}

# 7  
Old 03-20-2010
Code:
echo "H20090812982009081211021025301000000012689925" |awk '{print substr($0,12,6)"."substr($0,10,2)}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Rename file in directory using contents within each file

In the below there are two generic .vcf files (genome.S1.vcf and genome.S2.vcf) in a directory. There wont always be two genaric files but I am trying to use bash to rename each of these generic files with specfic text (unique identifier) within in each .vcf. The text will always be different, but... (11 Replies)
Discussion started by: cmccabe
11 Replies

2. Shell Programming and Scripting

Bash to copy file 3 times and rename based on another file

In the below bash I am trying to copy the only text file (always only one) in /home/cmccabe/Desktop/list/QC/metrics.txt and rename each of the 3 text files according to /home/cmccabe/Desktop/test/list.txt using lines 3, 4 ,5. This format (that is list.txt) is always 5 lines. Thank you :). ... (12 Replies)
Discussion started by: cmccabe
12 Replies

3. Shell Programming and Scripting

Rename specific file extension in directory with match to another file in bash

I have a specific set (all ending with .bam) of downloaded files in a directory /home/cmccabe/Desktop/NGS/API/2-15-2016. What I am trying to do is use a match to $2 in name to rename the downloaded files. To make things a more involved the date of the folder is unique and in the header of name... (1 Reply)
Discussion started by: cmccabe
1 Replies

4. UNIX for Dummies Questions & Answers

awk - Rename output file, after processing, same as input file

I have one input file ABC.txt and one output DEF.txt. After the ABC is processed and created output, I want to rename ABC.txt to ABC.orig and DEF to ABC.txt. Currently when I am doing this, it does not process the input file as it cannot read and write to the same file. How can I achieve this? ... (12 Replies)
Discussion started by: High-T
12 Replies

5. UNIX for Dummies Questions & Answers

Mapping a data in a file and delete line in source file if data does not exist.

Hi Guys, Please help me with my problem here: I have a source file: 1212 23232 343434 ASAS1 4 3212 23232 343434 ASAS2 4 3234 23232 343434 QWQW1 4 1134 23232 343434 QWQW2 4 3212 23232 343434 QWQW3 4 and a mapping... (4 Replies)
Discussion started by: kokoro
4 Replies

6. UNIX for Dummies Questions & Answers

look for specific values in a file and rename file with value found

Hi, i have a file with some data ..look for some specific value in the file and if found that value rename the file with the value found in the file.. ex.. File.txt 1236 43715825601ANDERSSON, 1236 437158256031963040120060901200609010000000 1236 43715825604123 MCCL AVE UPPER 1236 ... (11 Replies)
Discussion started by: dssyadav
11 Replies

7. Shell Programming and Scripting

.sh file To rename existing file and copy new file

Hi All, I am very new to shell scripting . In my current task i want to create .sh file that will rename the existing file with appending _bu in it. And then copy new file . e.g if i have file linuxFirst.java then i want to rename it to linuxFirst_bu.java ..Then want replace with latest... (1 Reply)
Discussion started by: maheshkaranjkar
1 Replies

8. Shell Programming and Scripting

A script that will move a file to a directory with the same name and then rename that file

Hello all. I am new to this forum (and somewhat new to UNIX / LINUX - I started using ubuntu 1 year ago).:b: I have the following problem that I have not been able to figure out how to take care of and I was wondering if anyone could help me out.:confused: I have all of my music stored in... (7 Replies)
Discussion started by: marcozd
7 Replies

9. UNIX for Dummies Questions & Answers

Rename file based on first 3 characters of data in file

I'm looking to determine if I can use a grep command to read file and rename the file based on the first 3 characters of the data in the file. An example is: Read FileA If the first 3 positions of the data in the file are "ITP", then rename the file as FileA_ITP, else if the first 3... (3 Replies)
Discussion started by: jchappel
3 Replies

10. UNIX for Dummies Questions & Answers

Help with multiple file rename - change case of part of file name

Hi there, I hope someone can help me with this problem : I have a directory (/var/www/file/imgprofil) which contains about 10000 JPG files. They have a naming convention thus : prefix-date-key-suffix.jpg they all have the prefix p-20050608- then AAAA is a 4 letter code the suffix is... (7 Replies)
Discussion started by: steve7
7 Replies
Login or Register to Ask a Question