0

I have a file.txt with a lot of line inside such as::

blastdbcmd -db /path/path/LO/db_lh/makedb_KO -entry scaffold_2731 -range 32270-32509-strand minus >> file.fst
blastdbcmd -db /path/path/LO/db_lh/makedb_lKO -entry scaffold_2781 -range 3230-3508-strand minus >> file.fst
etc.

And I would like replace all file.fst pattern by path/data/path/LO/file.txt

and then get:

blastdbcmd -db /path/path/LO/db_lh/makedb_KO -entry scaffold_2731 -range 32270-32509-strand minus >> path/data/path/LO/file.txt
blastdbcmd -db /path/path/LO/db_lh/makedb_lKO -entry scaffold_2781 -range 3230-3508-strand minus >> path/data/path/LO/file.txt

etc.

It tried :

sed -i -e 's/file.fst/path/data/path/LO/file.txt/g' /path/path/file_script_recover_copie.txt

But it does not work, someone has a better idea? Thank you.

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
bewolf
  • 165
  • 9

2 Answers2

2

Backup your file(s), and try this:

sed -i -e 's@file.fst@path/data/path/LO/file.txt@g' /path/path/file_script_recover_copie.txt

Eg:

$ cat file
blastdbcmd -db /path/path/LO/db_lh/makedb_KO -entry scaffold_2731 -range 32270-32509-strand minus >> file.fst
blastdbcmd -db /path/path/LO/db_lh/makedb_lKO -entry scaffold_2781 -range 3230-3508-strand minus >> file.fst
etc.

$ sed -e 's@file.fst@path/data/path/LO/file.txt@g' file
blastdbcmd -db /path/path/LO/db_lh/makedb_KO -entry scaffold_2731 -range 32270-32509-strand minus >> path/data/path/LO/file.txt
blastdbcmd -db /path/path/LO/db_lh/makedb_lKO -entry scaffold_2781 -range 3230-3508-strand minus >> path/data/path/LO/file.txt
etc.

The problem is because your replacement has / in it, where / also is your sed's s delimiter. But sed can use other characters as s's delimiter. So I changed it to @.
(You can use others too, so long they're in right places)

Another way is to escape /, by adding \before it, so inside s they become \/. (This applies to other special characters too, for example to use'you can put\'` inside.)

Til
  • 5,150
  • 13
  • 26
  • 34
  • Just to add to this: the first character after the `s` defines the delimiter in the substitution expression, it doesn't have to be `/`. – eduffy Jan 12 '19 at 12:04
  • It did work thank you :) so the `@` allows to take into account the `/// `patterned right? – bewolf Jan 12 '19 at 12:04
  • Kudos for your string of recent answers, but please refrain from answering questions which are obvious and clear duplicates. There's a good collection of shell scripting canonicals in the [Stack Overflow `bash` tag info page](/tags/bash/info). – tripleee Jan 12 '19 at 13:08
  • @tripleee Okay, got it. Thanks for the link. – Til Jan 12 '19 at 13:11
  • Sure thing! If you'd like to coordinate canonicals etc, there's a link to a Stack Overflow Bash chat room where I hang out. – tripleee Jan 12 '19 at 13:13
1

If you are ok with awk then you could try following.

awk -v val="path/data/path/LO/file.txt" '{sub("file.fst",val)} 1' Input_file

Pass path's value to -v val variable of awk and then it should work.

With sed in case you want to keep delimiter as / itself then try following.(I am escaping \/ in sed solution so that it will NOT be considered as it is used for closing sed's block)

sed 's/file.fst/path\/data\/path\/LO\/file.txt/'  Input_file
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93