【问题标题】:Extract Text between two strings (Command Line)提取两个字符串之间的文本(命令行)
【发布时间】:2017-08-28 14:16:31
【问题描述】:

我正在尝试从 Spotify API 的 json 文件中提取歌曲名称。 “名称”一词同时出现在艺术家和歌曲中,我正在尝试提取歌曲名称。

我尝试使用 grep,但我不确定如何在所需文本(歌曲名称)之前和之后查找字符串模式。在这种情况下,歌曲名称在“名称”之后,但在“流行度”之前。在艺术家姓名的情况下,名称在“名称”之后和“类型”之前。

如何提取两个单词之间的文本?

{
  "tracks" : [ {
    "album" : {
      "album_type" : "album",
      "artists" : [ {
        "external_urls" : {
          "spotify" : "https://open.spotify.com/artist/13ubrt8QOOCPljQ2FL1Kca"
        },
        "href" : "https://api.spotify.com/v1/artists/13ubrt8QOOCPljQ2FL1Kca",
        "id" : "13ubrt8QOOCPljQ2FL1Kca",
        "name" : "A$AP Rocky",
        "type" : "artist",
        "uri" : "spotify:artist:13ubrt8QOOCPljQ2FL1Kca"
      } ],
      "available_markets" : [ "AD", "AR", "AT", "AU", "BE", "BG", "BO", "BR", "CA", "CH", "CL", "CO", "CR", "CY", "CZ", "DE", "DK", "DO", "EC", "EE", "ES", "FI", "FR", "GB", "GR", "GT", "HK", "HN", "HU", "ID", "IE", "IS", "IT", "JP", "LI", "LT", "LU", "LV", "MC", "MT", "MX", "MY", "NI", "NL", "NO", "NZ", "PA", "PE", "PH", "PL", "PT", "PY", "SE", "SG", "SK", "SV", "TH", "TR", "TW", "US", "UY" ],
      "external_urls" : {
        "spotify" : "https://open.spotify.com/album/1E1eyI5uGllppJZCxNoF9w"
      },
      "href" : "https://api.spotify.com/v1/albums/1E1eyI5uGllppJZCxNoF9w",
      "id" : "1E1eyI5uGllppJZCxNoF9w",
      "images" : [ {
        "height" : 640,
        "url" : "https://i.scdn.co/image/b0dad68115b71ec6c81240eac982da7d14676e6d",
        "width" : 640
      }, {
        "height" : 300,
        "url" : "https://i.scdn.co/image/342fd8bf9219b2369394bc6adf57a69b3fecc0f7",
        "width" : 300
      }, {
        "height" : 64,
        "url" : "https://i.scdn.co/image/79d71b48c2fd180c7506a2db2e13ed0a7c8107a2",
        "width" : 64
      } ],
      "name" : "LONG.LIVE.A$AP (Deluxe Version)",
      "type" : "album",
      "uri" : "spotify:album:1E1eyI5uGllppJZCxNoF9w"
    },
    "artists" : [ {
      "external_urls" : {
        "spotify" : "https://open.spotify.com/artist/13ubrt8QOOCPljQ2FL1Kca"
      },
      "href" : "https://api.spotify.com/v1/artists/13ubrt8QOOCPljQ2FL1Kca",
      "id" : "13ubrt8QOOCPljQ2FL1Kca",
      "name" : "A$AP Rocky",
      "type" : "artist",
      "uri" : "spotify:artist:13ubrt8QOOCPljQ2FL1Kca"
    }, {
      "external_urls" : {
        "spotify" : "https://open.spotify.com/artist/3TVXtAsR1Inumwj472S9r4"
      },
      "href" : "https://api.spotify.com/v1/artists/3TVXtAsR1Inumwj472S9r4",
      "id" : "3TVXtAsR1Inumwj472S9r4",
      "name" : "Drake",
      "type" : "artist",
      "uri" : "spotify:artist:3TVXtAsR1Inumwj472S9r4"
    }, {
      "external_urls" : {
        "spotify" : "https://open.spotify.com/artist/17lzZA2AlOHwCwFALHttmp"
      },
      "href" : "https://api.spotify.com/v1/artists/17lzZA2AlOHwCwFALHttmp",
      "id" : "17lzZA2AlOHwCwFALHttmp",
      "name" : "2 Chainz",
      "type" : "artist",
      "uri" : "spotify:artist:17lzZA2AlOHwCwFALHttmp"
    }, {
      "external_urls" : {
        "spotify" : "https://open.spotify.com/artist/2YZyLoL8N0Wb9xBt1NhZWg"
      },
      "href" : "https://api.spotify.com/v1/artists/2YZyLoL8N0Wb9xBt1NhZWg",
      "id" : "2YZyLoL8N0Wb9xBt1NhZWg",
      "name" : "Kendrick Lamar",
      "type" : "artist",
      "uri" : "spotify:artist:2YZyLoL8N0Wb9xBt1NhZWg"
    } ],
    "available_markets" : [ "AD", "AR", "AT", "AU", "BE", "BG", "BO", "BR", "CA", "CH", "CL", "CO", "CR", "CY", "CZ", "DE", "DK", "DO", "EC", "EE", "ES", "FI", "FR", "GB", "GR", "GT", "HK", "HN", "HU", "ID", "IE", "IS", "IT", "JP", "LI", "LT", "LU", "LV", "MC", "MT", "MX", "MY", "NI", "NL", "NO", "NZ", "PA", "PE", "PH", "PL", "PT", "PY", "SE", "SG", "SK", "SV", "TH", "TR", "TW", "US", "UY" ],
    "disc_number" : 1,
    "duration_ms" : 233786,
    "explicit" : true,
    "external_ids" : {
      "isrc" : "USRC11201220"
    },
    "external_urls" : {
      "spotify" : "https://open.spotify.com/track/4XoP1AkbOurU9CeZ2rMEz2"
    },
    "href" : "https://api.spotify.com/v1/tracks/4XoP1AkbOurU9CeZ2rMEz2",
    "id" : "4XoP1AkbOurU9CeZ2rMEz2",
    "name" : "F**kin' Problems",
    "popularity" : 80,
    "preview_url" : "https://p.scdn.co/mp3-preview/1649b7c26c8bcfa8b252601cf428844cec9a8446?cid=8897482848704f2a8f8d7c79726a70d4",
    "track_number" : 7,
    "type" : "track",
    "uri" : "spotify:track:4XoP1AkbOurU9CeZ2rMEz2"
  }, {

【问题讨论】:

  • 在哪个操作系统上??。

标签: json command-line grep extract spotify


【解决方案1】:

我想通过一些研究,您可以找到很多解决问题的方法。 例如,如果你总是有你引用的结构类型

grep name filename  | cut -c15- | rev | cut -c 3- | rev

会起作用的。您可能会找到更简洁、更强大的解决方案,但在您的情况下这是可行的。

编辑

给定你可以使用的新文件

grep name filename | rev | cut -c 3- | cut -f1 -d'"' | rev

【讨论】:

  • 我已经编辑了代码块,虽然它很长。该模式往往是对于出现的每个“名称”字符串,都会出现艺术家、专辑或歌曲名称。在尝试了您推荐的 grep 之后,所有三种类型的名称都会出现。我发现歌曲名称不同的主要方式是它后面是“流行”。
  • 我已根据您的新文件编辑了答案。试一试。此外,当您在问题中添加代码时,请始终复制您的代码,选择并单击大括号以正确编辑。 (stackoverflow.com/editing-help)
【解决方案2】:

试试 pcregrep

pcregrep -o1 -M 'name" : "(.*)".*\n.*"popularity'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-13
    • 2013-05-14
    • 2016-10-27
    • 2018-11-02
    • 2016-08-02
    • 2023-01-15
    • 1970-01-01
    相关资源
    最近更新 更多