0

I have a directory that looks like so :

/path/to/files/data-file.1
/path/to/files/data-file.2
/path/to/files/data-file.3
/path/to/files/data-file.4
/path/to/files/data-file.5
/path/to/files/data-file.6

I'd like to make a script to list all the files between two values in variables like the following :

#!/bin/bash
fileA="/path/to/files/data-file.2"
fileB="/path/to/files/data-file.5"
ls -v /path/to/files/*-file.[0-9]* | sed -n '/$fileA/,/$fileB/p'

with the output :

/path/to/files/data-file.2
/path/to/files/data-file.3
/path/to/files/data-file.4
/path/to/files/data-file.5

However, the sed command does not seem to work. I'm guessing because of the slashes in the variables. Is there a way to overcome this without changing the variables ? I'd like to use this bit of code in a script where fileA and fileB are dynamic.

1 Answer 1

1

You can use a different delimiter if you escape it at its first occurrence, so the slashes will not confuse sed. However, note that using single quotes will result in bash not substituting variables, so in your example, sed looks for $fileA and $fileB literally, as the variables does not get expanded. You need to use double quotes if you use variables.

So, something like this should work:

#!/bin/bash
fileA="/path/to/files/data-file.2"
fileB="/path/to/files/data-file.5"
ls -v /path/to/files/*-file.[0-9]* | sed -n "\@$fileA@,\@$fileB@p'
1
  • I've tried so many different syntaxes but I missed the part where you need to escape the first occurence of the delimiter ! Thank you it works like a charm !
    – Ror
    Sep 22 at 14:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .