Search code examples
bashshellsedzsh

Sed is is very confused about the file path I am giving it


I am trying to pass a file with sed substitutions to sed, and it ignoring parts of the path I give it:

First, let's start with a simple assertion:

 test -f workdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/templates.sed && echo "exists"
exists

Yup, file exists. Now, the problem:

sed -E -n -f  /Users/cbongior/dev/omc/workdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/templates.sed /Users/cbongior/dev/omc/workdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/data.psv  
sed: orkdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/templates.sed: No such file or directory

It's dropping everything before the o in workdir!

I have tried a fresh shell, with no extensions

env -i /opt/brew/bin/bash
bash-5.2$ sed -n -E -f "/Users/cbongior/dev/omc/workdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/templates.sed"  "/Users/cbongior/dev/omc/workdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/data.psv"                                                                                                                                                                                                                                                                
sed: orkdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/templates.sed: No such file or directory

I have tried an older version of bash:

env -i bash
The default interactive shell is now zsh.
To update your account to use zsh, please run `chsh -s /bin/zsh`.
For more details, please visit https://support.apple.com/kb/HT208050.
bash-3.2$ sed -n -E -f "/Users/cbongior/dev/omc/workdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/templates.sed"  "/Users/cbongior/dev/omc/workdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/data.psv"
sed: orkdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/templates.sed: No such file or directory

I have tried it with zsh

env -i zsh
sed -E -n -f /Users/cbongior/dev/omc/workdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/templates.sed /Users/cbongior/dev/omc/workdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/data.psv                                                                                                                                                   
sed: orkdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/templates.sed: No such file or directory

I have also tried gsed

gsed -n -E -f "/Users/cbongior/dev/omc/workdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/templates.sed"  "/Users/cbongior/dev/omc/workdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/data.psv" 
gsed: couldn't open file orkdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/templates.sed: No such file or directory

I cd directly into the file of the file and remove all pathing:

bash-5.2$  cd $(dirname "/Users/cbongior/dev/omc/workdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/templates.sed" )     
bash-5.2$ pwd
/Users/cbongior/dev/omc/workdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58
bash-5.2$ ls templates.sed
templates.sed
bash-5.2$ ls -al templates.sed
-rw-r--r--  1 cbongior  staff  62087 Aug  7 15:51 templates.sed
bash-5.2$ sed -E -n -f templates.sed /Users/cbongior/dev/omc/workdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/data.psv
sed: orkdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/templates.sed: No such file or directory
bash-5.2$ chmod 777 templates.sed 
bash-5.2$ sed -E -n -f templates.sed /Users/cbongior/dev/omc/workdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/data.psv
sed: orkdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/templates.sed: No such file or directory

Using substitution:

bash-5.2$ set -x
bash-5.2$ sed -E -n -f $PWD/templates.sed $PWD/data.psv   
+ sed -E -n -f /Users/cbongior/dev/omc/workdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/templates.sed /Users/cbongior/dev/omc/workdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/data.psv
sed: orkdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/templates.sed: No such file or directory

tried this:

bash-3.2$ cd $(dirname  /Users/cbongior/dev/omc/workdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/templates.sed)
bash-3.2$ pwd
/Users/cbongior/dev/omc/workdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58
bash-3.2$ ls -al templates.sed data.psv
-rw-r--r--  1 cbongior  staff  1081134967 Aug  4 13:30 data.psv
-rwxrwxrwx  1 cbongior  staff       62087 Aug  7 15:51 templates.sed
bash-3.2$ sed -E -n -f templates.sed data.psv 
sed: orkdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/templates.sed: No such file or directory

Ok, now it's just making things UP!

bash-3.2$ cd ../../..
bash-3.2$ pwd
/Users/cbongior/dev/omc
bash-3.2$ sed -E -n -f orkdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/templates.sed workdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/data.psv
sed: orkdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/templates.sed: No such file or directory
bash-3.2$ sed -E -n -f workdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/templates.sed workdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/data.psv
sed: orkdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/templates.sed: No such file or directory
bash-3.2$ sed -E -n -f Workdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/templates.sed workdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/data.psv
sed: orkdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/templates.sed: No such file or directory
bash-3.2$ sed -E -n -f WWorkdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/templates.sed workdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/data.psv
sed: WWorkdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/templates.sed: No such file or directory
bash-3.2$ sed -E -n -f \workdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/templates.sed workdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/data.psv  
sed: orkdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/templates.sed: No such file or directory


So, if I add the missing w it ignores that. But if I add 2 w's it doesn't?

Here I changed up the arg order:

 sed -E -n -f workdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/templates.sed workdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/data.psv
sed: orkdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/templates.sed: No such file or directory

Ok, last attempt: If this is a shell issue, then any tool with have the same confusion:

bash-5.2$ grep -E '.*'  /Users/cbongior/dev/omc/workdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/templates.sed | wc -l
+ grep -E '.*' /Users/cbongior/dev/omc/workdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/templates.sed
+ wc -l
     499

My current guess is that (g)sed is interpreting this as some kind of switch, but I don't see it anywhere in the docs and this is code from a script that's been working for a while.


Solution

  • I suspect this isn't a problem with the command at all, but with something in the templates.sed file. Specifically, I suspect there's a line like

    workdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/templates.sed
    

    ...which sed will interpret as the w command (write the pattern space to a file), with the filename "orkdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/templates.sed". Here's a quick demo:

    $ cat demo.sed 
    workdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/templates.sed
    $ sed -f demo.sed 
    sed: orkdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/templates.sed: No such file or directory
    

    Try running grep orkdir /Users/cbongior/dev/omc/workdir/2023-08-03/2023-07-21-01-30-19_2023-07-31-23-59-58/templates.sed and see what it shows.