Inserting variable values in filename

Hi All,

I have a directory containing multiple files.
and also a txt file which contains the list of all filenames and certain values.

I want to read the text file line by line and if its 2nd column is matched with the filename in directory, then it must insert the values in 7th column to the filename.

ex: DIR [ a.txt
b.txt
c.txt]

filenamelist.txt

assa  dsad werr vdss a.txt is 2.4
assa  dsad werr vdss b.txt is 3.2
assa  dsad werr vdss c.txt is 4.3

result required in DIR :

a_2.4
b_3.2
c_4.3
#! /bin/bash
while read v1 v2 v3 v4 v5 v6 v7
do
    [ -e $v5 ] && mv "$v5" "${v5%.txt}_$v7"
done < filenamelist.txt
1 Like

I don't see any of the file names cited
a) in col 2 of your filenamelist.txt
b) in your desired output sample.

Hi Balajesuri
Thanks for the reply, but unfortunately this code is not working.....
Although its not showing any error..but i am not getting the required output.

what is the function of -e here?

Hi RudiC,

By mistake I wrote column 2, whereas the filename is in column 5. Filename in output is a, b, c followed by underscore and the values in column 7

[user@host ~]# cat file
assa  dsad werr vdss a.txt is 2.4
assa  dsad werr vdss b.txt is 3.2
assa  dsad werr vdss c.txt is 4.3
[user@host ~]# awk '{sub(/.txt/,"",$5); print $5"_"$7}' file
a_2.4
b_3.2
c_4.3
[user@host ~]#

Hi Balajesuri,

It is actually printing the above result shown by you but filenames are as such....I basically want to change the filenames as it is printed above.
Please suggest me something to change the filename.

The awk command in post #5 exactly does this.

Please elaborate. You want to change the filename to what?

I have a directory containing 1000's of files with extenxion.pdb

like a.pdb
b.pdb
c.pdb

Also, I have a text file which contains 7 columns. Out of which 5th column consists of current filename list and 7th column have some values. which I need to insert in the new filename with an "underscore".

So, the previous filename was

a.pdb
b.pdb
c.pdb

and the new filename must be

a_2.4 
b_3.2 
c_4.3

Would it work if you changed .txt to .pdb in the awk one-liner?

awk '{sub(/.pdb/,"",$5); print $5"_"$7}' file

Yes, it is printing the way I wanted but it is not changing the filename.

New filenames with underscore and the required values are printed on screen, but the original file remains as such with previous name.
My problem is to change the filename.

The solution in post #2 works for me on my GNU/Linux running bash 4.1.10.
Could you please post the output of uname -a and bash --version

1 Like

Try this,

echo "assa  dsad werr vdss a.txt is 2.4
assa  dsad werr vdss b.txt is 3.2
assa  dsad werr vdss c.txt is 4.3" | eval mv `awk '{f=$5; sub(/.txt/,"",$5); print f" "$5"_"$7; }'`

This should do

1 Like

Thanks Balajesuri,

your second post is working now, earlier I was doing some mistake. But now, it is showing "?" at the end of filename

a_2.4?
b_3.2?
c_4.3?

Please re-check for any typo errors. Could you post the exact script that you're using for this?

I believe there should be something wrong with the input! awk is taking the 7th field correctly and supplying here. check for extra characters in the 7th field of ur file.