editing file in place

Is in Unix a tool that could do this:

paste -d":" 'file1' 'file2' | special_save "file1" # so that file1 would be treated in a way that would prevent problems with race condition

I know it normally doesn't work but is there any intelligent way how to avoid using temporary file? I know methods like heredoc, redirections,... but still it's not too comfortable..

Thank you!

Never heard of such a tool, but what's wrong with:

paste -d":" file1 file2 > temp.file && mv temp.file file1

Regards

You can hide the temp file handling behind some sort of wrapper.

inplace () {
  local tmp dest
  tmp=`mktemp -t inplace.XXXXXX`
  file=$1
  shift
  "$@" >"$tmp"
  mv "$tmp" "$file"
}

inplace file1 paste -d ":" file1 file2

One clever workaround I stumbled on in these forums a few weeks ago used backticks to get the data out of the destination file before overwriting it.

echo "`paste -d ":" file1 file2`" >file1

Franklin52: well, I use this solution often but it seems to me a little weird to create a temporary file and rename it right away. I would expect this line:

paste -d":" 'file1' 'file2' | special_save "file1"  

just more natural..

And if I'm not wrong I need one more disk access with renaming than I would need with the command above.

2era:
> One clever workaround I stumbled on in these forums a few weeks ago used backticks to get the data out of the destination file before overwriting it.

Thank you! I like the solution :slight_smile: but it would not work properly with escape sequences, would it? Chars like "\n" are not treated as escape sequence unless switch -e is enabled - and this switch is not in single unix specification.

I think that:

variable="`paste -d ":" file1 file2`"
printf "%s" $variable >file1

should work always. Am I right?

One more thing racks my brains: How long text may I set to a variable?

You are right that echo is not portably safe, but then printf might not be available on legacy platforms either. If it's available, it's a good solution.

I have a var="`dd if=/dev/zero`" running in another window. So far, it's looking like it's only constrained by available memory. Actually I think I'll kill it now ... 11GB so far, with no particular indication that we would hit the limit any time soon. But this is probably highly platform-dependent.

oh, printf is part of GNU project, I didn't know that.

I was affraid of much lower limits than 11GB ... it's ok then :slight_smile: