PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > process multiple files
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

process multiple files

Réponse
 
LinkBack Outils de la discussion
Vieux 23/08/2006, 18h57   #1 (permalink)
Randy
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut process multiple files

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

  Réponse avec citation
Vieux 23/08/2006, 19h02   #2 (permalink)
Stephan 'smg' Grein
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: process multiple files

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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
>

for file in *; do echo "chars_to_add${file}"; done
Something like that?
Mabye you use find and some shell scripting depending on your $SHELL.

b.r.,
- --
Stephan 'smg' Grein, <stephan at stephan minus rockt dot de>
http://stephangrein.de
GnuPG-Key-ID: 0xF8C275D4
FingerPrint: 5B6F 134A 189B A24D 342B 0961 8D4B 0230 F8C2 75D4
Geek by nature, Linux by choice.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)

iQIVAwUBROyYMY1LAjD4wnXUAQK1tQ/9F/jGEzTesOile2Pg3rDwhbla3LnqItCt
OCQiGXfMMJCpU6JsU++fqXYVvvOeJ3Y3O4FlGIPZ82qt++TpKp YyDFWClnGw9rbG
OKbti3VFLATeDFkKku4WSH1y44irXRr074CwvrVt6yFutMNdXf +lrLai30jloKsJ
lVTswPBM1INoinm3chYiJ8GRTjbZ99Mm3CNM9kiSsm/T1XJrZXGNH2M2KRNw4JEB
kQR7vhQgnycj+9N2wRk6RAJPhs8My6ur0vpgXCrVo7iQkIBck3 iFIbOM+/COcBkz
pze7lmaHk6ddsjwEy7b+rMCrC4qxoYSPQS49+UO1+1a9HhkDgz FYVH2aK1ov+YmF
5BG9WU0UYrVnr87DzQzg7P0UqboSKqWR/6Rmu3oMzXUoSmA916czSpVCcEpmFT2M
6hIglKHL3875zvh7xIAbO5rE/JeHibA05bhR0T4/R2eBdflcd1c+5xVSStLTV/d/
9OPDDcNNuAHRsFHzvP8xtsjr2+naGHvwrl6Q1OJUCHKAt7wnv/NvZRHs+WhZDPTi
47jAgTWbSsXMntwqDu0ZicLUXJPY+qspYypuKeAOCAd40fz2F8 9+mrmUP3RB7reS
fS3pEpQuqvjSvrc/YZPmTljwt22CquQBrX4IE7i1qkrEeCK71zzhrsySgBhLQ3FO
asIBYjIBxTo=
=D294
-----END PGP SIGNATURE-----
  Réponse avec citation
Vieux 23/08/2006, 20h12   #3 (permalink)
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: process multiple files

On 2006-08-23, 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.


line="Inserted line"
for file in ./* ## replace * with whatever pattern you want
do
{
printf "%s\n" "$line"
cat "$file"
} > tempfile && mv tempfile "$file"
done

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
  Réponse avec citation
Vieux 23/08/2006, 20h20   #4 (permalink)
Steffen Schuler
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: process multiple files

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

or use

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

look for

man 1 bash
echo
for
man 1 cat
man 1 mv
info find
man 1 xargs
info sed

Regards,

Steffen Schuler
  Réponse avec citation
Vieux 24/08/2006, 10h26   #5 (permalink)
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
Vieux 26/08/2006, 15h53   #6 (permalink)
Steffen Schuler
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: process multiple files

Stephane Chazelas wrote:
> On Wed, 23 Aug 2006 21:20:58 +0200, Steffen Schuler wrote:
> There's nothing GNU specific above. However, the usage of echo
> makes it incorrect, as it depends on how the GNU shell was
> compiled.


Thank you Stephane for this info. I didn't know what you wrote about
echo.

Regards,

Steffen Schuler
  Réponse avec citation
Vieux 26/08/2006, 17h00   #7 (permalink)
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: process multiple files

2006-08-26, 16:53(+02), Steffen Schuler:
> Stephane Chazelas wrote:
>> On Wed, 23 Aug 2006 21:20:58 +0200, Steffen Schuler wrote:
>> There's nothing GNU specific above. However, the usage of echo
>> makes it incorrect, as it depends on how the GNU shell was
>> compiled.

>
> Thank you Stephane for this info. I didn't know what you wrote about
> echo.

[...]

That's in the FAQ.

echo is one of the least portable commands.

POSIX says that its behavior is unspecified.

UNIX says that it must behave like on System V where no option
is recognised, where \n, \b, \f... are expanded.

bash, by defaults behaves the BSD way. It is almost POSIX
conformant. The only problem being that echo -e doesn't output
"-e<LF>".

However you can compile bash so that its echo is more Unix
conformant (expands the \x), you can achieve the same by issuing
"shopts -s xpg_echo". However, it is still not POSIX nor UNIX as
echo -e doesn't output "-e".

More generally.

echo "$var"

doesn't output the content of $var followed by a newline
character unless $var doesn't start with a - or doesn't contain
any backslash character.

printf is the command that must be used instead of echo.

--
Stéphane
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h34.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11784 seconds with 15 queries