0

I'm having an issue with a bash script I've written. The command works until the SED, and I'm not really sure what I've done incorrectly. HAL is the variable in the text file that I'm trying to replace. Any ideas?

 for i in $( cat LIST.txt) do 
 sed s/HAL/i/ <~/test/template.txt > new$i.txt
 done

The error given is "syntax error near unexpected token sed" it then gives the sed line.

George
  • 179
  • 1
  • 11
  • `for i in $(cat foo.txt)` is definitely the wrong way to iterate over lines in a file, to start. See BashFAQ #001: http://mywiki.wooledge.org/BashFAQ/001 – Charles Duffy Oct 09 '14 at 15:08
  • ...that said, you aren't saying *how* it fails when it reaches the `sed` expression, so how are we supposed to know what needs to be fixed? – Charles Duffy Oct 09 '14 at 15:08
  • ...also, quote your expansions: `>"new$i.txt"` will succeed in places where `>new$i.txt` fails. – Charles Duffy Oct 09 '14 at 15:09
  • http://mywiki.wooledge.org/BashPitfalls#for_i_in_.24.28ls_.2A.mp3.29 is also relevant. – Charles Duffy Oct 09 '14 at 15:10
  • 2
    ...anyhow -- as it is, this question can't be answered, because it doesn't state a specific error, as opposed to "an issue" and "works until". – Charles Duffy Oct 09 '14 at 15:11
  • 1
    Your `sed` invocation as written simply replaces any occurrences of "HAL" with "i". Perhaps you meant `sed -e "s/HAL/${i}/"`? – twalberg Oct 09 '14 at 17:22

1 Answers1

0

I think you're missing the semi-colon between the for part and the do.

for i in $(cat LIST.txt); do 
    sed s/HAL/i/ <~/test/template.txt > new$i.txt
done

This is probably better rewritten as (assuming there's one word per line in LIST.txt:

while read i; do
    # Are you sure you don't want to substitute with the variable (`$i`
    # in this case) instead of "i" in the line below?
    sed s/HAL/i/ < ~/test/template.txt > new$i.txt
done < LIST.txt

You might want your sed line to be like below if you want to substitute with $i:

sed "s/HAL/$i/" < ~/test/template.txt > new$i.txt
zerodiff
  • 1,690
  • 1
  • 18
  • 23