Appending Words

Posted on 2011-05-07
Medium Priority
Last Modified: 2012-05-11

I need to append words from a file across another file by specifying columns to where append the data.


went to the store
came back from the store

If 0 is specified in a script, then it would be ideal to have input rows to be appended at the beginning of each line. If 1 is specified, then input rows would be appended after 1st word etc. In case where a row does not contain a column before or after which to append the word, it can be ignored.

Thank you
Question by:faithless1
LVL 85

Expert Comment

ID: 35714261
So in this case, would you want the result for 0 to be
Word1 went to the store
Word2 came back from the store
and if 1 is specified
went Word1 to the store
came Word2 back from the store

LVL 27

Accepted Solution

wilcoxon earned 2000 total points
ID: 35714264
This should do what you want...

use strict;
use warnings;
use Fcntl 'O_RDONLY';
use Tie::File;

# pass in the position to place the words or default to 0
my $loc = shift || 0;

tie my @in, 'Tie::File', 'input.txt', mode => O_RDONLY
    or die "could not tie input.txt: $!";
tie my @out, 'Tie::File', 'output.txt' or die "could not tie output.txt: $!";

for my $i (0..@in-1) {
    # don't append bare words to output if it is shorter
    if ($i < @out) {
        my @words = split /\s+/, $out[$i];
        # skip if the position is longer than the line
        next if ($loc >= @words);
        splice @words, $loc, 0, $in[$i];
        $out[$i] = join ' ', @words;

Open in new window


Author Closing Comment

ID: 35735481
Awesome, thank you

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Six Sigma Control Plans
Suggested Courses

862 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