LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Parameters in sed command (https://www.linuxquestions.org/questions/linux-newbie-8/parameters-in-sed-command-144135/)

linuxdev 02-09-2004 02:02 PM

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

david_ross 02-09-2004 02:16 PM

What about something like:
Code:

#!/bin/bash
ver=1
while [ -f "file$ver" ];do
ver=$(($ver+1))
done
echo $ver

This will check for files called "file1", "file2", "file3" and so un until a file with that version doesn't exist - then you can just use $ver for the version number in your script.

linuxdev 02-09-2004 02:27 PM

Quote:

Originally posted by david_ross
What about something like:
Code:

#!/bin/bash
ver=1
while [ -f "file$ver" ];do
ver=$(($ver+1))
done
echo $ver

This will check for files called "file1", "file2", "file3" and so un until a file with that version doesn't exist - then you can just use $ver for the version number in your script.


_>>>>>>>>>>>>>>>>>>>
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

david_ross 02-09-2004 02:31 PM

Re: Parameters in sed command
 
In your first post you said you were doing this within a script. Is this not the case?
Quote:

Originally posted by linuxdev
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...


linuxdev 02-09-2004 02:46 PM

Re: Re: Parameters in sed command
 
Quote:

Originally posted by david_ross
In your first post you said you were doing this within a script. Is this not the case?


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

david_ross 02-09-2004 02:52 PM

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`

linuxdev 02-09-2004 04:17 PM

Quote:

Originally posted by david_ross
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`


->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
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??????

david_ross 02-09-2004 04:36 PM

Are you definately using backticks?

crabboy 02-09-2004 04:36 PM

Try:
Code:

DATE=`date`

sed "s/oldfile/newfile.v${DATE}/" oldfilename >newfilename


linuxdev 02-09-2004 04:59 PM

Quote:

Originally posted by crabboy
Try:
Code:

DATE=`date`

sed "s/oldfile/newfile.v${DATE}/" oldfilename >newfilename



->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
This option didn't work somehow

DATE=`date`

sed "s/oldfile/newfile.v${DATE}/" oldfilename >newfilename


?????????????

linuxdev 02-09-2004 05:00 PM

Quote:

Originally posted by david_ross
Are you definately using backticks?

->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Yeah I am using ` backticks ............
but it didn't work

?????????

david_ross 02-09-2004 05:06 PM

Perhaps you could post the whole script so we can see what you are trying to do.

sundialsvcs 02-09-2004 05:48 PM

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.

homey 02-09-2004 06:14 PM

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
for i in *.txt;
do
r=$(( $r + 1 ))
n=`date '+%d'`
j=`echo $i |sed -e "s/\(.*\)//g"`
echo mv "$i" "file$r.ver${n}.txt"
done



All times are GMT -5. The time now is 11:06 PM.