|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
Quote:
Please elaborate. You want to change the filename to what? |
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|