Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have output which looks like this:

"vancouver/news/vancouver-voices": {
    "status": 0,
    "time": 0,
    "timestamp": 1371298701
},
"vancouver/news/vancouvers-march-second-annual-success": {
    "status": 0,
    "time": 0,
    "timestamp": 1371298701
}

I need to extract just the partial URLs (vancouver/news/vancouvers-march-second-annual-success and vancouver/news/vancouver-voices for example)

I'm basically calling a command via bash and then piping it to grep awk or sed

If I use grep (| grep -o '".*": {') I can get close. I end up with output like:

"vancouver/news/vancouver-voices": {
"vancouver/news/vancouvers-march-second-annual-success": {

I suspect I need to use sed or awk though. I've tried several things but I think I need to escape some of the special characters. For instance I tried | awk '\"(.*)\": {' --output='$1' but it barfs because of the backslashes.

Can any sed or awk guru out there clue me in on how I properly escape the special characters?

share|improve this question
    
This seems to do the trick although I have to imagine there is a more elegant way of doing this: | grep -o '".*": {' | sed 's/(^"\|": {$)//g'. If anyone has any better suggestions please let me know. –  Brad Jun 15 '13 at 12:33
add comment

3 Answers 3

up vote 2 down vote accepted
awk '/{/ {print $2}' FS='"'

Result

vancouver/news/vancouver-voices
vancouver/news/vancouvers-march-second-annual-success

However this would be better done using jq

share|improve this answer
    
Thanks for the help - that's much simpler! –  Brad Jun 15 '13 at 12:47
add comment

Solution with sed:

sed -n '/{/s="\([^"]*\)".*=\1=p'

grep + cut :

grep '{' | cut -d'"' -f2
share|improve this answer
add comment

You can still use grep:

| grep -o '[^"/]\+\(/[^"/]\+\)\+'

yields:

vancouver/news/vancouver-voices
vancouver/news/vancouvers-march-second-annual-success

It searches for an initial string of non-", non-/ characters, followed by one or more sequences consisting of a slash followed by the same type of string as the initial. One could also use an alphanumeric-plus-dash character class to select the URL components.

share|improve this answer
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.