[ILUG] how to clear space at end of line?
Brady, Padraig
Padraig.Brady at compaq.com
Mon Nov 13 12:47:59 GMT 2000
> -----Original Message-----
> From: David Neary [mailto:dneary at informix.com]
> Sent: 13 November 2000 07:37
> To: RenWei
> Cc: ilug at linux.ie
>
> RenWei wrote:
> >
> > hi ,
> > i have some c file in dos format , when i try to compile
> it in linux ,
> > it always reports there's some whitespace after "\", like this:
> >
> > #define pss(x) \
> > if(1) { \
> > grab_stmof(x) \
> > ...... \
> >
> > }
> >
> > it's bothering , so i use
> > cat file |col -b > file.new
> > to clear them , but the whitespaces in file are replace by
> tab, i don't want
> > this.
> >
> > how to clear these whitespace at tail, without change the normal
> > whitespace?
> > is sed usable, or tr? any one can tell me how to do?
>
> sed -e 's/[ ]*$//' filename > new.filename
> will do it. That's a space and a tab, by the way. sed is greedy, so
> it'll take the longest match it can. In vi, you can use :%s/[
> ]*$// to
> do the same thing (where that's a space and tab again).
>
> Cheers,
> Dave.
Yep this is a common problem.
Note it's useful to also add ^M to the char list as this
handles text files in DOS format also. ^M is carriage
return, ascii 10, and how it's entered depends on your editor.
In vi you press Ctrl-V and then return.
sed -e 's/[ ]*[^M]*$//' filename > new.filename
The following will flag files with whitespace @ end of any line
(or a line containing just whitespace)
grep -El "[ ]+[^M]*$" *.c
To flag just files with space after \ then it would be:
grep -El "\\[ ]+[^M]*$" *.c
Also you can do this in visual studio *spit* if you
have it by doing a regular expression search and replace.
Note to type the tab character you must copy one to the
clipboard and paste it in. Of course being in doze means
you can't extend this functionality to multiple files,
so you must open each file and repeat the process.
Following is a useful general script for applying
edits or any filter to multiple files. So to use
it in your case you would do:
modify -f "sed -e 's/[ ]*[^M]*$//'" *.c
Padraig.
##########################################################################
# Shellscript: modify - modify file using arbitrary command
# Author : Heiner Steven (heiner.steven at odn.de)
# Date : 1994-04-26
# Category : File Utilities, Text Utilities
# $Id: modify,v 1.2 2000/02/06 19:55:36 heiner Exp $
##########################################################################
PN=`basename "$0"` # Program name
VER=`echo '$Revision: 1.2 $' | cut -d' ' -f2`
Usage () {
echo "$PN - modify files using arbitrary command, $VER (stv '94)
usage: $PN [-f] command [file ...]
-f: force execution, no interactive prompts
This program basically does the following:
(command) < file > tmp && mv tmp file
In other words: the file will be replaced with the standard
output of the command." >&2
exit 1
}
Tmp=${TMPDIR:=/tmp}/mo$$
interactive=yes # Ask for confirmation?
trap "rm -f $Tmp" 0
trap "exit 2" 1 2 3 15
if [ $# -gt 0 ] && [ "$1" = "-f" ]
then
interactive=no
shift
fi
[ $# -lt 1 ] && Usage
Command="$1"
shift
for i
do
if [ $interactive = yes ]
then
echo "replace $i with \`($Command) < $i\` (y/n, ^D)? y\c"
read OK || { echo; exit 1; }
case "$OK" in
[nN]*) continue ;;
esac
fi
cp -p "$i" "$Tmp" || exit
eval "$Command" < "$i" > "$Tmp" && mv "$Tmp" "$i"
done
More information about the ILUG
mailing list