如何从文本文件中提取数字数据? [英] How can I extract numeric data from a text file?
问题描述
我希望Perl脚本从文本文件中提取数据并将其另存为另一个文本文件.文本文件的每一行都包含一个指向jpg的URL,例如" http ://pics1.riyaj.com/thumbs/000/082/104//small.jpg ".我希望脚本将每个jpg URL的后6个数字(即082104)提取到一个变量中.我希望将变量添加到新文本每一行的不同位置.
I want the Perl script to extract a data from a text file and save it as another text file. Each line of the text file contains an URL to a jpg like "http://pics1.riyaj.com/thumbs/000/082/104//small.jpg". I want the script to extract the last 6 numbers of each jpg URL, (i.e 082104) to a variable. I want the variable to be added to a different location on each line of the new text.
输入文字:
text http://pics1.riyaj.com/thumbs/000/082/104/small.jpg text
text http://pics1.riyaj.com/thumbs/000/569/315/small.jpg text
输出文字:
text php?id=82104 text
text php?id=569315 text
谢谢
推荐答案
到目前为止,您尝试了什么?
What have you tried so far?
这是一个简短的程序,可让您了解问题的实质,您可以添加其余部分:
Here's a short program that gives you the meat of the problem, and you can add the rest of it:
while( )
{
s|http://.*/\d+/(\d+)/(\d+).*?jpg|php?id=$1$2|;
print;
}
这与命令行程序非常接近,可以使用-p
开关为您处理循环和打印(请参见 perlrun 文档以获取详细信息):
This is very close to the command-line program the handles the looping and printing for you with the -p
switch (see the perlrun documentation for the details):
perl -pi.old -e 's|http://.*/\d+/(\d+)/(\d+).*?jpg|php?id=$1$2|' inputfile > outputfile
这篇关于如何从文本文件中提取数字数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!