-2

Related to a previous Post. I wanted to add some prefix based on text occurring in a file.

My next piece of the jigsaw - I need to manipulate text a little and have spent a while digging around.

My file now shows: ( the lines will contact differing text - not all the same like im showing )

Denver.Line 1   ExtraText  I need this information
Denver.Line 2   ExtraText  I need this information
Denver.Line 3   ExtraText  I need this information
New York.Line 1   ExtraText  I need this information 
New York.Line 2   ExtraText  I need this information

I need

The place is called Denver.Line 1 and we say "I need this information"!
The place is called Denver.Line 2 and we say 'I need this information'!
The place is called Denver.Line 3 and we say 'I need this information'!
The place is called New York.Line 1 and we say 'I need this information'!
The place is called New York.Line 2 and we say 'I need this information'!

So.. I need to prefix the lines I need to remove " ExtraText " and replace with "and we say'" I need to append each line with "'!"

Thanks in advance to the gurus here.

Community
  • 1
  • 1
Gripsiden
  • 467
  • 2
  • 15

2 Answers2

1

Here is a bash version of a solution. My results are based on your input data as given and your requested output. The input data is in the file input.txt for this code.

#!/bin/bash

while IFS='.' read text1 text2
do
  set -f
  textarr=($text2)
  echo "The place is called $text1.${textarr[0]} ${textarr[1]} and we say '${textarr[3]} ${textarr[4]} ${textarr[5]} ${textarr[6]}'!"
done < input.txt

Input data (i.e. file input.txt):

Denver.Line 1   ExtraText  I need this information
Denver.Line 2   ExtraText  I need this information
Denver.Line 3   ExtraText  I need this information
New York.Line 1   ExtraText  I need this information
New York.Line 2   ExtraText  I need this information

Results:

The place is called Denver.Line 1 and we say 'I need this information'!
The place is called Denver.Line 2 and we say 'I need this information'!
The place is called Denver.Line 3 and we say 'I need this information'!
The place is called New York.Line 1 and we say 'I need this information'!
The place is called New York.Line 2 and we say 'I need this information'!
tale852150
  • 1,618
  • 3
  • 17
  • 23
0

Assuming the input file consists of tab-separated values,

while (<>) {
   chomp;
   my @fields = split /\t/;
   print("This place is called $fields[0] and we say \"$fields[2]\"!\n");
}

If not,

while (<>) {
   my ($name, $text) = /^(\S+)\s+\S+\s+(.*)/;
   print("This place is called $name and we say \"$text\"!\n");
}

Note that ExtraText can't contain spaces in this scenario.

ikegami
  • 367,544
  • 15
  • 269
  • 518