Parameters in sed command
I am using sed command like
sed 's/oldfile/newfile.v/' oldfile >newfile........(in my script) I want to have change in version number of "newfile.v" whenever I run the script having sed command... In other words I want to replace "oldfile" with "newfile.1" then with "newfile.2", "newfile.3".......so on each time I run my script... I tired using $date to get latest version from systems but it didn't work? How can I achieve that if at all........ Thanks |
What about something like:
Code:
#!/bin/bash |
Quote:
_>>>>>>>>>>>>>>>>>>> I am not sure if I got u correctly but I am trying to change newfile.v dynamically inside the following statement sed 's/oldfile/newfile.v/' oldfile >newfile and it seems the $ver or $date doesn't work when used like sed 's/oldfile/newfile.v$date/' oldfile >newfile........................ note the "$date" inside sed statement above |
Re: Parameters in sed command
In your first post you said you were doing this within a script. Is this not the case?
Quote:
|
Re: Re: Parameters in sed command
Quote:
->>>>>>>>>>>>>>>>> Yeah the......... sed 's/oldfile/newfile.v/' oldfile >newfile ....is a line in my script.... but when I tired using $date inside this statement just to pick latest system date so that I can use it in my version reference...it didn't work pls let me know if I am still not clear in any way...Thanks for your insight |
To start with you wanted version numbers so that's what I suggested. If you want a date then try escaping the date command with whatever format you want - eg:
`date +%F_%T` |
Quote:
->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> I tired using `date` inside sed command but its not working .... sed 's/oldfile/newfile.v`date`/' oldfilename >newfilename in the results I am getting expression: newfile.v`date`......rather then newfile.v FEb 9............ probably I am using date in wrong way?????? |
Are you definately using backticks?
|
Try:
Code:
DATE=`date` |
Quote:
->>>>>>>>>>>>>>>>>>>>>>>>>>>>>> This option didn't work somehow DATE=`date` sed "s/oldfile/newfile.v${DATE}/" oldfilename >newfilename ????????????? |
Quote:
->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Yeah I am using ` backticks ............ but it didn't work ????????? |
Perhaps you could post the whole script so we can see what you are trying to do.
|
For what it's worth, the tool that I vastly prefer to use for most such things is awk, not sed.
Although sed is clearly a powerful piece of software, I find that it suffers greatly from chicken-scratch-itis which means that, even for my own code, I find myself staring at a line full of punctuation-marks ("chicken scratches") puzzling what they mean. |
This example is for files with the .txt extension. You can change that to an extention of the type which you are using or even no extension....for i in *;
Run the command to see what the output looks like. If that is what you have in mind, remove the word echo from echo mv "$i" "file$r.ver${n}.txt" Code:
#!/bin/bash |
All times are GMT -5. The time now is 11:06 PM. |