1/4 Hello UNIX command line gurus, let's say I have a file called
filenames.log that contains file names, like /usr/bin/hello,
/media/dvd/wayne's world, /media/music/my music!.mpg. I'd like
to do something like:
% cat filenames.log | xargs tar rvf /backup/today.tar
However, I can't do that because I need to escape characters
like ', \ , \!, and many others. What's an elegant way of
doing this? I thought about using sed, but I'd have to come
up with a comprehensive list of characters that I have to
escape, which is lame. Ideally I'd like something like:
% cat filenames.log | escape | xargs rvf /backup/today.tar
Got ideas? Thanks!
\_ Here are a few ways to do it. Hopefully you find one elegant.
sed 's/[^A-Za-z0-9]/\\&/g' filenames.log | xargs tar rvf ...
sed 's/./\\&/g' filenames.log | xargs tar rvf ...
tr '\n' '\0' <filenames.log | xargs -0 tar rvf ...
sed 's/[^A-Za-z0-9]/\\&/g' filenames.log | xargs tar rvf today.tar
\_ thanks. So the above sed, with "&", is equivalent to
perl's $1 or \1? It's seems like it's the same as perl's
s/([^A-Za-z0-9])/\\$1/g;
So here is another question. How do you specify $1, $2,
etc in sed? thanks.
\_ Sed's & is perl's $& (the entire search string). Sed uses \1,
\2, etc. to retrieve stuff from parens. Also note that you
have to use \( and \) in sed, not just ( and ) like in perl.
sed 's/./\\&/g' filenames.log | xargs tar rvf today.tar
tr '\n' '\0' <filenames.log | xargs -0 tar rvf today.tar
tar rvfI today.tar filenames.log
\_ % cat filenames.log | perl -ne 'print quotemeta;' |
xargs rvf /backup/today.tar
\-i'd use the tr command above to NULL pad + xargs -0 OR
modify a perl script called "findtar" OR use GNU tar's
-T|--files-from option possibly with --null. ok tnx --psb
\_ if performance is an issue would perl be slower because the
executable is bigger? Or would it be faster because it's
got optimizations built in?
\-no offense, but this is not a question worth asking.
or at least not worth answering.
\_ xargs is wrong in this case. Use normal tar with -T. -vadim
\- tar -rv -f mybackup.tar -T file_list.txt
\_ keywords: perl escape character space |