Inserting variable values in filename | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Inserting variable values in filename

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-07-2013
CAch CAch is offline
Registered User
 
Join Date: May 2011
Last Activity: 17 December 2013, 7:24 AM EST
Posts: 62
Thanks: 24
Thanked 1 Time in 1 Post
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

Code:
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 :

Code:
a_2.4
b_3.2
c_4.3


Last edited by Scrutinizer; 03-07-2013 at 06:48 AM.. Reason: code tags
Sponsored Links
    #2  
Old 03-07-2013
balajesuri's Avatar
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 18 September 2014, 1:00 PM EDT
Location: India
Posts: 1,827
Thanks: 25
Thanked 524 Times in 503 Posts

Code:
#! /bin/bash
while read v1 v2 v3 v4 v5 v6 v7
do
    [ -e $v5 ] && mv "$v5" "${v5%.txt}_$v7"
done < filenamelist.txt

The Following User Says Thank You to balajesuri For This Useful Post:
CAch (04-05-2013)
Sponsored Links
    #3  
Old 03-07-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 18 September 2014, 5:49 PM EDT
Location: Aachen, Germany
Posts: 4,266
Thanks: 72
Thanked 1,030 Times in 976 Posts
I don't see any of the file names cited
a) in col 2 of your filenamelist.txt
b) in your desired output sample.
    #4  
Old 04-05-2013
CAch CAch is offline
Registered User
 
Join Date: May 2011
Last Activity: 17 December 2013, 7:24 AM EST
Posts: 62
Thanks: 24
Thanked 1 Time in 1 Post
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
Sponsored Links
    #5  
Old 04-05-2013
balajesuri's Avatar
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 18 September 2014, 1:00 PM EDT
Location: India
Posts: 1,827
Thanks: 25
Thanked 524 Times in 503 Posts

Code:
[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 ~]#

Sponsored Links
    #6  
Old 04-05-2013
CAch CAch is offline
Registered User
 
Join Date: May 2011
Last Activity: 17 December 2013, 7:24 AM EST
Posts: 62
Thanks: 24
Thanked 1 Time in 1 Post
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.

Last edited by CAch; 04-05-2013 at 03:03 AM..
Sponsored Links
    #7  
Old 04-05-2013
balajesuri's Avatar
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 18 September 2014, 1:00 PM EDT
Location: India
Posts: 1,827
Thanks: 25
Thanked 524 Times in 503 Posts
Quote:
Originally Posted by CAch View Post
Filename in output is a, b, c followed by underscore and the values in column 7
The awk command in post #5 exactly does this.

Quote:
Originally Posted by CAch View Post
I basically want to change the filenames as it is printed above. Please suggest me something to change the filename.
Please elaborate. You want to change the filename to what?
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
modify a file by inserting a conditional values Indra2011 Shell Programming and Scripting 4 02-07-2012 05:26 AM
inserting a variable to a new line maitree Shell Programming and Scripting 7 06-09-2011 02:32 AM
Inserting variable value into filename TheSMan5 Shell Programming and Scripting 3 08-24-2009 10:35 PM
help in inserting values in date format ali560045 Shell Programming and Scripting 3 02-04-2008 09:12 AM
Inserting Values From A File Into A Table ragha81 Shell Programming and Scripting 2 09-16-2006 07:10 PM



All times are GMT -4. The time now is 09:47 PM.