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.

Can someone explain me what this sed command is trying to achieve?

sed 's/^[[:space:]]*//;s/[[:space:]]*$//

I understand that it is searching for whitespace characters from the beginning of the line, but I am not able to understand the part starting from //;s/[[:space:]]*$//

share|improve this question
    
the second part is it is removing the whitespaces at the end of the line. Overall, trimming the white space characters at the beginning and at the end of the line. –  Nikhil Mulley Feb 20 '12 at 11:40
    
is ;s part of the pattern it is trying to find or it is some keyword? I am confused just coz of this part –  Incognito Feb 20 '12 at 11:42
4  
The ; is to separate two commands; just like in a shell script. It could be written sed -e 's/^[[:space:]]//' -e 's/[[:space:]]$//'. –  Arcege Feb 20 '12 at 11:46
    
Thanks @Arcege , I see that it was ; which is used to separate the two commands. It's clear to me now! –  Incognito Feb 20 '12 at 11:48
    
It's a Trim! –  razpeitia Feb 20 '12 at 14:16

2 Answers 2

up vote 11 down vote accepted

There are two s commands there: sed can take a list of commands seperated by semicolons. You could put the same thing in a script file like so:

s/^[[:space:]]*//
s/[[:space:]]*$//

Also, you're parsing the s command wrongly: s isn't search, it is substitute: it takes two arguments, the string to match and the string to replace it with (s/find/replace/).

So, s/^[[:space:]]*// means

s                (substitute)
/^[[:space:]]*/  (leading whitespace)
//               (with an empty string)

and the second command s/[[:space:]]*$// means substitute trailing whitespace with an empty string.

share|improve this answer

As far as I know it's just a substitute chain. First it removes all leading whitespace via s/^[[:space:]]// and it moves on to apply s/[[:space:]]$// to remove all trailing whitespace.

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.