2016-08-12 2 views
-2

에 해당하며 앞의 Post과 관련 있습니다. 파일에있는 텍스트를 기반으로 일부 접두사를 추가하고 싶습니다.Perl 또는 Bash - 접두사, 제거, 스왑 텍스트는

다음 퍼즐 조각 - 텍스트를 약간 조작하고 잠시 파고 들었습니다.

내 파일은 이제 보여줍니다 (선이 텍스트를 다른 연락을 드릴 것입니다 - 모든 같은 같은 메신저를 보여주는) .. 내가

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

그래서 필요

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 

나는 접두사 필요 lines "ExtraText"를 제거하고 "and say we say '"라고 말하십시오. 각 줄에 "!"을 추가해야합니다.

앞서 전문가에게 미리 감사드립니다. ExtraText이 시나리오에서 공백을 포함 할 수 있음이

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

참고

+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

입력 파일을 가정하면, 탭 - 구분 된 값으로 구성

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

아니라면.