Search code examples
regexperlunixcut

How to cut each word in a file


Is there a command which can turn a tab delimited file to give just the first 4 letters of each word?

Eg. Turning this file

Jackal Poorest Kingship Twinkle
Viscount George Lizard
Stone Goose Elephant Yvonne Chicken
Gecko Amoeba
Richard

To this file

Jack Poor King Twin
Visc Geor Liza
Ston Goos Elep Yvon Chic
Geck Amoe
Rich

Thanks


Solution

  • Use substr to trim each word. Name the following trim.pl:

    #!/usr/bin/env perl
    
    use strict;
    use warnings;
    
    while (<>) {
        chomp;
        my @words = split /\s+/;
        my @trim;
        for my $word (@words) {
            push @trim, substr($word,0,4);
        }
        print join ' ', @trim;
        print "\n";
    }
    

    Run it as:

    cat names.txt | trim.pl
    

    Which outputs:

    Jack Poor King Twin
    Visc Geor Liza
    Ston Goos Elep Yvon Chic
    Geck Amoe
    Rich