I am using cygwin and have a lot of files in which I want to convert tabs to spaces (four spaces per tab) and remove all space characters that exist at end of line. Is there a utility for this in Linux?
1 Answer
sed is your friend for that. Comes for free with all Linux distros, as far as I know. It's the "Stream EDitor", and works with regular expressions. To apply it to your example:
sed -E 's/\t/ /g;s/\s*($)/\1/g' infile outfile
Each tab (\t
) is replaced by 4 spaces, and trailing spaces (actually, all whitespace characters up to the end of the line ($
) are removed. If you don't want the result in a new file, you can tell sed
to do the replacements inline (-i
):
sed -i -E 's/\t/ /g;s/\s*($)/\1/g' file
In a lot of files? No prob:
sed -i -E 's/\t/ /g;s/\s*($)/\1/g' *
(just pass a "mask", like *.txt
or whatever files should be matched).
You can get sed
from your distro's package management, e.g. apt install sed
.
-
What language is 's/\t/ /g;s/\s*($)/\1/g'? Is that a regular expression? I don't see the digit 4 anywhere in the regular expression. What is going on here? Jun 24, 2021 at 15:02
-
Yes, it's regular expression. And no, the question wasn't asking for digits – but the replacement part has 4 spaces as requested. Just try it out with some sample files (I did, and it does what you asked for). The trailing
/g
makes sure it's applied Globally, so all tabs are replaced by 4 spaces each.– Izzy ♦Jun 25, 2021 at 7:20 -
I am just curious to know, how did you specify the "4 spaces" part? Jun 25, 2021 at 10:42
-
By using 4 spaces? The big gap following the
\t/
are 4 spaces.sed
syntax iss/<RegExToMatch>/<replacement>/<op>
, basically meanings
earch for this<RegEx>
and replace it according to<replacement>
applying<op>
. You can concatenate multiple operations using a;
. So taking the first part, it meanss
earch for a\t
ab, replace it by 4 spaces,g
lobally. The second part iss
earch for white-\s
paces up to the EOL ($
) and replace that with the EOL (back reference:\1
is the first(match)
).– Izzy ♦Jun 25, 2021 at 21:13
sed