0

I have a directory (Confidential) which contains a bunch of text files.

Confidential

  :- Secret-file1.txt
  :- Secret-file2.txt
  :- Secret-file3.txt

I want to produced another textfile (Summary.txt) with textwidth, say, 80 and with following formating

Secret-file1         - This file describes various secret activities of
                       organization Secret-Organization-1
Secret-file2         - This file describes various secret activities of
                       organization Secret-Organization-2. This summarizes
                       their activities from year 2001.
Secret-file3         - This file describes various secret activities of
                       organization Secret-Organization-3. This summarizes
                       their activities from year 2024.

Where the second column is right-aligned and copied from first line of corresponding text file. For example, the "Secret-file1.txt" looks like this

This file describes various secret activities of organization Secret-Organization-1.
XXXXXXXXXXXXXXXXX BUNCH of TEXT TILL EOF XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

How can I do that? I am looking at various options at bash (e.g., sed, awk,grep, your-prefered-bash-built-in).

Thanks

A

2
  • 4
    Sorry, potential solutions are secret. Commented Jul 4, 2013 at 17:06
  • It looks to me like you'll need to extract the first lines of each file (head or sed) and then format that ready for indentation (fmt? it is not clear there's a standard word-wrapper), then arrange to process each of the formatted outputs so the first line is preceded by the file name and dash, and the remainder by a corresponding number of spaces. Commented Jul 4, 2013 at 17:18

3 Answers 3

1

This is the simplest thing that came to my mind, since you didn't write what you tried I'm leaving possible tweaks to you, but I believe this is a good start ;)

for file in "*"; do echo "$file\t\t$(head -1 "$file")"; done
Sign up to request clarification or add additional context in comments.

Comments

1

You can do this cleanly with a few lines of Python:

#!/usr/bin/env python3.3

import glob
import textwrap
from os.path import basename

INDENT=' ' * 22

for filename in glob.glob("Confidential/*.txt"):
    with open(filename, 'r') as secret:
        print("{:20s}- {}\n".format(
            basename(filename),
            '\n'.join(textwrap.wrap(secret.readline(),
                                    width=74,
                                    initial_indent=INDENT,
                                    subsequent_indent=INDENT)).strip()),
            end="")

prints

Secret-file1.txt    - This file describes various secret activities of
                      organization Secret-Organization-1
Secret-file2.txt    - This file describes various secret activities of
                      organization Secret-Organization-2. This summarizes
                      their activities from year 2001.
Secret-file3.txt    - This file describes various secret activities of
                      organization Secret-Organization-3. This summarizes
                      their activities from year 2024.

It’s not shell, but it’s going to be faster because you’re not forking a bunch of processes, and you’re not going to spend a ton of time with string-formatting and writing loops to indent the text when the textwrap module can do it for you.

Comments

0

Take a look at the fmt command in Unix. It can reformat your document in a specific width and even control indentations.

It's been a long while since I used it. However, it can follow indents, set width, etc. I have a feeling it may do what you want.

Another command to look at is pr. pr, by default breaks text into pages, and adds page numbers, but you can turn all of that offi. This is another command that may be able to munge your text the way you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.