-2

I am looking to programmatically edit the newlines of .txt files. The desired behavior is that any single newline in between lines of text will become two newlines.

edit (clarification by @kaan): Lines separated by one newline should be separated by two newlines. Any lines that are already separated by two or more lines should be left as is

edit (context): I am working with the .fountain syntax and an npm module called afterwriting that exports text files into a script format as a pdf. lines of text separated by only one new line do not properly space when printed to pdf using the package. So i want to automatically convert single newlines into double, because i also don't want to have to add two new lines in all of the files i am converting

For instance an example of an input would look like:

File with text in it

A new line

Another new line



Line with three new lines above

One last new line

would become

File with text in it


A new line


Another new line



Line with three new lines above


One last new line

Any ideas of how this could be achieved in a bash script would be appreciated

Grady Woodruff
  • 143
  • 2
  • 11
  • If there are already 2 new lines, make it 4? – Guru Dec 27 '19 at 12:01
  • 1
    It looks like two lines separated by _one_ newline should then be separated by _two_. Any lines that are already separated by two (or more) lines should be left as is. – Kaan Dec 27 '19 at 16:47
  • This is not a well defined problem. Are you just looking to replace runs of exactly two newlines with 3 newlines, or do you also want to replace any isolated blank line (that is, a line containing nothing but whitespace) with 2 blank lines? That is, do you treat `^$` differently than `^\s+$`? Be precise about the question, and the answer becomes rather obvious. – William Pursell Dec 27 '19 at 17:26
  • @WilliamPursell sorry, I didn't explain this well at all. kaan is correct. I am editing the og post for clarification and context – Grady Woodruff Dec 27 '19 at 22:11
  • What have you tried? What is the error message? – ceving Dec 27 '19 at 22:14

2 Answers2

1

Here is a way to do it using sed:

  • read the whole file (since normal sed behavior will remove all newlines)
  • look for a word boundary (\b) followed by two newlines (\n\n – one for ending the current line, then one that's the single blank line), then one more word boundary (\b)
  • for any matches, add one extra newline in there.

With your sample text inside data.txt, it looks like this:

sed -n 'H; ${x; s/\b\n\n\b/\n\n\n/g; p}' < data.txt | tail -n +2

(Edit: added | tail -n +2 to remove the extra newline that's inserted at the beginning)

Kaan
  • 5,434
  • 3
  • 19
  • 41
  • You're printing 1 more blank line at the beginning. – dibery Dec 27 '19 at 17:28
  • just updated the example. I hadn't noticed the extra newline at the start, and in a quick scan I don't see any obvious issues with the `sed` command, so I tacked `tail` onto the end. – Kaan Dec 28 '19 at 03:21
  • @kaan when i run it as above, I get the error `sed: 1: "H; ${x; s/\b\n\n\b/\n\n ...": extra characters at the end of p command`. I tried adding a semi-colon after the `p`, like `sed -n 'H; ${x; s/\b\n\n\b/\n\n\n/g; p;}' < test.txt | tail -n +2` but it just prints the file and doesn't alter anything – Grady Woodruff Dec 29 '19 at 18:03
  • what OS are you using? Mac OS X uses a [BSD version of `sed`](https://unix.stackexchange.com/questions/13711/differences-between-sed-on-mac-osx-and-other-standard-sed) by default which doesn't support various things (as opposed to GNU `sed`). – Kaan Dec 29 '19 at 20:04
  • @kaan Thank you, I wasn't aware of this difference. I installed gsed (http://gridlab-d.sourceforge.net/wiki/index.php/Mac_OSX/Gsed) and now it works perfectly – Grady Woodruff Dec 30 '19 at 01:53
1

This might work for you (GNU sed):

sed '/\S/b;N;//{P;b};:a;n;//!ba' file

This solution appends another line to the first empty line encountered. If the appended line is not empty it prints the first and bails out, thus doubling the empty line. Otherwise if the appended line is empty, it print them both and then prints any further empty lines until it encounters a non-empty line.

potong
  • 55,640
  • 6
  • 51
  • 83
  • I get the error `extra characters at the end of g command`. I tried some variations of adding semi-colons to attempt to fix the error but none made it work – Grady Woodruff Dec 28 '19 at 01:51
  • that's what i'm unsure of also. Researching that error message brought me a lot of things, but no clarity on why it would show for this command – Grady Woodruff Dec 29 '19 at 18:04
  • Sorry, I wasn't aware the OSX version of sed was different, as @kaan recommended in the other answer I installed gsed (gridlab-d.sourceforge.net/wiki/index.php/Mac_OSX/Gsed and now this works perfectly, thank you – Grady Woodruff Dec 30 '19 at 01:59