0

I'd like to extract text between two strings in a text file that contains Spotify chart results

Text file extract:

{"tracks":[{"date":"2014-12-14","country":"TW","track_url":"https:\/\/play.spotify.com\/track\/34gCuhDGsG4bRPIf9bb02f","track_name":"Thinking Out Loud","artist_name":"Ed Sheeran","artist_url":"https:\/\/play.spotify.com\/artist\/6eUKZXaKkcviH0Ku9w2n3V","album_name":"x","album_url":"https:\/\/play.spotify.com\/album\/1xn54DMo2qIqBuMqHtUsFd","artwork_url":"http:\/\/o.scdn.co\/300\/646e9619750dfa3d1eadbbea959dc6f528a9109e","num_streams":51672,"window_type":"weekly","percent_male":58,"percent_age_group_0_17":4,"percent_age_group_18_24":45,"percent_age_group_25_29":25,"percent_age_group_30_34":12,"percent_age_group_35_44":7,"percent_age_group_45_54":1,"percent_age_group_55_plus":6},

The text I want to extract is the track name. Desired output from the above text file:

1 Thinking Out Loud
2 xxx
3 xxx

To achieve this, I would like to use the sed and nl commands to extract text between the strings

"track_name":"       and
   ",

... and output the results to another text file

closed as unclear what you're asking by jimmij, slm Dec 16 '14 at 20:18

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    At a minimum, please provide example input and what you have tried... – jasonwryan Dec 16 '14 at 17:53
  • Thanks, have updated the request with example input and desired output – wyt18 Dec 17 '14 at 0:24
  • See: unix.stackexchange.com/a/137704/6761 – jasonwryan Dec 17 '14 at 0:31
  • Thanks @jasonwryan for the edits. could you also remove the [on hold] on the title? – wyt18 Dec 17 '14 at 0:32
  • 1
    You want a json parser for this, eg.: jshon -e 0 -e track_name -u <file produces: Thinking Out Loud – jasonwryan Dec 17 '14 at 0:49
1

If you have GNU grep, you can use a perl-compatible regular expressions. This is handy due to the look-around assertions:

grep -oP '(?<=track_name).*?(?=,)' filename
1

This is an example regex that matches your provided input and gives you the output you desire. Using this method, we're causing sed to do a search+replace (s) on each line, replacing the whole line with just the part in the middle, if the line contains track_name and a comma. We then only print matched lines (p).

[me:~]$ cat work/tmp/example.txt 
{"tracks":[{"date":"2014-12-14","country":"TW","track_url":"https:\/\/play.spotify.com\/track\/34gCuhDGsG4bRPIf9bb02f","track_name":"Thinking Out Loud","artist_name":"Ed Sheeran","artist_url":"https:\/\/play.spotify.com\/artist\/6eUKZXaKkcviH0Ku9w2n3V","album_name":"x","album_url":"https:\/\/play.spotify.com\/album\/1xn54DMo2qIqBuMqHtUsFd","artwork_url":"http:\/\/o.scdn.co\/300\/646e9619750dfa3d1eadbbea959dc6f528a9109e","num_streams":51672,"window_type":"weekly","percent_male":58,"percent_age_group_0_17":4,"percent_age_group_18_24":45,"percent_age_group_25_29":25,"percent_age_group_30_34":12,"percent_age_group_35_44":7,"percent_age_group_45_54":1,"percent_age_group_55_plus":6},
[me:~]$ sed -n 's/.*track_name":"\(.*\)","artist_name.*/\1/p' work/tmp/example.txt | nl
     1  Thinking Out Loud

I've omitted redirecting that output to a file to display as an example. Append a stdout redirect, using > file.txt to write to a file.

That said, this depends on "track_name" being right before "artist_name", and neither of those strings being anywhere else on the line. This works with GNU Sed.

Ultimately, this is probably not the right tool for the job, as you've got explicitly formatted (json) text, so using something that can parse json would be more reliable. Different versions of sed may support different extensions to regex, so results can vary across platforms.

  • Thank you ... I tried this (running a shell command on Android) but it didn't work for me. The command runs, the output file is created, but it is empty. I also updated the question based on the moderator's suggestion. Any ideas as to what I'm doing wrong? – wyt18 Dec 17 '14 at 0:26
  • Updated using new example, and added a comment about sed on different platforms. My example I'm not familiar with the software available on your android, and I don't know if it varies from android to android. Run "sed --version" to see what version. I tested this on GNU sed version 4.2.1 – rjewell Dec 17 '14 at 17:00
  • It's also possible that you're just typoing it. regex is hard to type. :) – rjewell Dec 17 '14 at 17:01
  • thank you - I went with Json instead and found a solution: var spotify = JSON.parse(global("HTTPD")); for (var i = 1; i < 11; i++) { setGlobal('Spotify'+i, spotify.tracks[i-1].track_name + ' ' + spotify.tracks[i-1].artist_name); } – wyt18 Dec 17 '14 at 19:04

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