Xargs: variable replacement after redirect

I try to find all text files encoded iso-8859-1 and convert them to UTF-8. My attempt:

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

The problem (obvious) is that the last variable substitution will not work, because it {}occurs after the redirect and does not belong xargs. How can I get only one file with a name {}.converted, not a.txt.converted, b.txt.convertedetc. How can I do this job?

Note. I am doing this on Cygwin, where iconv does not seem to support it -o.

+5
source share
5 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

GNU Parallel :

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

GNU. : https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1

+3

for:

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
+1

Assuming none of your files have newlines in the name and assumes you have a GNU search and xargs ::

find . -name '*.txt' -print0 |
xargs -0 grep -l 'iso-8859-1' |
while read -r file; do
    iconv -f ISO-8859-1 -t UTF-8 "$file" > "$file".converted 
done

With grep -lyou do not need a command cutin the pipeline.

+1
source

You are almost there:

find . -name '*.txt' | xargs grep -i iso-8859-1 | cut -f1 -d: | \
xargs -I% echo iconv -f l1 -t utf8 % \> %.utf | bash
0
source

echo the command you want xargs to work with the string that was passed to the shell and that will overcome the replacement problem.

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

All Articles