Steve
Steve

Reputation: 37

I get a blank echo from regex

If I run this file, it works fine and outputs the lines I expect:

workspaceFile=`cat tensorflow/workspace.bzl`
echo $workspaceFile | grep -oP '\/[a-z0-9]{12}.tar.gz'

However, if I run this, all I get is blank output in the terminal:

workspaceFile=`cat tensorflow/workspace.bzl`
TAR_FILE_WITH_SLASH=$workspaceFile | grep -oP '\/[a-z0-9]{12}.tar.gz'
echo $TAR_FILE_WITH_SLASH

The file is quite long so I'll add a shortened version here for simplicity's sake:

tf_http_archive(
  name = "eigen_archive",
  urls = [
    "https://mirror.bazel.build/bitbucket.org/eigen/eigen/get/6913f0cf7d06.tar.gz",
    "https://bitbucket.org/eigen/eigen/get/6913f0cf7d06.tar.gz",
  ],

Upvotes: 2

Views: 55

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626927

You need to use $() syntax, echo the contents of workspaceFile and then pipe the grep command:

TAR_FILE_WITH_SLASH="$(echo $workspaceFile | grep -oE '/[a-z0-9]{12}\.tar\.gz')"

Also, note you need no PCRE regex here, you can use a POSIX ERE regex (that is, replace P with E). You may even use a POSIX BRE pattern here, like grep -o '/[a-z0-9]\{12\}\.tar\.gz'. The dot must be escaped to match a literal dot and the / is not special here and needs no escaping.

See the online demo.

Upvotes: 1

John Goofy
John Goofy

Reputation: 1419

What's about the path?

workspaceFile=`cat ~/tensorflow/workspace.bzl`

Upvotes: 0

Related Questions