Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 247
  • Last Modified:

Appending Words

Hello,

I'm looking for a way to append a word at the end of each record which does not contain a specific word.

example:

shopping best store
shoppingrandomstore
expertshopping
expert shopping

ouput: Append "expert"


shopping best store expert
shoppingrandomstore expert
expertshopping
expert shopping


I'm also looking for a way to specify where to append a word ( beginning or end)

Thanks
0
faithless1
Asked:
faithless1
1 Solution
 
ozoCommented:
$word="expert";
while( <DATA> ){
   s/$/ $word/ unless /$word/;
   print;
}
__DATA__
shopping best store
shoppingrandomstore
expertshopping
expert shopping
0
 
wilcoxonCommented:
This should do it...

It allows you to pass in the file to use for input plus optionally the word to use and the location to put it.
#!/usr/local/bin/perl

use strict;
use warnings;

my $fil = shift or die "Usage: $0 file [word [begin_end]]\n";
# default to expert
my $wrd = shift || 'expert';
# default to end
# use start or beg* to put at beginning of line
my $loc = shift || 'end';

open IN, $fil or die "could not open $fil: $!";
while (<IN>) {
    chomp;
    ($loc =~ m{^(?:beg|start$)} and s{^}{$wrd }) or s{$}{ $wrd}
        unless m{$wrd};
    print $_, "\n";
}
close IN;

Open in new window

0
 
faithless1Author Commented:
Thanks!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now