LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-09-2004, 03:02 PM   #1
linuxdev
Member
 
Registered: Feb 2004
Posts: 34

Rep: Reputation: 15
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
 
Old 02-09-2004, 03:16 PM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 66
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.
 
Old 02-09-2004, 03:27 PM   #3
linuxdev
Member
 
Registered: Feb 2004
Posts: 34

Original Poster
Rep: Reputation: 15
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
 
Old 02-09-2004, 03:31 PM   #4
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 66
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...
 
Old 02-09-2004, 03:46 PM   #5
linuxdev
Member
 
Registered: Feb 2004
Posts: 34

Original Poster
Rep: Reputation: 15
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
 
Old 02-09-2004, 03:52 PM   #6
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 66
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`
 
Old 02-09-2004, 05:17 PM   #7
linuxdev
Member
 
Registered: Feb 2004
Posts: 34

Original Poster
Rep: Reputation: 15
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??????
 
Old 02-09-2004, 05:36 PM   #8
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 66
Are you definately using backticks?
 
Old 02-09-2004, 05:36 PM   #9
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
Try:
Code:
DATE=`date`

sed "s/oldfile/newfile.v${DATE}/" oldfilename >newfilename
 
Old 02-09-2004, 05:59 PM   #10
linuxdev
Member
 
Registered: Feb 2004
Posts: 34

Original Poster
Rep: Reputation: 15
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


?????????????
 
Old 02-09-2004, 06:00 PM   #11
linuxdev
Member
 
Registered: Feb 2004
Posts: 34

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by david_ross
Are you definately using backticks?

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

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

?????????
 
Old 02-09-2004, 06:06 PM   #12
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 66
Perhaps you could post the whole script so we can see what you are trying to do.
 
Old 02-09-2004, 06:48 PM   #13
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 8,626
Blog Entries: 4

Rep: Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999
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.
 
Old 02-09-2004, 07:14 PM   #14
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 59
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed command rharris72 Programming 6 11-20-2005 09:26 PM
sed/awk most frequently used parameters ilnli Linux - General 1 07-02-2005 05:50 AM
positional parameters $1 $2 in sed command Warmduvet Programming 12 09-22-2004 04:56 PM
sed command pazvant Linux - Software 2 05-09-2004 01:58 PM
sed command linuxdev Linux - Newbie 9 02-24-2004 05:50 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration