Another way, reading all lines into an array and chomping them then printing the join of the shifted first word and a slice of however many subsequent words are required.
$ perl -Mstrict -Mwarnings -E ' open my $inFH, q{<}, \ <<EOF or die $!; The quick brown fox jumps over the lazy dog EOF chomp( my @words = <$inFH> ); close $inFH or die $!; my $n = shift || 2; say join q{ }, shift( @words ), @words[ 0 .. $n - 2 ] while scalar @words >= $n;' The quick quick brown brown fox fox jumps jumps over over the the lazy lazy dog $
$ perl -Mstrict -Mwarnings -E ' open my $inFH, q{<}, \ <<EOF or die $!; The quick brown fox jumps over the lazy dog EOF chomp( my @words = <$inFH> ); close $inFH or die $!; my $n = shift || 2; say join q{ }, shift( @words ), @words[ 0 .. $n - 2 ] while scalar @words >= $n;' 4 The quick brown fox quick brown fox jumps brown fox jumps over fox jumps over the jumps over the lazy over the lazy dog $
I hope this is of interest.
Cheers,
JohnGG
In reply to Re: Create n-grams from tokenized text file
by johngg
in thread Create n-grams from tokenized text file
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |