0

I want to create a script that puts a multi-line text as a header in multiple files with txt extension. But the text to be inserted also includes the name of the file in which it will be inserted.

Here goes the text to be inserted:

abcdgd FILENAME dhsgabc 
shcnwk shfgamvk 
cjshjg nwdcbi 
skfh 
nwvjcnd
skfh dvwuv
fshvur

egfbt hethtn nhnh

gdngng  dehdnnrty

I have many files, with the name 001.txt, 002.txt, 003.txt and so on. The NAMEFILE needs to be only 001, 002, 003, and so on (without the .txt extension).

2

You should be able to do that with a simple shell loop over a here-document. For example, given files 001.txt to 005.txt of the form

$ cat 003.txt 
=== START OF ORIGINAL FILE 003 ===
stuff
more stuff
even more stuff

then

for i in {001..005}; do cat - "$i.txt" <<EOF >tmpfile
+++ HEADER $i +++
new stuff
more new stuff

EOF
mv tmpfile "$i.txt"
done

results in files of the form

$ cat 003.txt 
+++ HEADER 003 +++
new stuff
more new stuff

=== START OF ORIGINAL FILE 003 ===
stuff
more stuff
even more stuff

and so on.

1

The script below does the job recursively.

Using exactly your example text:

#!/usr/bin/env python3
import os
import sys

dr = sys.argv[1]

def addlines(file):
    """
    the list below contains the lines, to be added at the top. The lines will
    appear in separate lines. In case you want to add an extra line break, use
    \n like in the last two lines in the example- content below.
    """
    return [
        "abcdgd "+file.replace(".txt", "")+" dhsgabc",
        "shcnwk shfgamvk",
        "cjshjg nwdcbi",
        "skfh",
        "nwvjcnd",
        "skfh dvwuv",
        "fshvur",
        "\negfbt hethtn nhnh",
        "\ngdngng  dehdnnrty",
        ]

for root, dirs, files in os.walk(dr):
    for file in files:
        path = root+"/"+file
        # first read the file and edit its content
        with open(path) as new:
            text = ("\n").join(addlines(file))+"\n"+new.read()
        # then write the edited text to the file
        with open(path, "wt") as out:
            out.write(text)

It changes a file, called:

Liesje leerde Lotje lopen.txt

with content:

aap
noot

into:

abcdgd Liesje leerde Lotje lopen dhsgabc
shcnwk shfgamvk
cjshjg nwdcbi
skfh
nwvjcnd
skfh dvwuv
fshvur

egfbt hethtn nhnh

gdngng  dehdnnrty
aap
noot

How to use

  1. Copy the script into an empty file, save it as change_file
  2. Change in the function addlines(file) the text (but don't touch +file.replace(".txt", "")+. \n stands for a(n extra) line break.
  3. Run it with the targeted directory as an argument (the directory with your files):

     python3 /path/to/change_file /directory
    

    if /directory includes spaces, use quotes:

     python3 /path/to/change_file '/directory'
    

Note

If the files are really huge, we might need to optimize the procedure a bit into a per line approach, but in average situations, this should work fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.