bash replace spaces in list.txt with \

I'm trying to run a Linux virus scan on a list of files/folders I have ported to list.txt in a format:

some file with spaces
some other file

but I need to feed my scanning script in the format:

some\ file\ with\ spaces/
some\ other\ file/

so I would like to read in list.txt and output list_no_spaces.txt or something similar, will sed work for this, or should I use something else?

try this and redirect to a file

sed -e 's|\\|/|g' -e 's| ||g' list.txt

Why you want to add the last slash '/' to filename ?

#!/bin/bash
while read line
do 
   echo ${line// /\\ } ; # do something here
done < file

or sed

sed 's/\ /\\ /g' file

or awk

awk '{gsub(" ","\\ ")}1' file

Please search the forum next time :cool:

Yeah, I probably should've searched more diligently, sorry about that :frowning:

The problem is now sed is putting the slashes in, but it never gets to my scan command with the slashes intact I'm trying something like

#!/bin/bash
ls /some\ path\ with\ spaces/ > list.txt
sed 's/\ /\\ /g' list.txt | while read line
do
     scan="scan_command /some\ path\ with\ spaces/"$a
     eval $scan
done

if I just use that sed command to dump ls to a file, that file has all the slashes.

#!/bin/bash
dir='/some path with spaces'
a=foo;

while read line
do
     scan="scan_command ${line}/$a"
     echo "$scan" # replace to eval 
done <<< "$(ls "${dir}")"

hmm, still doesn't seem to work, output of command to scan

.../some folder

scan_command /correct\ path/some
not found

scan_command /correct\ path/folder
not found

try..

-bash-3.2$ sed -e 's| | \\|g' -e 's|$|/|g' test
some \file \with \spaces/
some \other \file/
-bash-3.2$