Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
$(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
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
do
echo $line
# other ops here
done
Interestingly, when I look at the FILE-LIST.tmp file again after running the script, the file looks correct, so I assume the sed part is doing it's job, but I'd expect to see: -
and I'm want to escape the spaces so that the output ends up like: -
Code:
/home/directory/my\ documents/my\ file.txt
so that Linux won't shit itself. The following regex...
Code:
for filename in $(cat ~/file.txt | sed -e 's/ /\\/g')
do
echo "$filename"
done
...outputs this: -
Code:
/home/directory/my\documents/my\file.txt
Instead the output I get is: -
Code:
/home/directory/my\
documents/my\
file.txt
What the hell? Since when did a space become a newline character?
The problem is not sed. It's your for loop
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
The problem is not sed. It's your for loop
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
1. first we get the total number of lines of file.txt and store the
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
1. first we get the total number of lines of file.txt and store the
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
: 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
I'm getting command not found doing that. Not sure where. Thanks anyway.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.