euglug.org

[Eug-lug] Source Code Processing

작성자

카테고리:



Neil ParkerTue May 3 22:15:23 PDT 2005

Jason Van Cleve wrote,
>I'd like to process a whole bunch of source files uniformly, stripping
>off any whitespace at the ends of lines and also making sure there is
>exactly one newline before the EOF.  That last part may be tricky, but
>is there a speedy *nix utility for getting rid of trailing whitespace,
>or maybe for general source code processing?

Regular Expressions Are Your Friend.

     sed 's/[ ^I]*$//' original_source.c > stripped_source.c

(where "^I" means control-I, i.e. press the TAB key.  If you enter this
directly on the command line [i.e. not in a script], you may need to press
control-V first.)

Or if you like your control characters visible, you can use perl:

     perl -pe 's/[ \011]*$//;' original_source.c > stripped_source.c

Perl will even do a whole batch of files at once for you:

     perl -pi'orig_*' -e 's/[ \011]*$//;' file1.c file2.c file3.c ...

saving the originals as "orig_file1.c", "orig_file2.c", etc.

            - Neil Parker