Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash add backslash before every space?

Tags:

grep

sed

awk

if I have text like

/mnt/data/web/web content/page 1/home page.txt

I get my text with the following command that is then piped to another command.

cat somefile.txt | awk '{$1=$2=$3=$4=$5=$6=$7=$8=$9=$10=""; print $0}'

I need to pipe the above command into another command to add the back slashes

How can I add a back slash before every space so I get correct *nix paths

ie.

/mnt/data/web/web\ content/page\ 1/home\ page.txt

the text could theoretically be infinitely long but I always need a backslash before every space. Final script is to be used in freebsd & linux

thanks!

like image 532
JJD Avatar asked Feb 13 '14 08:02

JJD


Video Answer


1 Answers

This awk should do:

awk '{gsub(/ /,"\\ ")}8' file
/mnt/data/web/web\ content/page\ 1/home\ page.txt
like image 103
Jotne Avatar answered Nov 15 '22 11:11

Jotne