Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
sed alternative with hardlink support?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
TheSmallOne
Guru
Guru


Joined: 22 Jan 2005
Posts: 467
Location: Germany

PostPosted: Sun Mar 23, 2014 8:20 am    Post subject: sed alternative with hardlink support? Reply with quote

Hi,
not sure, if "programming" is the right subforum, but somehow it has to do with it.

I often change small parts of some websites, using sed:
Code:
sed -i -e "s#search#replace#g" */*.html

Now I've noticed, that the in-place option of sed doesn't really work in-place, but instead creates a new file, which is then replaced. This obviously breaks hardlinks.

Does anybody know an alternative to sed, which does changes really in place and therefore preserves existing hardlinks?
Back to top
View user's profile Send private message
Akkara
Bodhisattva
Bodhisattva


Joined: 28 Mar 2006
Posts: 6702
Location: &akkara

PostPosted: Sun Mar 23, 2014 8:53 am    Post subject: Reply with quote

I don't know if this is the best way of doing it, but here's an idea -

Run sed normally (without the -i inplace), sending its output into a new temporary file.

Then truncate the original file to size 0. Then append the temporary file to the original file. That should preserve hardlinks.

Example:
Quote:
$ echo 1234 >a
$ ln a b
$ ls -l a b
-rw-r--r-- 2 user user 5 Mar 23 11:44 a
-rw-r--r-- 2 user user 5 Mar 23 11:44 b
$ cat a
1234
$ cat b
1234

$ truncate --size 0 a ; echo ABCD >> a

$ ls -l a b
-rw-r--r-- 2 user user 5 Mar 23 11:46 a
-rw-r--r-- 2 user user 5 Mar 23 11:46 b
$ cat a
ABCD
$ cat b
ABCD

_________________
Many think that Dilbert is a comic. Unfortunately it is a documentary.
Back to top
View user's profile Send private message
aCOSwt
Bodhisattva
Bodhisattva


Joined: 19 Oct 2007
Posts: 2537
Location: Hilbert space

PostPosted: Sun Mar 23, 2014 8:54 am    Post subject: Re: sed alternative with hardlink support? Reply with quote

TheSmallOne wrote:
Now I've noticed, that the in-place option of sed doesn't really work in-place, but instead creates a new file, which is then replaced. This obviously breaks hardlinks.

You are correct.
What about using good old simple ed ?
I suggest :
Code:
acoswt@PrimaPratica ~ $ cat bidule.txt
inode
acoswt@PrimaPratica ~ $ ls -i bidule.txt
942922 bidule.txt

acoswt@PrimaPratica ~ $ printf '%s\n' ',s/^/KEEP/' w q | ed -s bidule.txt

acoswt@PrimaPratica ~ $ cat bidule.txt
KEEPinode
acoswt@PrimaPratica ~ $ ls -i bidule.txt
942922 bidule.txt

_________________
Back to top
View user's profile Send private message
Hu
Moderator
Moderator


Joined: 06 Mar 2007
Posts: 21633

PostPosted: Sun Mar 23, 2014 3:46 pm    Post subject: Reply with quote

Akkara wrote:
Then truncate the original file to size 0. Then append the temporary file to the original file. That should preserve hardlinks.
I think you can optimize that step by using an in-place overwrite of the original file with the temporary, rather than truncate+append. I do not know how to avoid creating the temporary file, but assuming you have original.file and temporary.file, running cat temporary.file > original.file should truncate it when the shell opens for writing, then place the contents of the cat'd file into the original. As with your example, this creates a brief window when the file content is neither the old file nor the new one, so it must not be used in the presence of concurrent readers.
Back to top
View user's profile Send private message
steveL
Watchman
Watchman


Joined: 13 Sep 2006
Posts: 5153
Location: The Peanut Gallery

PostPosted: Mon Mar 24, 2014 7:04 am    Post subject: Reply with quote

Hooray for ed! :-) #ed is open to you on IRC: chat.freenode.net should you wish to learn more.
Back to top
View user's profile Send private message
TheSmallOne
Guru
Guru


Joined: 22 Jan 2005
Posts: 467
Location: Germany

PostPosted: Mon Mar 24, 2014 11:42 am    Post subject: Re: sed alternative with hardlink support? Reply with quote

aCOSwt wrote:
You are correct.
What about using good old simple ed ?
I suggest :


Syntax looks much more complicated than a simple sed command. However, I just tried and there seems to be a much bigger problem:
It looks like ed doesn't work well with wildcards. I tried:
Code:
printf '%s\n' ',s#test#success#g' w q | ed -s *.testfiles

But for some reason, ed only changed a single file (the alphabetically first one).

As I wrote in my first post: I usually edit a large batch of files at once using different wildcards. If I just needed to change a single file, I'd be doing it with vi in the first place.
For this reason, the other suggestion in this thread (leaving out inplace and manually merging the temporary files with the real files) is not useful as well.

I guess combining ed with find may solve this issue, though this makes the command line even more complicated. 8O
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum