Skip to main content
when creating a new file, don't affect the old file until the new version is ready
Source Link
Gilles 'SO- stop being evil'
  • 865.9k
  • 205
  • 1.8k
  • 2.3k

You can't. Either use ed or GNU sed or perl, or do what they do behind the scenes, which is to create a new file for the contents.

ed, portable:

ed foo <<EOF 1,$s/^\([^,]*\),\([^,]*\),\([^,]*\).*/\1,\3/ w q EOF 

GNU sed:

sed -i -e 's/^\([^,]*\),\([^,]*\),\([^,]*\).*/\1,\3/' foo 

Perl:

perl -i -l -F, -pae 'print @F[1,3]' foo 

cut, creating a new file (recommended, because if your script is interrupted, you can just run it again):

mv foo foo.old cut -d , -f 1,3 <foo.old >foo.new && rmmv -f foo.oldnew foo 

cut, replacing the file in place (retains the ownership and permissions of foo, but needs protection against interruptions):

cp -f foo foo.old && cut -d , -f 1,3 <foo.old >foo && rm foo.old 

I recommend using one of the cut-based methods. That way you don't depend on any non-standard tool, you can use the best tool for the job, and you control the behavior on interrupt.

You can't. Either use ed or GNU sed or perl, or do what they do behind the scenes, which is to create a new file for the contents.

ed, portable:

ed foo <<EOF 1,$s/^\([^,]*\),\([^,]*\),\([^,]*\).*/\1,\3/ w q EOF 

GNU sed:

sed -i -e 's/^\([^,]*\),\([^,]*\),\([^,]*\).*/\1,\3/' foo 

Perl:

perl -i -l -F, -pae 'print @F[1,3]' foo 

cut, creating a new file (recommended, because if your script is interrupted, you can just run it again):

mv foo foo.old cut -d , -f 1,3 <foo.old >foo && rm foo.old 

cut, replacing the file in place (retains the ownership and permissions of foo, but needs protection against interruptions):

cp foo foo.old && cut -d , -f 1,3 <foo.old >foo && rm foo.old 

I recommend using one of the cut-based methods. That way you don't depend on any non-standard tool, you can use the best tool for the job, and you control the behavior on interrupt.

You can't. Either use ed or GNU sed or perl, or do what they do behind the scenes, which is to create a new file for the contents.

ed, portable:

ed foo <<EOF 1,$s/^\([^,]*\),\([^,]*\),\([^,]*\).*/\1,\3/ w q EOF 

GNU sed:

sed -i -e 's/^\([^,]*\),\([^,]*\),\([^,]*\).*/\1,\3/' foo 

Perl:

perl -i -l -F, -pae 'print @F[1,3]' foo 

cut, creating a new file (recommended, because if your script is interrupted, you can just run it again):

cut -d , -f 1,3 <foo >foo.new && mv -f foo.new foo 

cut, replacing the file in place (retains the ownership and permissions of foo, but needs protection against interruptions):

cp -f foo foo.old && cut -d , -f 1,3 <foo.old >foo && rm foo.old 

I recommend using one of the cut-based methods. That way you don't depend on any non-standard tool, you can use the best tool for the job, and you control the behavior on interrupt.

Source Link
Gilles 'SO- stop being evil'
  • 865.9k
  • 205
  • 1.8k
  • 2.3k

You can't. Either use ed or GNU sed or perl, or do what they do behind the scenes, which is to create a new file for the contents.

ed, portable:

ed foo <<EOF 1,$s/^\([^,]*\),\([^,]*\),\([^,]*\).*/\1,\3/ w q EOF 

GNU sed:

sed -i -e 's/^\([^,]*\),\([^,]*\),\([^,]*\).*/\1,\3/' foo 

Perl:

perl -i -l -F, -pae 'print @F[1,3]' foo 

cut, creating a new file (recommended, because if your script is interrupted, you can just run it again):

mv foo foo.old cut -d , -f 1,3 <foo.old >foo && rm foo.old 

cut, replacing the file in place (retains the ownership and permissions of foo, but needs protection against interruptions):

cp foo foo.old && cut -d , -f 1,3 <foo.old >foo && rm foo.old 

I recommend using one of the cut-based methods. That way you don't depend on any non-standard tool, you can use the best tool for the job, and you control the behavior on interrupt.