user1094066
user1094066

Reputation: 11

batch or perl reshaping a txt file

I would like to write a script that reshapes a text file that looks like this:

word1
word2
word3
word4

word5
word6
word7
word8

word9
word10
word11
word12
...

into this:

word1 word2 word3 word4
word5 word6 word7 word8
word9 word10 word11 word12
...

Does anyone know of an easy way to do so?

Upvotes: 1

Views: 172

Answers (3)

Axeman
Axeman

Reputation: 29854

perl -l -00 -aF/\s*\n\s*/ -ne 'print "@F"' /source/file > /dest/file

Explanation (using TLP's suggestions)

  • -l: auto-chomp records and makes print act like say.
  • -00: puts perl into "paragraph mode" and it looks for successive "\n\n"
  • -a: auto-split records into fields array @F
  • -F: use the following pattern to split the record (using auto-split).
  • -ne: execute the following for each record (delimited by "\n\n"), only print when told

Upvotes: 1

TLP
TLP

Reputation: 67910

perl -i.bak -pwe 's/\S\s*\K\n$/ /' input.txt
  • -i.bak saves a backup in input.txt.bak
  • -p puts a while (<>) loop around your script
  • The regex looks for a line that ends with a non-whitespace character not followed by whitespace, followed by newline and replaces the newline with a space. The \K escape means "keep anything before this".

Another option:

perl -i.bak -lpwe 'BEGIN { $/="" }; s/\n/ /g' input.txt

Using paragraph mode in the BEGIN block, and following each print with a newline with -l

Upvotes: 2

JRFerguson
JRFerguson

Reputation: 7526

It appears that your groups are "paragraphs" so read them as such; split the words on whitespace; rejoin them using a single space (blank) and print the result:

#!/usr/bin/env perl
use strict;
use warnings;
local $/ = '';
my @words;
while (<DATA>) {
    @words = split;
    print join q( ), @words, "\n";
    @words = ();
}
__DATA__
word1
word2
word3
word4

word5
word6
word7
word8

word9
word10
word11
word12

Upvotes: 1

Related Questions