Extract the string between Quotes of particular occurrence in unix

24,440

Solution 1

It's unclear exactly what you're trying to do. For one thing, your grep command includes a curly brace and the input doesn't. Also, it appears that you want to make a substitution based on a comparison of your input and output.

However, taking your question literally, here's how you can grep the strings between the quotes. You can use non-greedy matching:

grep -Po '".*?"'

Example:

$ echo 'set name "username"; # comment "should be updated"' | grep -Po '".*?"'
"username"
"should be updated"

Edit:

In order to substitute a value, you can use sed. You would not use grep.

sed 's/"[^"]*"/"new name"/'

Example:

$ echo 'set name "old name"; # comment "should be updated"' | sed 's/"[^"]*"/"new name"/'
set name "new name"; # comment "should be updated"

Solution 2

Sed:

sed 's/[^"]*"\([^"]*\)".*/\1/'

Awk:

awk -F'"' '{ print $2 }'

Solution 3

I assume the word after should be is supposed to be the new username

sed 's/^\([^"]\+"\)[^"]\+\(.*should be \)\([^"]\+\)/\1\3\2\3/' 

a little tidier with GNU sed

sed -r 's/^([^"]+")[^"]+(.*should be )([^"]+)/\1\3\2\3/'

Edit - Dennis has a good answer. If the new name is held in a shell variable, you can use a quoting trick:

new_name="Fred"
sed 's/"[^"]*/"'"$new_name"'"/'
Share:
24,440
user1228191
Author by

user1228191

Updated on April 19, 2020

Comments

  • user1228191
    user1228191 about 4 years

    Input file

    ..
    set name "old name"; # comment "should be updated"
    ..
    

    Output file

    ..
    set name "new name" ; #comment "should be updated"
    ..
    

    when i tried to grep the content between quotes with grep -i 'name' inputfile | grep -P \".+{\"} its grepping content between first " and last "

    i.e old name"; # comment "should be updated

    any idea to accomplish that using grep, sed or awk!

  • user1228191
    user1228191 about 12 years
    Thanks so much , can we re-direct that grepped output for replacement, sorry i'm a newbie, just learning things!
  • user1228191
    user1228191 about 12 years
    sorry, its my fault, i didn't mean that..new username can be anything.. thanks for answering
  • user1228191
    user1228191 about 12 years
    what i mean is if we pipe the output of grep to sed/awk, with what varaible we can access that!
  • user1228191
    user1228191 about 12 years
    if we pipe the output of grep to sed/awk, with what varaible we can access that!
  • user1228191
    user1228191 about 12 years
    Thanks, its helpful, now updated my question, hope it was clear now, if we pipe the output of grep to sed/awk, with what varaible we can access that! so that we can replace the output of grep with other string and write back
  • glenn jackman
    glenn jackman about 12 years
    @user1228191, note the technique used to achieve non-greedy matching: a quote, then as many non-quote characters as possible, then another quote ("[^"]*")
  • SourceSeeker
    SourceSeeker about 12 years
    You could instead use double quotes for the outer set: sed "s/\"[^\"]*\"/\"$new_name\"/". However, in replacing the hairy quoting, you're left with lots of hairy escaping and you'd have to be careful to escape some instances of $. You could use octal escapes (\o42 - letter "o" believe it or not) for versions of sed that support it, but in this case I don't think it's any better. By the way, you're missing a double quote before the second slash.