[SOLVED] Replacing space characters w/ '\ ' using tr
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
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?
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.
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
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.
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"
Last edited by konsolebox; 06-20-2010 at 09:55 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.