View previous topic :: View next topic |
Author |
Message |
TheSmallOne Guru
Joined: 22 Jan 2005 Posts: 467 Location: Germany
|
Posted: Sun Mar 23, 2014 8:20 am Post subject: sed alternative with hardlink support? |
|
|
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 |
|
|
Akkara Bodhisattva
Joined: 28 Mar 2006 Posts: 6702 Location: &akkara
|
Posted: Sun Mar 23, 2014 8:53 am Post subject: |
|
|
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 |
|
|
aCOSwt Bodhisattva
Joined: 19 Oct 2007 Posts: 2537 Location: Hilbert space
|
Posted: Sun Mar 23, 2014 8:54 am Post subject: Re: sed alternative with hardlink support? |
|
|
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 |
|
|
Hu Moderator
Joined: 06 Mar 2007 Posts: 21633
|
Posted: Sun Mar 23, 2014 3:46 pm Post subject: |
|
|
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 |
|
|
steveL Watchman
Joined: 13 Sep 2006 Posts: 5153 Location: The Peanut Gallery
|
Posted: Mon Mar 24, 2014 7:04 am Post subject: |
|
|
Hooray for ed! :-) #ed is open to you on IRC: chat.freenode.net should you wish to learn more. |
|
Back to top |
|
|
TheSmallOne Guru
Joined: 22 Jan 2005 Posts: 467 Location: Germany
|
Posted: Mon Mar 24, 2014 11:42 am Post subject: Re: sed alternative with hardlink support? |
|
|
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. |
|
Back to top |
|
|
|