-2

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?

Igor Yukhimenko
  • 247
  • 1
  • 3
  • 10
  • A mandatory link: [*Why you shouldn't parse the output of `ls(1)`*](https://mywiki.wooledge.org/ParsingLs). :) – Kamil Maciorowski Jul 17 '18 at 20:35
  • 2
    The two questions are so distinct, they should be posted separately. Except the second one is already [here](https://superuser.com/q/246837/432690). Also note an answer to "can I…?" is usually "yes" or "no"; "how can I…?" would make a better question. – Kamil Maciorowski Jul 17 '18 at 20:44
  • -1. Feedback: despite my remark you let the two questions be. Now there are two answers, each one answers a different question. This is not how the site is meant to work. – Kamil Maciorowski Jul 18 '18 at 09:35
  • Now it's a duplicate of [How do I add text to the beginning of a file in Bash?](https://superuser.com/q/246837/432690) Plus you made one of the answers off-topic, but this was unavoidable, unless you left the two distinct questions, which had its obvious downsides. The problem is that from some point there was no way out of this mess without any sacrifices. :( – Kamil Maciorowski Jul 18 '18 at 10:52

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,300
  • 3
  • 32
  • 48
  • Note: the OP narrowed the question and turned your answer off-topic. – Kamil Maciorowski Jul 18 '18 at 10:55
  • @KamilMaciorowski - Well, half the answer, anyway, but removal of `grep` is a reasonable part of the answer to the the question as it now stands, though it maybe should not be the first thing to say. I took account of your comment about avoiding `ls`. I also saw the question as an X-Y problem, and tried to answer what I thought was the real question. – AFH Jul 18 '18 at 11:13
  • True… I just wanted to let you know the things got somewhat complicated. – Kamil Maciorowski Jul 18 '18 at 11:20
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,327
  • 4
  • 20
  • 29