LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Replacing space characters w/ '\ ' using tr (http://www.linuxquestions.org/questions/programming-9/replacing-space-characters-w-%5C-using-tr-814630/)

NirvanaII 06-17-2010 12:41 AM

Replacing space characters w/ '\ ' using tr
 
Okay, so I have a .txt file, LL.txt; here is its contents:

Code:

/mnt/sda1/Music/Lydia Lunch/Honeymoon In Red
/mnt/sda1/Music/Lydia Lunch/Honeymoon In Red
/mnt/sda1/Music/Lydia Lunch/Honeymoon In Red
/mnt/sda1/Music/Lydia Lunch/Honeymoon In Red
/mnt/sda1/Music/Lydia Lunch/Honeymoon In Red
/mnt/sda1/Music/Lydia Lunch/Honeymoon In Red
/mnt/sda1/Music/Lydia Lunch/Honeymoon In Red
/mnt/sda1/Music/Lydia Lunch/Honeymoon In Red
/mnt/sda1/Music/Lydia Lunch/Honeymoon In Red

I'm looking to change the ' ' chars to '\ ' with the tr command, but it is proving more difficult than I had imagined.

Thinking I was being clever I set up a $space string with '\ ' assigned, but tr returned only the backslash - see:

Code:

stuart@stuart:~/music transfer$ find /mnt/sda1/Music/Lydia\ Lunch/Honeymoon\ In\ Red/ -iname "*.mp3" > LL.txt; cut -d/ -f1-6 LL.txt | tr ' ' $space $1
tr: warning: an unescaped backslash at end of string is not portable
/mnt/sda1/Music/Lydia\Lunch/Honeymoon\In\Red
/mnt/sda1/Music/Lydia\Lunch/Honeymoon\In\Red
/mnt/sda1/Music/Lydia\Lunch/Honeymoon\In\Red
/mnt/sda1/Music/Lydia\Lunch/Honeymoon\In\Red
/mnt/sda1/Music/Lydia\Lunch/Honeymoon\In\Red
/mnt/sda1/Music/Lydia\Lunch/Honeymoon\In\Red
/mnt/sda1/Music/Lydia\Lunch/Honeymoon\In\Red
/mnt/sda1/Music/Lydia\Lunch/Honeymoon\In\Red
/mnt/sda1/Music/Lydia\Lunch/Honeymoon\In\Red
stuart@stuart:~/music transfer$

Surely I can do this directly with tr? Can I list '\ ' with find, perhaps?

Help much appreciated.

konsolebox 06-17-2010 12:48 AM

Maybe sed is more fit for that:
Code:

sed 's@ @\\ @g' LL.txt

NirvanaII 06-17-2010 02:00 AM

That worked for me - thanks!

grail 06-17-2010 05:47 AM

I would also perhaps ask what the purpose of getting the output like this is?
Or maybe it is just practice??

Please mark as SOLVED if you have your solution.

NirvanaII 06-17-2010 12:30 PM

Quote:

Originally Posted by grail (Post 4006329)
I would also perhaps ask what the purpose of getting the output like this is?
Or maybe it is just practice??

Please mark as SOLVED if you have your solution.

I'm writing a (very long!) command that finds all .ra files at a given location (each individual .ra file represented by the paths found in LL.txt), copys and then converts them to .mp3 within the working directory; once converted each path is fed into an array, and the.mp3 copied back.

Once done i'll make changes, getting the command pared down, sym linking each .ra files and so on, but that can wait for now.

You could say that I am practicing, as i'm only two days in from learning this stuff, but the command does have a practical purpose. With only limited knowledge i'm accepting of the fact that it isn't to be the most elegant command, but am enjoying the challenge of going the long way round! As things are however i'm running into other problems so have deliberately chosen not to mark this thread as "SOLVED", for the sake of keeping a sense of continuity.

NirvanaII 06-17-2010 03:15 PM

Why doesn't cp like these paths?

Code:

stuart@stuart:~/music transfer$ find /mnt/sda1/Music/Lydia\ Lunch/Honeymoon\ In\ Red/ -iname "*.mp3" > LL.txt; xargs -0 -a LL.txt | xargs -I{} cp -v $1{} . ; cut -d/ -f1-6 LL.txt  > LLnofile.txt; typeset -i x; x=1; while ((x < 10)) && array[x]=$(awk NR==$x LLnofile.txt) ; do ((x=x+1)) ; done; x=1; while (( x < 10 )) && flac_file[x]=$(ls -tr *.flac | awk NR==$x $1) ; do ((x=x+1)) ; done; x=1; while (( x < 10)) && cp -v “${flac_file[x]}” “${array[x]}”; do ((x=x+1)); done
`/mnt/sda1/Music/Lydia Lunch/Honeymoon In Red/05 - Done Dun.mp3' -> `./05 - Done Dun.mp3'
`/mnt/sda1/Music/Lydia Lunch/Honeymoon In Red/08 - Dead In The Head.mp3' -> `./08 - Dead In The Head.mp3'
`/mnt/sda1/Music/Lydia Lunch/Honeymoon In Red/09 - Some Velvet Morning.mp3' -> `./09 - Some Velvet Morning.mp3'
`/mnt/sda1/Music/Lydia Lunch/Honeymoon In Red/06 - Still Burning.mp3' -> `./06 - Still Burning.mp3'
`/mnt/sda1/Music/Lydia Lunch/Honeymoon In Red/07 - Field Of Fire.mp3' -> `./07 - Field Of Fire.mp3'
`/mnt/sda1/Music/Lydia Lunch/Honeymoon In Red/04 - Three Kings.mp3' -> `./04 - Three Kings.mp3'
`/mnt/sda1/Music/Lydia Lunch/Honeymoon In Red/02 - So Your Heart.mp3' -> `./02 - So Your Heart.mp3'
`/mnt/sda1/Music/Lydia Lunch/Honeymoon In Red/01 - Come Fall.mp3' -> `./01 - Come Fall.mp3'
`/mnt/sda1/Music/Lydia Lunch/Honeymoon In Red/03 - Dead River.mp3' -> `./03 - Dead River.mp3'
ls: cannot access *.flac: No such file or directory
ls: cannot access *.flac: No such file or directory
ls: cannot access *.flac: No such file or directory
ls: cannot access *.flac: No such file or directory
ls: cannot access *.flac: No such file or directory
ls: cannot access *.flac: No such file or directory
ls: cannot access *.flac: No such file or directory
ls: cannot access *.flac: No such file or directory
ls: cannot access *.flac: No such file or directory
cp: target `Red”' is not a directory

(There are no .flac files to copy, but they copy fine)

Code:

stuart@stuart:~/music transfer$ echo ${array[1]}
/mnt/sda1/Music/Lydia Lunch/Honeymoon In Red
stuart@stuart:~/music transfer$ echo ${array[1]} | wc -c
45

That's 44 chars + 1 newline.

NirvanaII 06-17-2010 09:08 PM

Argh! Different quote marks! "”

I've spent hours doing all sorts :) Note to self: don't copy and paste commands out of Open Office!

grail 06-18-2010 01:19 AM

Well I would be intrigued what the before and after looks like based on the script you are running?
I ask as there appears to be a lot of redundancy that we could probably help you with. Maybe when you have finished practicing.

I would probably note that I do not see a reason to replace the white space as it could handled as part of your script.

konsolebox 06-20-2010 10:54 PM

you might take a simple note at this:
Code:

VAR="a b c"
echo "$VAR"  # you'll find this as a single argument
echo $VAR    # if IFS is set to default $' \t\n', the variable will be split to 3 arguments

so when copying files with filenames with spaces, surround the arguments with double-quotes
Code:

cp "file name . ext" somewhere
cp "$VAR" somewhere

also this will work
Code:

cp *.flac somewhere
but this will not
Code:

VAR='*.flac'
cp "$VAR" somewhere

and sometimes (depends on the shell; does not work with zsh at least) this will but i think is dirty (as it is also dependent to the variable IFS (what if the expression contains a character that is part of IFS, then there will be an unexpected split):
Code:

cp $VAR somewhere
so if you still want to parse it, better use eval:
Code:

eval "cp $VAR somewhere"
if things does not work with eval as expected you can perhaps debug it by echo-ing the command:
Code:

echo "cp $VAR somewhere"

NirvanaII 06-22-2010 06:20 PM

I've certainly run into trouble with space delimiters a few times already, so thankyou konsolebox, it helps to know these things; remember however...

Syntax seems to cause me the biggest headaches a lot of the time that's for sure.


All times are GMT -5. The time now is 01:23 PM.