spaces in file names and $1

Fahrvergnuugen

I am the law!
I'm trying to write a simple shell script (basically as a learning exercise) that will pipe the out put of find to a command that takes two arguments (such as cp for example)

Here's the closest I've gotten
find ~/path/to/dir -name "*.txt" -print | replace " " "\ " | awk '{print $1}'

for some reason, the contents of $1 are truncated right at the space.

EXAMPLE
find ~reloaded/Documents/test -name "*.txt" -print | replace " " "\ "

will output this:
/Users/reloaded/Documents/test/file\ with\ space.txt

find ~reloaded/Documents/test -name "*.txt" -print | replace " " "\ " | awk '{print $1}'

will output this:
/Users/reloaded/Documents/test/file\


How can I fix this?
 
From memory, I think the command line is broken up into commands seperated by spaces and each of these is labelled $1, $2, $3 and so on. There was another symbol for the entire command line. I think it was $* but I can't test that right now because I'm not at my Mac (sigh).
 
As I am not entirely familiar with the replace command, would it perhaps be easier to use awk's built in gsub syntax? i believe for awk it is something like
Code:
gsub(" ","\ ",/path/to/file)
--- hope this is of some use

graham
 
entirely possible. but the problem here is that the path of the file has spaces in it. and spaces are the delimiter for setting up the $1 $2, etc variables. I tried $@ but that didnt' work, gave me this error:

awk: syntax error at source line 1
context is
{print >>> $@ <<<
awk: illegal statement at source line 1
 
You need to escape your spaces when you run the command. So, it's either:

./myscript "pathname with\spaces.txt"

or

./myscript pathname\ with/spaces.txt

That will prevent the shell from trying to break the pathname up.

Wade
 
if you read the script i wrote, I AM escaping the spaces. It doesnt matter because there is still a space "\ "

so everything before the space gets assigned to $1 and everything after the first space gets assigned to $2, etc. it uses spaces as the delimiter for breaking up the variables.

try it yourself, all you have to do is copy and paste my script into your terminal, it doesn't actually do anything except list files.

find ~reloaded/Documents/test -name "*.txt" -print | replace " " "\ "

compared to

find ~reloaded/Documents/test -name "*.txt" -print | replace " " "\ " | awk '{print $1}'
 
Doesn't $0 contain the first argument - in this case the word 'awk'?

Check the manpage for find - adding the -X flag seems to be what you need.

Edit - d'oh. looks like the -X doesn't make it escape the spaces, just skip files with spaces and such in them...
 
OK, here ya go.

find -name "*.txt" -print0 | xargs -0 -n 2 somecommand

the -print0 to find means print the names separated by an ascii zero.
The -0 to xargs means treat everything between two ascii zeroes as one argument.
The -n 2 to xargs means pass a maximum of two arguments to each invocation of somecommand. Obviously you can use any number...
 
Probably worth noting the awf -F (field separator) flag. This is more useful than $0 in most situations...

echo 1 2 3 4 | awk -F "\t" '{print $1}'
returns:
1 2 3 4

You can set the field sep. to anything you want - in this case I set it to a tab - instead of the default which is a space..
 
Back
Top