Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The man page doesn't give me much hope, but I'm hoping it's an undocumented (and/or GNU-specific) feature.

share|improve this question
up vote 11 down vote accepted

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

GNU sed:

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


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.

share|improve this answer

The moreutils package from ubuntu (and also debian) has a program called sponge, which sort-of also solves your problem.

From man sponge:

sponge reads standard input and writes it out to the specified file. Unlike a shell redirect, sponge soaks up all its input before opening the output file. This allows constricting pipelines that read from and write to the same file.

Which would let you do something like:

cut -d <delim> -f <fields> somefile | sponge somefile
share|improve this answer
Really cool utility. :) – balki Jan 14 '11 at 17:43

I don't think that is possible using cut alone. I couldn't find it in the man or info page. You can do something such as

mytemp=$(mktemp) && cut -d" " -f1 file > $mytemp && mv $mytemp file

mktemp makes you a relatively safe temporary file that you can pipe the cut output into.

share|improve this answer

Try vim-way:

$ ex -s +'%!cut -c 1-10' -cxa file.txt

This will edit the file in-place (so do the backup first).

Alternatively use grep, sed or gawk.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.