Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xargs: variable substitution after redirection

Tags:

find

bash

xargs

I'm trying to find all text files which have the encoding iso-8859-1 and convert these to UTF-8. My attempt so far is:

find . -name '*.txt' | xargs grep 'iso-8859-1' | cut -d ':' -f1 | 
xargs iconv -f ISO-8859-1 -t UTF-8 {} > {}.converted

The (obvious) problem is that the last variable substitution won't work, since {} occurs after the redirection, and doesn't belong to xargs. As is I only get one file called {}.converted, not a.txt.converted, b.txt.converted etc. How can I make this work?

Note: I'm doing this on Cygwin, where iconv doesn't seem to support -o.

like image 958
Alexander Torstling Avatar asked Jan 24 '12 11:01

Alexander Torstling


2 Answers

If you have GNU Parallel http://www.gnu.org/software/parallel/ installed you can do this:

find . -name '*.txt' | parallel grep -il iso-8859-1 | parallel iconv -f ISO-8859-1 -t UTF-8 {} \> {}.converted

You can install GNU Parallel simply by:

wget http://git.savannah.gnu.org/cgit/parallel.git/plain/src/parallel
chmod 755 parallel
cp parallel sem

Watch the intro videos for GNU Parallel to learn more: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1

like image 148
Ole Tange Avatar answered Oct 14 '22 01:10

Ole Tange


How about a for loop like:

for file in `find . -name '*.txt' | xargs grep 'iso-8859-1' | cut -d ':' -f1`; do
    iconv -f ISO-8859-1 -t UTF-8 $file > $file.converted
done
like image 2
e.dan Avatar answered Oct 13 '22 23:10

e.dan