Append word

Posted on 2011-04-24
Last Modified: 2012-05-11

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)


store rocket went
phone laptop

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

Thank you

Question by:faithless1
    LVL 19

    Expert Comment

    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
    LVL 4

    Accepted Solution

    while (<IN>) {
       $_ = $_ . ' appendword1' if /presentword1/;
       $_ = $_ . ' appendword2' if /presentword2/;
       print "$_\n";

    cat infile.txt | ./ > outfile.txt
    LVL 4

    Expert Comment

    sorry replace <IN> with <> in above's answer

    Author Comment

    a1j - Thank you! ! worked perfectly

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

    Thanks again

    LVL 4

    Expert Comment

    Just replace

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
    Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now