3

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?

  • 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
2
awk '/{/ {print $2}' FS='"'

Result

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

However this would be better done using jq

1

Solution with sed:

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

grep + cut :

grep '{' | cut -d'"' -f2
0

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.

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.