Afficher un message
Vieux 24/08/2006, 10h26   #5
Stephane Chazelas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: process multiple files

On Wed, 23 Aug 2006 21:20:58 +0200, Steffen Schuler wrote:
> Randy wrote:
>> I am new to unix shell.
>>
>> I need to add a single line to the beginning of multiple ( 100's )
>> files. Can I automate this through shell scripting and if so, can
>> anyone point me to a newbie resource.
>>
>> Thanx Randy
>>

> with GNU tools use:
>
> for FILE in *
> do
> echo "$LINE" | cat - "$FILE" > "$FILE.NEW"
> mv "$FILE.NEW" "$FILE"
> done


There's nothing GNU specific above. However, the usage of echo
makes it incorrect, as it depends on how the GNU shell was
compiled. The GNU shell (bash) has printf, as every other POSIX
conformant shell (and there's a printf command as well in the
GNU coreutils), so you shouldn't need to use echo.

> or use
>
> find -type f -print0 | xargs -r0 sed -i "0i$LINE"

[...]

Yes, there the directory ommitted, -print0, -r, -0, -i, 0,
i<text> are all GNU specific and nor POSIX (though some if not
all can also be found in some BSDs).

The Unix/POSIX equivalent would be:

export LINE
find . -type f -exec sh -c '
for f do
{
rm -f "$f" &&
sed "1i\\
$LINE" > "$f"
} < "$f"
done' inline {} +

Except that contrary to with GNU sed, the ownership and
permissions of the files would not be attempted to be restored.

With zsh:

zmodload zsh/mapfile
for f (**/*(.DN)) mapfile[$f]=$LINE$'\n'$mapfile[$f]

--
Stephane
  Réponse avec citation
 
Page generated in 0,04982 seconds with 9 queries