Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl或Bash-前缀、删除、交换文本_Bash_Perl - Fatal编程技术网

Perl或Bash-前缀、删除、交换文本

Perl或Bash-前缀、删除、交换文本,bash,perl,Bash,Perl,与前科有关的。我想根据文件中出现的文本添加一些前缀 我的下一块拼图——我需要稍微操纵一下文本,并花了一段时间四处挖掘 我的文件现在显示:(行将接触不同的文本-与im显示的不完全相同) 我需要 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

与前科有关的。我想根据文件中出现的文本添加一些前缀

我的下一块拼图——我需要稍微操纵一下文本,并花了一段时间四处挖掘

我的文件现在显示:(行将接触不同的文本-与im显示的不完全相同)

我需要

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”并替换为“and we say'” 我需要在每一行后面加上“'!”


提前感谢这里的大师们

假设输入文件由制表符分隔的值组成

while (<>) {
   chomp;
   my @fields = split /\t/;
   print("This place is called $fields[0] and we say \"$fields[2]\"!\n");
}
while(){
咀嚼;
我的@fields=split/\t/;
打印(“这个地方叫$fields[0],我们说\“$fields[2]\”!\n”);
}
如果没有,

while (<>) {
   my ($name, $text) = /^(\S+)\s+\S+\s+(.*)/;
   print("This place is called $name and we say \"$text\"!\n");
}
while(){
我的($name,$text)=/^(\S+)\S+\S+\S+(.*)/;
打印(“这个地方叫$name,我们说\“$text\”!\n”);
}

请注意,
extractext
在此场景中不能包含空格。

这里是解决方案的
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
结果:

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”是实际字符串“ExtraText”还是任意字符串?输入文件选项卡是否分开?谢谢-抱歉延迟-不在办公室。没问题。很高兴我能帮忙。
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'!