find and replace a path with sed?

Hi,

I am attempting to find the path /subject/grin* and replace it with /subject/$i
My attempt:

for i in $(ls)
do
sed -e 's:"/subjects/grin.*":"/subjects/$i.*":g'

and several variants with no luck. Please help. Thanks!

sed  's/\/subject\/grin\*/\/subjects\/$i/g'
1 Like
for i in $(ls)

Using ls is superfluous, this is sufficient:

for i in *
1 Like

Thank you so much for responding. One problem, though. It is reading $i as characters and not a variable.....

sed  's/\/subject\/grin\*/\/subjects\/"'$i'"/g'

try this code ....:slight_smile:

Pls explain a bit more. sed needs an input stream which it does not see if used in your example's for loop; it's reading stdin instead. So sth. like ls|sed... could work on a directory, but then, what variable (i) do you want to use? I guess you need two loops on two different directories, one producing the input stream and the other the variables to replace certain parts of that stream?!

Hi RudiC,

the ls is actually a list of subject names that are being replaced within a parameter file that another application is dependent on. As the app processes a particular subject, the previous info is being replaced with new subject info via sed. Here is what I have, including what pamu recommended - which isn't working.

for i in $(cat list.txt)


do
sed -e 's/\/subjects\/grin\*/\/subjects\/"'i'"/g' -e "s/DDG_FL/"$i"_DDG_FL/g" -e "s/grin.*_BET_MPRAGEMoCo/"$i"_BET_MPRAGEMoCo/g" -e "s/grin.*_DDG_MC/"$i"_DDG_MC/g" L1_DDG.fsf > "$i"_DDG.fsf

feat "$i"_DDG.fsf


done

---------- Post updated at 03:00 PM ---------- Previous update was at 02:08 PM ----------

sed -e 's/\/subjects\/grin.../*\/subjects\/'$i'/g'

works......... thanks all ...

I don't think than anybody can follow this without representative sample input, a definitive description of the process, and matching expected output.

In this sentence, what is "it" ? Are you trying to rename the files or edit the contents of the files or what?

Hi methyl,

sorry for the ambiguity. What I meant by "it" was the first path. I was trying to replace a path within a text file with a different path. I could not get it to work because it involved escapes, a wildcard, and a variable. I doubt it is elegant but it seems to work now. Thanks for following up.

1 Like