与之前的Post相关。我想根据文件中出现的文字添加一些前缀。
我的下一个拼图 - 我需要稍微操纵文字并花费一些时间来挖掘。
我的文件现在显示:(这些行将联系不同的文本 - 与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”并替换为“and we say'” 我需要在每行附加“'!”
先谢谢这里的大师。
答案 0 :(得分: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'!
答案 1 :(得分:0)
假设输入文件由制表符分隔的值组成,
while (<>) {
chomp;
my @fields = split /\t/;
print("This place is called $fields[0] and we say \"$fields[2]\"!\n");
}
如果没有,
while (<>) {
my ($name, $text) = /^(\S+)\s+\S+\s+(.*)/;
print("This place is called $name and we say \"$text\"!\n");
}
请注意,ExtraText
在此方案中不能包含空格。