|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#2 (permalink) |
|
Messages: n/a
Hébergeur: |
-----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----- |
|
|
|
#3 (permalink) |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 (permalink) |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#5 (permalink) |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 (permalink) |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#7 (permalink) |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|