Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I want to pass the output of a Sed command to a variable in batch. For example, in the file "input.txt" I have name=kiddy bla bla. I want to extract the the word kiddy and save it in a variable (say, name), so I can use it later (with %name%).

share|improve this question
up vote 1 down vote accepted

Sure, you can set a variable with the output of a sed command with a for loop:

for /f %%v in ('sed blah blah...') do (set "name=%%v")

Yes, it's not so intuitive, but it works.
Also, you can change the command to anything you want, as long as it's between apostrophes (').

P.S
To extract the value kiddy from name="kiddy" bla bla, use such sed syntax:

sed "s/""/'/g" | sed "s/.*='\(.[^']*\)'.*/\1/"
share|improve this answer
    
for the extracting thing, this work if kiddy was between "" , like "kiddy" ? – Leo92 Jun 21 '12 at 12:20
    
@Leo92 This works only for "kiddy" or 'kiddy'. Look at the Sed script. – Eitan T Jun 21 '12 at 12:28
    
ok , i am trying to change name with name" using sed, sed "s/name/name"/g" but it giving me error , I tried putting \ before "" error tired with ^ before " again error , I dont know what to do to skip it , – Leo92 Jun 21 '12 at 12:40
    
@Leo92 " is not escaped in batch in the same way it is escaped in Sed. Working with quotation marks (") is not really recommended in batch scripts, because the batch interpreter tries to handle them itself, so you sometimes get unexpected results. Use apostrophes (') in your strings instead. – Eitan T Jun 21 '12 at 13:12

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.