Visit Our UNIX and Linux User Community


bash replace spaces in list.txt with \


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash replace spaces in list.txt with \
# 1  
Old 07-22-2009
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:
Code:
some file with spaces
some other file

but I need to feed my scanning script in the format:
Code:
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?

Last edited by unclecameron; 07-22-2009 at 11:11 PM..
# 2  
Old 07-22-2009
try this and redirect to a file

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

# 3  
Old 07-22-2009
Why you want to add the last slash '/' to filename ?
Code:
#!/bin/bash
while read line
do 
   echo ${line// /\\ } ; # do something here
done < file

or sed
Code:
sed 's/\ /\\ /g' file

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

Please search the forum next time Smilie
# 4  
Old 07-23-2009
Yeah, I probably should've searched more diligently, sorry about that Smilie

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
Code:
#!/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.
# 5  
Old 07-23-2009
Code:
#!/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}")"

# 6  
Old 07-23-2009
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
# 7  
Old 07-23-2009
try..
Code:
-bash-3.2$ sed -e 's| | \\|g' -e 's|$|/|g' test
some \file \with \spaces/
some \other \file/
-bash-3.2$


Previous Thread | Next Thread
Test Your Knowledge in Computers #624
Difficulty: Medium
The continue statement is used in Python to skip the rest of the statements in the current loop block and to continue to the next iteration of the loop.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Linux Commands needed for replacing variable number of spaces with a single , in a txt file

Hi I want to read a text file and replace various number of spaces between each string in to a single "," or any other character .Please let me know the command to do so. My input file is a txt file which is the output of a SQL table extract so it contains so many spaces between each column of the... (2 Replies)
Discussion started by: Hari Prasanth
2 Replies

2. Shell Programming and Scripting

Bash incert line from 1.txt to 2.txt

i would like to insert a line from 2.txt into 1.txt between " and " or a way of adding to the end of each line " _01_ and have the numbers correspond to the line # 1.txt= foofoo "" _01_ foofoo "" _02_ foofoo "" _03_ foofoo "" _04_ 2.txt= ... (6 Replies)
Discussion started by: klein
6 Replies

3. Windows & DOS: Issues & Discussions

2 Questions: replace text in txt file, add text to end of txt file

so... Lets assume I have a text file. The text file contains multiple "#" symbols. I want to replace all thos "#"s with a STRING using DOS/Batch I want to add a certain TEXT to the end of each line. How can I do this WITHOUT aid of sed, grep or anything linux related ? (1 Reply)
Discussion started by: pasc
1 Replies

4. Shell Programming and Scripting

Populating a BASH array with a list of files including spaces-in-the-name

For the record, I already tried telling mgmt and the users to disallow spaces in filenames for this script, but it isn't happening for a number of ID10T-error-based reasons. I have simple list of 3 files in a directory that are named like this: bash-3.2$ ls -1 file* file1 file1 part2... (2 Replies)
Discussion started by: ckmehta
2 Replies

5. Shell Programming and Scripting

Deleting lines that contain spaces in a txt file

I need some help deleting lines in a file that contain spaces. Im sure awk or sed will work but i dont know much about those commands. Any help is appreciated :D (7 Replies)
Discussion started by: r04dw4rri0r
7 Replies

6. Shell Programming and Scripting

command to list .txt and .TXT file

Hi expersts, in my directory i have *.txt and *.TXT and *.TXT.log, *.txt.log I want list only .txt and .TXT files in one command... how to ?? //purple (1 Reply)
Discussion started by: thepurple
1 Replies

7. Solaris

list files .txt and .TXT in one command

Dear experts, In a directory i have both *.TXT and *.txt files. I have a script- for file in `ls *.txt`; do mv $file /tmp/$file How to list both *.txt and*.TXT file in one command so that script will move both .txt or .TXT whatever it find. br//purple (4 Replies)
Discussion started by: thepurple
4 Replies

8. Shell Programming and Scripting

Replace spaces

Hi guys, so I have another issue. Can I use sed to replace spaces in a string or variable with %20 I am having trouble with using curl on URL's containing spaces Thanks! (12 Replies)
Discussion started by: tret
12 Replies

9. UNIX for Dummies Questions & Answers

counting a list of string in a list of txt files

Hi there! I have 150 txt files named chunk1, chunk2, ........., chunk150. I have a second file called string.txt with more than 1000 unique strings, house, dog, cat ... I want to know which command I should use to count how many times each string appears in the 150 files. I have tried... (4 Replies)
Discussion started by: Pep Puigvert
4 Replies

10. Shell Programming and Scripting

If I want to replace txt file with somestring,which way should I go?

If I have txt file named "test.txt" like this 1|2|3|4| 2|3|4|5| 3|4|5|6| 4|5|6|7| I need to replace a "|"(pipe) at the end of the line with ";"(semicolon) it will look like this after replace 1|2|3|4; 2|3|4|5; 3|4|5|6; 4|5|6|7; Which scripts I have to use? Many Thanks (10 Replies)
Discussion started by: guitaroa47
10 Replies

Featured Tech Videos