LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-16-2010, 11:41 PM   #1
NirvanaII
Member
 
Registered: Jun 2005
Posts: 139

Rep: Reputation: 18
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.

Last edited by NirvanaII; 06-16-2010 at 11:44 PM.
 
Old 06-16-2010, 11:48 PM   #2
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Maybe sed is more fit for that:
Code:
sed 's@ @\\ @g' LL.txt
 
Old 06-17-2010, 01:00 AM   #3
NirvanaII
Member
 
Registered: Jun 2005
Posts: 139

Original Poster
Rep: Reputation: 18
That worked for me - thanks!
 
Old 06-17-2010, 04:47 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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.
 
Old 06-17-2010, 11:30 AM   #5
NirvanaII
Member
 
Registered: Jun 2005
Posts: 139

Original Poster
Rep: Reputation: 18
Quote:
Originally Posted by grail View Post
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.

Last edited by NirvanaII; 06-17-2010 at 02:58 PM.
 
Old 06-17-2010, 02:15 PM   #6
NirvanaII
Member
 
Registered: Jun 2005
Posts: 139

Original Poster
Rep: Reputation: 18
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.
 
Old 06-17-2010, 08:08 PM   #7
NirvanaII
Member
 
Registered: Jun 2005
Posts: 139

Original Poster
Rep: Reputation: 18
Argh! Different quote marks! "”

I've spent hours doing all sorts Note to self: don't copy and paste commands out of Open Office!
 
Old 06-18-2010, 12:19 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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.
 
Old 06-20-2010, 09:54 PM   #9
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
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"

Last edited by konsolebox; 06-20-2010 at 09:55 PM.
 
Old 06-22-2010, 05:20 PM   #10
NirvanaII
Member
 
Registered: Jun 2005
Posts: 139

Original Poster
Rep: Reputation: 18
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Replacing Characters with sed zokken Programming 9 12-02-2009 07:34 PM
Regarding replacing characters in log file rajaniyer123 Linux - General 4 04-12-2008 10:32 AM
bash replacing characters within a variable baks Programming 5 03-19-2007 12:33 AM
Replacing characters in file names? yuccabrevifolia Linux - Software 1 04-19-2006 01:31 PM
replacing characters in a file with others pcdude Linux - General 6 01-19-2005 12:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration