• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 389
  • Last Modified:

Append word

Hello,

I'm looking for a way to append a word at the end of each record which does not contain specific words (i.e. store or rocket)

example

store rocket went
phone laptop

output (append a word - in this case I specify store)
store rocket went
phone laptop store

Thank you

0
faithless1
Asked:
faithless1
  • 3
1 Solution
 
Kim RyanIT ConsultantCommented:
Try this
if ( $line !~ /\bstore\b/ or $line !~ /\brocket\b/)  # key word not found, \b means search on word boundary
{
    $line .= ' store'; # append store to string
}
0
 
a1jCommented:
#!/usr/bin/perl
while (<IN>) {
   chomp;
   $_ = $_ . ' appendword1' if /presentword1/;
   $_ = $_ . ' appendword2' if /presentword2/;
   print "$_\n";
}

==========
cat infile.txt | ./thisscript.pl > outfile.txt
0
 
a1jCommented:
sorry replace <IN> with <> in above's answer
0
 
faithless1Author Commented:
a1j - Thank you! ! worked perfectly

One other question is what tweak would i need to make to append the word before matches.

Thanks again

0
 
a1jCommented:
Just replace
$_=$_.'word'
with
$_='word'.$_
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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