0

I am working on a script that involves paths with wildcards. I know the wildcards will match one file only, I just don't know what the file extension will be ahead of time so I am using a wildcard.

The goal here is to find the path to the appropriate file, and then add that path to line 16 of a script.

I have something like this:

path=/path/to/somewhere/fileName*

sed "16 a file=$path" myScript.sh

What I expect to get is this (on line 16):

file=/path/to/somewhere/fileName.extension

But what I get is:

file=/path/to/somewhere/fileName*

For some reason sed is not expanding the wildcard when it adds the contents of $path and I can't figure out how to make sed do such a thing. I'm looking for a solution that either a) has sed properly expand $path or b) a way to get $path to contain the fully expanded string before being passed to sed.

Cole
  • 600
  • 6
  • 12

2 Answers2

1

Your variable contains just a string and you then interpolate that string. sed can't know that's not what you mean. If you want the shell (not sed!) to expand the wildcard, probably use a loop.

for path in /path/to/somewhere/fileName*; do
   if [ -e "$path" ]; then   # handle wildcard possibly not matching
      sed "16 a file=$path" myScript.sh
   fi
done

It's unclear what should happen if the wildcard matches multiple files; perhaps you want to add a break before fi to only substitute the first one if that happens.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • The computer probably doesn't know that, so the code should do the right thing even when it doesn't. – tripleee Nov 05 '20 at 09:07
  • Good catch on what I thought was the solution, which I've now deleted because it wasn't correct. – Johannes Riecken Nov 05 '20 at 09:13
  • A loop is an interesting solution. Is there a simpler way to get bash to expand/interpolate the string? – Cole Nov 05 '20 at 10:29
  • You could put it in an array, for example. – tripleee Nov 05 '20 at 10:56
  • @tripleee : Your suggestion how to fix it, is correct of course, but I think your explanation of the problem catches it only halfway: In bash, the assignement `path=/path/to/somewhere/fileName*` would already undergo pathname expansion. The fact that `file` still has an asterisk, means that there were no matching files. While it is right that this is unrelated to `sed`, the havoc occurs earlier already. – user1934428 Nov 05 '20 at 11:52
  • 1
    @user1934428 No, that's not correct. https://ideone.com/di5VT9 – tripleee Nov 05 '20 at 12:11
  • @tripleee : Uh, you'r right. Silly me - when I tried to recreate it, I forgot quoting the variable when printing it. – user1934428 Nov 05 '20 at 12:16
1

This might work for you (GNU sed, echo and bash):

export path='/path/toSomeWhere/filename*'
sed '16{s/$/\na file=$(echo $path)/;s/.*/echo "&"/e}' file

Export the variable path which has been set to /path/toSomeWhere/filename* (notice the single quotes which prevents interpolation).

On line 16 of file append a line a file=$(echo $path) and then surround both lines by double quotes and prepend the echo command and evaluate (the e flag on the second substitution command) the expression.

Alternative:

sed '17e echo "a file=$(echo /path/toSomeWhere/filename*)"' file
potong
  • 55,640
  • 6
  • 51
  • 83