0

I have a problem with changing file content using sed. Let me describe what I would like to do.

I have a few text files in one folder:

123.txt

456.txt

789.txt

If you open them, each of those text files will have such a text at the top line:

Blablabla

What I would like to do is using while and sed commands to replace Blablabla with the file name without extension and that's how I wanted to do it:

for files in ./*.txt; do
    while ! [[ $(grep "${files%.*}" ./"${files##*/}") ]]; do
        table_name="${files%.*}"
        sed -i 's/Blablabla/$table_name/g' ./"${files##*/}"
    done    
done

Unfortunately it doesn't work. Could anyone help me with that?

Thanks in advance!

nemox007
  • 29
  • 4
  • 1
    BashFAQ #21, http://mywiki.wooledge.org/BashFAQ/021, covers the topic in detail -- including (in accordance with best practices) suggesting tools that are more portable than `sed -i` (a non-POSIX extension). – Charles Duffy Apr 26 '16 at 20:06
  • 1
    Why do you need `while` loop inside `for` loop? – anubhava Apr 26 '16 at 20:06
  • 1
    What about your attempt doesn't work? What is that `grep` supposed to be doing? – Etan Reisner Apr 26 '16 at 20:07
  • @anubhava: My code has a bug and sometimes **sed** doesn't work so using **while** I want to repeat loop until it's done. – nemox007 Apr 27 '16 at 08:55
  • @ Etan Reisner: I wanted to use **grep** to check if the text inside of the file was already changed or not – nemox007 Apr 27 '16 at 08:56

0 Answers0