1

So I have a bunch of files that I want to edit at once using sed but the problem is that I need to edit a line and change the text to a file name stored in variable filename. Every time I tried, it changes the text to the literal "filename" and I don't know how to fix it.

The command I've used is:

sed -i 's/$x/'$filename'/g' *.html
Mihir Luthra
  • 6,059
  • 3
  • 14
  • 39
Clui
  • 13
  • 3
  • 3
    A good idea would be to show how you did it so we can understand it better and help you. Its hard to theoritically solve it. – Mihir Luthra May 22 '20 at 10:06
  • 3
    Welcome to Stack Overflow. SO is a question and answer page for professional and enthusiastic programmers. Add your own code to your question. You are expected to show at least the amount of research you have put into solving this question yourself. – Cyrus May 22 '20 at 10:06
  • 3
    Please add sample input (no descriptions, no images, no links) and your desired output for that sample input to your question (no comment). – Cyrus May 22 '20 at 10:07

2 Answers2

2

From your description and the command used I assume that you try to replace a variable text stored in

X="text2replace"

with a filename stored in

FILENAME="filename"

According this, a command like

sed -i "s/${X}/${FILENAME}/g" *.html

should do the job. It will replace all occurrences of text2replace in all HTML files found with the string filename.

You may have also a look into

U880D
  • 8,601
  • 6
  • 24
  • 40
1

This might work for you (GNU parallel and sed):

parallel --header : sed -i 's#{x}#{filename}#' {file} ::: file *.html ::: x pattern ::: filename name
potong
  • 55,640
  • 6
  • 51
  • 83