-5

I created file called Test1.txt and I add a line.

The file look like this,

TEST = admin

Now I want to add double quotes for TEST="admin".

How to add double quotes using sed command?

I want Test1.txt look like this below

TEST = "admin"
  • 6
    This should be rather elementary. Did you attempt anything? – devnull Mar 18 '14 at 13:40
  • 6
    How did a perfectly fine question end-up receiving that many downvotes? – Stéphane Chazelas Mar 16 '15 at 10:36
3

With GNU sed and assuming POSIXLY_CORRECT is not in the environment:

sed 's/\badmin\b/"admin"/' -i filename

where -i means "inplace". The \b is there for safety (to only match the whole word, but not "administrator").

  • Oops, missing slash. – orion Mar 18 '14 at 13:48
  • where did you missed. sir – Beginner Mar 18 '14 at 13:49
  • Fixed it already. The syntax is 's/regularexpression/replacement/'. Check out gnu.org/software/sed/manual/html_node/Regular-Expressions.html to learn regular expressions (they are used everywhere, not just in sed). – orion Mar 18 '14 at 13:51
  • 1
    This only works for the one hard coded value in the question. Presumably if they were only trying to fix one instance they could either fix it at the source or do virtually any other search and replace function. This does not provide a generic solution that is useful for anybody except maybe the original poster. – Caleb Jun 21 '14 at 16:14
3
sed 's/\(=[[:blank:]]*\)\(.*\)/\1"\2"/'

Would add quotes around what's on the right of an equal sign followed by any number of blanks.

  • In sed, printable whitespace can be matched with \s which is slighlty more practical. – orion Mar 18 '14 at 14:52
  • 1
    @orion, that's only in GNU sed, and \s also includes vertical space characters which you don't want here (though it's unlikely to harm). [:blank:] is POSIX and portable. – Stéphane Chazelas Mar 18 '14 at 15:05
  • Sorry. As a GNU+linux user, I always silently assume GNU without thinking. It probably works for most people here. – orion Mar 18 '14 at 15:09
0

Without fiddling with regular expressions and sed:

$ echo 'TEST = "admin"' >Test1.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.