You are not logged in.

#1 2009-11-30 02:34:56

archnemesis
Member
Registered: 2009-11-27
Posts: 53

how do I enclose each line returned by grep in quotes?

here is what i want to do:

 cp `ls | grep -i stuff` ~/zzTMP/

but the problem is the filenames returned contain spaces and are therefore interpreted by copy as many separate files per line, none of which match. So can i somehow enclose the lines returned by grep in single quotes?

Offline

#2 2009-11-30 03:24:15

fsckd
Forum Fellow
Registered: 2009-06-15
Posts: 4,173

Re: how do I enclose each line returned by grep in quotes?

archnemesis wrote:

here is what i want to do:

 cp `ls | grep -i stuff` ~/zzTMP/

but the problem is the filenames returned contain spaces and are therefore interpreted by copy as many separate files per line, none of which match. So can i somehow enclose the lines returned by grep in single quotes?

I would use find and xargs but if you really insist then use sed or whatever: cp `ls | grep -i stuff | sed "s/^\(.*\)\$/'\1'/"` ~/zzTMP/.


aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies

Offline

#3 2009-11-30 03:26:17

IgnorantGuru
Member
Registered: 2009-11-09
Posts: 640
Website

Re: how do I enclose each line returned by grep in quotes?

wouldn't this be equivalent:

cp *stuff*  ~/zzTMP/

or if you really need grep you could use

IFS=$'\n'; for f in `ls -1 | grep -i stuff`; do cp "$f" ~/zzTMP/; done; IFS=" "

Last edited by IgnorantGuru (2009-11-30 03:36:15)

Offline

#4 2009-11-30 03:30:17

IgnorantGuru
Member
Registered: 2009-11-09
Posts: 640
Website

Re: how do I enclose each line returned by grep in quotes?

Yeah find is probably better

find -maxdepth 1 -type f -iname "*stuff*" -execdir cp {} ~/zzTMP/ \;

Offline

Board footer

Powered by FluxBB