There are lots of ways to do this. There are also lots of ways to shoot yourself in the foot while doing this if you're not careful or if you're new to Linux as you appear to be. Assuming that you can create a list of files that you want to convert, either by using something like find
or manually with an editor, just pipe that list into the following.
while read file
do
expand "$file" > /tmp/expandtmp
mv /tmp/expandtmp "$file"
done
One way you can shoot yourself in the foot with that is to make a typo so that you wind up mv'ing an empty file to all of the file names you specify, thereby deleting the contents of all your files. So be careful and test whatever you do first on a small set of files that you have backed up.
That will replace each tab by a single space. Since person mentioned using
expand
, I assume s/he wants the alignment of the text preserved. – garyjohn – 2010-07-02T05:27:28.953You need to have
's/\t/ /g'
to replace more than just one tab per line. – Daniel Andersson – 2012-03-28T20:49:21.0131A substantial speedup if there are many files is doing "
find ./ -type f -exec sed -i ’s/\t/ /g’ {} +
" (that is, "+
" instead of "\;
"), if thefind
version supports it (and I haven't personally met any version that doesn't, but it's not a POSIX standard, so I guess it could happen on some systems. See "-exec command {} +
" in the manual). Instead of launching one instance ofsed
for every file, this will build an argument list with as many file name arguments as the system supports (getconf ARG_MAX
=2097152 on my system), just likexargs
, and thus launch much fewersed
processes. – Daniel Andersson – 2012-03-29T06:43:13.6736Note to any Mac users who find this: OS X's version of
sed
doesn't understand the\t
tab escape sequence. You can replace it with a literal tab character, which you can enter in the shell by[Ctrl]+V, [Tab]
. – Jeremy Banks – 2012-12-17T20:44:55.710expand
is probably better thansed
for this, as explained in: http://stackoverflow.com/a/11094620/131824 – David Weinraub – 2013-11-16T16:09:22.117