Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Inserting variable values in filename

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 07:48 AM.. Reason: code tags
Sponsored Links
    #2  
Old Unix and Linux 03-07-2013
balajesuri's Unix or Linux Image
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 1 December 2016, 8:47 AM EST
Location: India
Posts: 1,966
Thanks: 30
Thanked 567 Times in 543 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 Unix and Linux 03-07-2013
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 3 December 2016, 2:05 PM EST
Location: Aachen, Germany
Posts: 9,896
Thanks: 198
Thanked 2,942 Times in 2,747 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 Unix and Linux 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 Unix and Linux 04-05-2013
balajesuri's Unix or Linux Image
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 1 December 2016, 8:47 AM EST
Location: India
Posts: 1,966
Thanks: 30
Thanked 567 Times in 543 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 Unix and Linux 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 04:03 AM..
Sponsored Links
    #7  
Old Unix and Linux 04-05-2013
balajesuri's Unix or Linux Image
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 1 December 2016, 8:47 AM EST
Location: India
Posts: 1,966
Thanks: 30
Thanked 567 Times in 543 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 Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 06:26 AM
inserting a variable to a new line maitree Shell Programming and Scripting 7 06-09-2011 03:32 AM
Inserting variable value into filename TheSMan5 Shell Programming and Scripting 3 08-24-2009 11:35 PM
help in inserting values in date format ali560045 Shell Programming and Scripting 3 02-04-2008 10:12 AM
Inserting Values From A File Into A Table ragha81 Shell Programming and Scripting 2 09-16-2006 08:10 PM



All times are GMT -4. The time now is 05:48 PM.