Ajure
Ajure

Reputation: 125

formatting file using bash

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

Upvotes: 0

Views: 151

Answers (3)

David W.
David W.

Reputation: 107040

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.

Upvotes: 0

andrewdotn
andrewdotn

Reputation: 34813

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.

Upvotes: 1

Alberto Zaccagni
Alberto Zaccagni

Reputation: 31560

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

Upvotes: 1

Related Questions