Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

The answer to removing trailing whitespace with sed has most of the answer, but I want

sed -i 's/[ \t]*$//' $1

to be able to take arbitrary number of file arguments as a shell script on the command line, including glob arguments. I.e. suppose the script is called strip_trailing_whitespace. Then I'd like to be able to do both

strip_trailing_whitespace foo.cc bar.cc

and

strip_trailing_whitespace *.cc *.hh

to strip trailing whitespaces from all files of the form *.cc and *.hh. Arguments not based on the answer quoted above are also fine.

share|improve this question

2 Answers 2

up vote 8 down vote accepted

$1 is a positional parameter; it will expand to the first argument passed to the script. There are similarly $2, $3...$9, ${10}, ${11},...

The special parameter "$@" will expand to a list of all the positional parameters.

So you can do the following:

sed -i 's/[ \t]*$//' "$@"

If you want to pass a glob/pattern to this script (or to any program), it must be escaped or quoted when you call the script - this is a function of the shell; it will expand any patterns before your script even sees it. This case shouldn't need that - the shell can expand the pattern, and the results of that expansion all get passed to sed.

share|improve this answer
    
Thanks, Shawn. That works. –  Faheem Mitha Oct 26 '11 at 22:08
    
Exactly the same way: echo "$@". Using the parameter doesn't change it in any way, so you can reference $@ (or any other parameter: positional, special or not) as many times as you want. –  Shawn J. Goff Oct 26 '11 at 22:09
    
Yes, I realised that after I posted, but was apparently too slow to remove the question. :-) –  Faheem Mitha Oct 26 '11 at 22:17
    
keep in mind -i.bak, too ... not to mention that sometimes I come across some variant of sed without -i :) ... +1, still. –  0xC0000022L Feb 22 '13 at 20:26
    
This doesn't work with the version of sed that ships with OS X. Instead, install gnu-sed (brew install gnu-sed) and replace sed with gsed in the command. –  Robert Audi Apr 2 at 12:34

I find it easy to just use sed with xargs as follows:

find . -name "*.xml" | xargs sed -i 's/[ \t]*$//'

find . -type f | grep cc | xargs sed -i 's/[ \t]*$//'

share|improve this answer

Your Answer

 
discard

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.