-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?

4
  • A mandatory link: Why you shouldn't parse the output of ls(1). :) Commented Jul 17, 2018 at 20:35
  • 2
    The two questions are so distinct, they should be posted separately. Except the second one is already here. Also note an answer to "can I…?" is usually "yes" or "no"; "how can I…?" would make a better question. Commented Jul 17, 2018 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. Commented Jul 18, 2018 at 9:35
  • Now it's a duplicate of How do I add text to the beginning of a file in Bash? 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. :( Commented Jul 18, 2018 at 10:52

2 Answers 2

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.
3
  • Note: the OP narrowed the question and turned your answer off-topic. Commented Jul 18, 2018 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
    Commented Jul 18, 2018 at 11:13
  • True… I just wanted to let you know the things got somewhat complicated. Commented Jul 18, 2018 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.

0

Not the answer you're looking for? Browse other questions tagged .