Gripsiden Gripsiden - 3 months ago 9
Perl Question

Perl Or Bash - Prefix , remove , swap text around

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.

Answer

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'!
Comments