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.

why n option is not working here

   [root@testgfs2 final]# echo a b c | sed -ne 's/[[:blank:]]/"/gp' -ne 's/^/"/pg' -ne 's/$/"/pg'
    a"b"c
    "a"b"c
    "a"b"c"

i just need :

"a"b"c"

actually i want sed to do somehting like :

echo a b c d | sed ...

output:

'a' 'b' 'c' 'd'
share|improve this question
add comment

1 Answer 1

up vote 3 down vote accepted

The problem is you are using p as a modifier which prints the "pattern space", i.e. result. To fix your example just do:

echo a b c | sed -e 's/[[:blank:]]/"/g' -e 's/^/"/g' -e 's/$/"/g

You may also consider using something like:

echo a b c | sed -e 's/\(\w*\)/"\1"/g'

instead.

share|improve this answer
1  
+1. Or the shorter ERE version: sed -r "s/\w+/'&'/g". –  Thor Feb 27 '13 at 10:31
    
what does \w* stands for or does –  munish Feb 27 '13 at 10:32
    
@munish: \w is a GNU sed extension and is equivalent to this group [[:alnum:]_]. What version of sed are you using? –  Thor Feb 27 '13 at 11:09
1  
Which means sed "s/[^[:blank:]]\{1,\}/'&'/g" would be standard, more correct (as you want non-blanks, not just alnums) and more portable. –  Stéphane Chazelas Feb 27 '13 at 21:09
add comment

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.