sed whitespace substitution problem.
Gah - does sed do something special with whitespace in regexs?
For example, a file (file.txt) contains a list of paths with spaces in the directory names such as:- Code:
/home/directory/my documents/my file.txt Code:
/home/directory/my\ documents/my\ file.txt Code:
for filename in $(cat ~/file.txt | sed -e 's/ /\\/g') Code:
/home/directory/my\documents/my\file.txt Code:
for filename in $(cat ~/file.txt | sed -e 's/ /\\ /g') Code:
/home/directory/my\ |
$(cat ~/file.txt | sed -e 's/ /\\ /g')
this creates a list of blank separated items, and the for loop takes each item and executes the echo command, so you get three lines, because the echo command has been called three times. that may produce what you expect : sed -ibak -e 's/ /\\ /g' ~/file.txt cat ~/file.txt |
Nope, sorry - same problem.
|
Process substitution should manage blank spaces correctly:
Code:
while read filename |
I tested the commands I gave to you on my machine and the 'cat file.txt' gives :
/home/directory/my\ documents/my\ file.txt So what do you mean same problem ? |
The problem, as mentioned by berbae, is with the space in the for loop. This is controlled by the IFS env. var which defines the Field separator characters, and space is one of them.
Can you post what was the output from berbae's script? just one small change to it though; remember to put a space after the \\. However, if I need to do something in a loop with data which has spaces, I usually go with a while read loop, like so: Code:
sed -e 's/ /\\ /g' ~/file.txt | while read line -RN. |
Quote:
The FILE-LIST.tmp contains the line (could be more, but one will do for now...) Code:
/home/sambashare/JonWork/Test Directory/Some File.txt Code:
#!/bin/sh Code:
/home/sambashare/JonWork/Test\ Code:
/home/sambashare/JonWork/Test\ Directory/Some\ File.txt |
Quote:
The separator for the for loop is ' ' (space), so it feeds the variable filename with the string just before the space and sed has already replaced ' ' with '\ ' One solution that comes to my mind right now would be to read the file line by line (using head and tail and looping until we reach the total number of lines) e.g., say you store the number of lines of file.txt in the variable N then i=0 while [ $i -lt $N ] do i=$((i+1)) head -n$i file.txt | tail -n1 | sed 's/ /\\&/g' done |
Quote:
|
Quote:
result in the variable N N=$(cat file.txt | wc -l) 2. Next we read line 1, line 2, ..., until I reach line N we use a loop with a counter i that we initialize to 0 (zero) i=0 i=$((i+1)) : this increment the i counter by 1 head -n$i file.txt : this reads the '$i' first lines in file.txt tail -n1 : this reads the last line of the result of head so that we're exactly at line number $i sed 's/ /\\&/g' : replace all occurences of ' ' (space) with '\ ' (escaped space) in the line $i To recap: N=$(cat file.txt | wc -l) i=0 while [ $i -lt $N ] do i=$((i+1)) head -n$i file.txt | tail -n1 | sed 's/ /\\&/g' done |
Quote:
|
Have you tried process substitution as I suggested above?
|
Code:
awk '{ |
Quote:
Code:
./inc.sh: line 10: syntax error near unexpected token `<' |
Quote:
|
All times are GMT -5. The time now is 07:06 PM. |