Skip to contentSkip to conversation

leading spaces in a text file.

DISCUSSION Matthew Stapleton 4 participants 4 messages Aug 24, 2002

lore
Share & tools
lore
#0 lore raw

i have been formatting some texts for an online archive of e-texts. every once and a while i encounter a file that has leading spaces in front of each sentence. to remedy this problem i have used the 'tr' utility to squeeze multiple occurrences of spaces. this works very nicely with the exception that the output file has a single leading space at the beginning of each sentence. is there a better way of doing this or of finishing this job?

thanks

Matthew Stapleton
====================================
http://webpages.marshall.edu/~staple12
Resistance is Futile!
====================================
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

#1 lore raw
Quoted 7 lines · Matthew Stapleton

i have been formatting some texts for an online archive of e-texts. every once and a while i encounter a file that has leading spaces in front of each sentence. to remedy this problem i have used the 'tr' utility to squeeze multiple occurrences of spaces. this works very nicely with the exception that the output file has a single leading space at the beginning of each sentence. is there a better way of doing this or of finishing this job?

Could you take another shot at describing the problem? Do you perhaps mean leading spaces in the front of each *paragraph* (that is, indenting) rather than at the beginning of each *sentence*? Normal writing, like your message and my reply, has a space in front of each sentence that is *not* the start of a new paragraph (that is, a space between the prior sentence's closing punctuation and the first character of the new sentence), so I'm having trouble seeing that pattern as a problem needing correcting.

-IF- you do mean paragraph, and -IF- the text skips a line between paragraphs (and if it uses Unix-style newlines), I would look for the pattern "\n\n " and replace it with "\n\n" (after you have done the multi-to-single-space pass, of course). If it does not skip lines, then "\n " to "\n" should work. tr should be able to do this (I know Perl's tr command, which I've used much more then the standalone app, can do it).

--
-------------------------------------------"Never tell me the odds!"--------
Ray Olszewski -- Han Solo
Palo Alto, California, USA [email protected]
-------------------------------------------------------------------------------

-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

#2 lore raw
In reply to Matthew Stapleton

Matthew,

I don't know what you use as a text editor, but if it has a search and replace, you can readily remove multiple spaces. For example, I run emacs and use replace-string to replace a fixed number of multiple spaces at the beginning of each line with null.

This does not work with a single space at the start of each line, for replacing a single space with null removes the spaces between words. I don't know how to handle this situation.

Haines Brown

Quoted 14 lines

Date: Fri, 23 Aug 2002 23:21:28 -0700
X-Juno-Line-Breaks: 6-13
From: Matthew Stapleton <[email protected]>
Sender: [email protected]
X-Mailing-List: [email protected]

i have been formatting some texts for an online archive of e-texts. every once and a while i encounter a file that has leading spaces in front of each sentence. to remedy this problem i have used the 'tr' utility to squeeze multiple occurrences of spaces. this works very nicely with the exception that the output file has a single leading space at the beginning of each sentence. is there a better way of doing this or of finishing this job?

Quoted 7 lines

thanks

Matthew Stapleton
====================================
http://webpages.marshall.edu/~staple12
Resistance is Futile!
====================================

-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

#3 lore raw

Hi Matthew, Haines.

Quoted 8 lines
Quoted 7 lines

i have been formatting some texts for an online archive of e-texts. every once and a while i encounter a file that has leading spaces in front of each sentence. to remedy this problem i have used the 'tr' utility to squeeze multiple occurrences of spaces. this works very nicely with the exception that the output file has a single leading space at the beginning of each sentence. is there a better way of doing this or of finishing this job?

Quoted 8 lines

I don't know what you use as a text editor, but if it has a search and replace, you can readily remove multiple spaces. For example, I run emacs and use replace-string to replace a fixed number of multiple spaces at the beginning of each line with null.

This does not work with a single space at the start of each line, for replacing a single space with null removes the spaces between words. I don't know how to handle this situation.

There is an easy way to preprocess any text file such that all strings of multiple tabs or spaces are replaced with a single space, and all leading and trailing spaces are stripped. Use the following sequence...

    cat "$OLD" | tr -s '\t' ' ' | sed 's/^ //;s/ $//' > "$NEW"

...where "$OLD" is the file as it currently stands, and "$NEW" is the file to create with the modified version in it. For reference...

1. The command tr -s '\t' ' ' says to replace all sequences of spaces and/or tabs with a single space.

2. The command sed 's/^ //;s/ $//' says to remove a single leading and/or trailing space from each line.

...and the combination does exactly what you want.

Best wishes from Riley.

-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs