-2

I have to go through 2 files stored as variables and delete the lines which contain a string stored in another variable:

file1="./file1"
file2="./file2"
text="searched text"
for i in $file1,$file2; do
sed -i.txt '/$text/d' $i
done

The files to exist in the same folder as the script. I get "No such file or directory". I have been stuck for the past 3 hours on this and honestly I'm pretty much about to quit Linux.

codeforester
  • 39,467
  • 16
  • 112
  • 140
  • try adding `echo "variable i = $i"` before the `sed` cmd. When you see the error there, remove the comma in the `for` loop. Good luck. – shellter Mar 29 '18 at 18:28
  • 2
    Running your script with `bash -x yourscript` is another good way to see what the problem is (logging what each line will actually do when executed), so you can see what it's calling and thus why it throws that specific error. – Charles Duffy Mar 29 '18 at 18:32
  • ShellCheck: *`SC2016: Expressions don't expand in single quotes, use double quotes for that`*. Also see [How to use Shellcheck](https://github.com/koalaman/shellcheck), [How to debug a bash script?](https://unix.stackexchange.com/q/155551/56041) (U&L.SE), [How to debug a bash script?](https://stackoverflow.com/q/951336/608639) (SO), [How to debug bash script?](https://askubuntu.com/q/21136) (AskU), [Debugging Bash scripts](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html), etc. – jww Mar 29 '18 at 19:52

2 Answers2

1

You have a several issues in your script. The right way to do is:

file1="./file1"
file2="./file2"
text="searched text"
for i in "$file1" "$file2"; do
  sed -i.txt "/$text/d" "$i"
done

Issues:

  • for expects a space delimited list of arguments, not comma separated
  • it is important to enclose your variable expansions in double quotes to prevent word splitting
  • you need double quotes to enclose the sed expression since single quotes won't expand the variable inside

You could catch these issues through shellcheck and debug mode (bash -x script) as suggested by Charles.

codeforester
  • 39,467
  • 16
  • 112
  • 140
0

Sorry to say that your shell script is not nicely design. In a shell scripts multi files should not be stored in multiple variables. Suppose you need to do the same operation on 100 different files what will you do? So follow the below style of code. Put all your file names in a file for example filelist.dat now see:-

First put all the file names in filelist.dat and save it

text="searched text"
while read file; do
     sed -i.txt '/$text/d' $i
done < filelist.dat

Also not sure whether sed command will work like that. If not working make it like below:-

 sed -i.txt 's|'"$text"'|d' $i
Abhijit Pritam Dutta
  • 5,521
  • 2
  • 11
  • 17