2016-08-12 7 views
-2

以前のPostに関連するテキスト。私はファイル内に存在するテキストに基づいていくつかの接頭辞を追加したかったのです。PerlまたはBash - プレフィックス、削除、スワップテキスト

私の次のジグソーパズル - テキストを少し操作して、しばらく掘り下げています。

マイファイルになりました示しています(行は異なるテキスト連絡いたします - すべてではない同じようなIMの上映を)

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 

私は

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

..だから私は接頭辞に必要 を必要とします"私は" ExtraText "を削除し、"と私たちは言う "" 私はそれぞれの行を追加する必要があります "を削除する必要があります!

ここでは教祖に感謝します。

+0

"ExtraText" 実際の文字列 "ExtraText"、または任意の文字列ですか? –

+0

入力ファイルはタブで区切られていますか? – ikegami

答えて

1

ここには、bashバージョンのソリューションがあります。私の結果はあなたの入力データとあなたの要求された出力に基づいています。入力データはこのコードのファイルinput.txtにあります。

#!/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.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 

結果:

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'! 
+0

遅れて申し訳ありません - ありがとうございました。 – Gripsiden

+0

問題ありません。お役に立てて嬉しいです。 – tale852150

0

ない場合ExtraTextは、このシナリオにはスペースを含めることはできません

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

は注意、

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

、入力ファイルがタブ区切り値で構成されてと仮定。

関連する問題