-1

To postgenerate fake-module-bpy (For blender Python)... нowever, the language does not matter. So, I use next console command

]$ ls  | grep -v _init_ | sed -E 's/^([a-zA-Z0-1]+)/import \1/' >> __init__.py

It will written all python files to __init__.py as import (Exclude itself __init__.py)

Can I write/append not the end, but the beginning of the file?

2 Answers2

1

Everything that grep does can be done within sed: see this tutorial. In this case you add -e '/PATTERN/d' to reproduce the grep -v option:

ls | sed -E -e '/_init_/d' -e 's/^([a-zA-Z0-1]+)/import \1/' >> __init__.py

You can eliminate ls by using instead:

for f in *; do echo "$f"; done | sed -E -e '/_init_/s' -e 's/^([a-zA-Z0-1]+)/import \1/' >> __init__.py

This removes the need for using an external program and handling any extra information or flags that may be added by ls defaults.

There are several ways to add the files in reverse order:

  • If you use ls, then ls -r will output the files in reverse order.
  • If you use for f in *; ..., then pipe the output through sort -r.
  • You can use tac to reverse the line order in the output afterwards.
  • The long-winded way is to output each file to a single-line file, append the current output list to it, then move this to overwrite the running output list.
AFH
  • 17,958
1

This answers your second question.

Here's a little trick to write the content at the start of the file with sed:

sed -i '1s/^/to start of file\n/' existing_file.txt

Maybe with a little modification it is the right thing for you.

robinCTS
  • 4,407