Is it possible to do it in one liner?

I have an output like this:

"First line" - Description  
" Second line" - Description  
"Third line" - Description  
" Fourth line" - Description  

This input is generated automatically.

I want to replace the first occurrence of " (quotation mark + space) with " (quotation mark) for each line. If I apply the substitution globally, it will also change every occurrence of line" - to line"-, so I was wondering if it is possible to use a sed one liner to accomplish this.

I have tried using ^ like this

sed -r ':a;N;$!ba;s/(\^\" )/\"/g'

But it's not working, it doesn't replace anything. I tried

sed -r ':a;N;$!ba;s/(^|\" )/\"/g'

and it replaces all the occurrences. I've just started to use sed, so I don't really know if I'm doing something wrong.

What am I missing here?

  • A note about labels: The command you wrote above may work in some implementations of sed, but for portability and to avoid really weird errors, you should end a given command argument after a label (if you need them at all). E.g., sed -e ':a' -e 'N;$!ba' -e 'whatever'
    – Wildcard
    Commented Apr 11, 2016 at 20:47

3 Answers 3


You're overthinking it. sed replaces only the first instance on a line by default (without the /g modifier), although you still want to anchor because you don;t so much want the first instance in the line as the one at the start of the line; and you usually don't need the explicit line actions you're trying to use (why?).

sed 's/^" /"/'
  • Thanks! I thought it was necessary to explicitly declare the line actions when working on a file, read about it and just kept on using them. I really was over-thinking it, that couldn't be simpler and it does the trick :)
    – Steve B.
    Commented Apr 9, 2012 at 3:33
  • I'm tempted to say those commands are pretty much obsolete; it is possible to do quite complex multiline things with them, but it's not easy to do so and these days you're really better off using perl/python/ruby/whatever instead.
    – geekosaur
    Commented Apr 9, 2012 at 3:40
  • 2
    Completely disagree with the above comment; multi-line sed commands such as N and labels such as :a are not obsolete at all and they are portable and POSIX specified, unlike Perl/Python/Ruby/whatever. It is true however that if you find yourself needing them often, you should learn awk.
    – Wildcard
    Commented Apr 11, 2016 at 19:16

A more general answer, since I can't comment on geekosaur's answer - You wouldn't put the ^ (start of line anchor). For example, if you want to replace the first occurence of A with B, you would have

sed 's/A/B/'

Using Perl

~$ perl -pe 's/^" /"/;'   file 

Using Raku (formerly known as Perl_6)

~$ raku -pe 's/^\" \s /"/;'  file

Sample Input:

"First line" - Description  
" Second line" - Description  
"Third line" - Description  
" Fourth line" - Description  

Sample Output (both code examples):

"First line" - Description  
"Second line" - Description  
"Third line" - Description  
"Fourth line" - Description  


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .