英文:
Matching a number after a tab as one single line
问题
if ($linea =~ /^(.[a-z0-9]\d+.\d+)\t(.+)$/){
print $2, "\n";
}
英文:
I would like to know how to extract all the numbers after the ID (KC000001-3), including the number set after a tap using Perl regex.
The additional number (0.50) for the first ID, (0.60) second ID, and (0.70 0.80) third ID is always starting with a space as a new line and ending up with another tap.
Input file.
KC000001 0.30 0.40 0.50
KC000002 0.30 0.40 0.50 0.60
KC152363 0.30 0.40 0.50 0.60 0.70 0.80
I would like to get this output file.
0.30 0.40 0.50
0.30 0.40 0.50 0.60
0.30 0.40 0.50 0.60 0.70 0.80
I have prepared this regex.
if ($linea =~ /^(.[a-z0-9]\d+.\d)\s(.?)$/){
print $line
}
However, it is giving me the following error (it is not printing the number after the tab (0.50 for the first), (0.60 for the second), and (0.70 0.80 for the third))
0.30 0.40
0.30 0.40 0.50
0.30 0.40 0.50 0.60
I would like to know what is wrong with this regex. Is it possible to make it with a regex only?
Input file.
KC000001 0.30 0.40 0.50
KC000002 0.30 0.40 0.50 0.60
KC152363 0.30 0.40 0.50 0.60 0.70 0.80
Output file
0.30 0.40
0.30 0.40 0.50
0.30 0.40 0.50 0.60
答案1
得分: 3
使用Perl的一行代码:
0.30 0.40 0.50
0.30 0.40 0.50 0.60
0.30 0.40 0.50 0.60 0.70 0.80
这是一个数组切片操作,保留从第二列到最后一列的数据。
英文:
With a Perl one-liner:
$ perl -F"\t" -nE 'say join "\t", @F[1..$#F]' file | tee output_file
0.30 0.40 0.50
0.30 0.40 0.50 0.60
0.30 0.40 0.50 0.60 0.70 0.80
This is an array slice operation, to retain only column-2-till-the-end.
答案2
得分: 1
使用这个Perl单行命令:
perl -pe 's{^KC\w+\t}{}' infile > outfile
或者在原地修改文件:
perl -i.bak -pe 's{^KC\w+\t}{}' infile
这个Perl单行命令使用了以下命令行标志:
`-e`:告诉Perl在行内查找代码,而不是在文件中。
`-p`:逐行循环处理输入,将其默认赋值给`$_`。在每次循环迭代之后添加`print $_`。
`-i.bak`:在原地编辑输入文件(覆盖输入文件)。在覆盖之前,通过在文件名后添加扩展名`.bak`来保存原始文件的备份副本。如果要跳过写入备份文件,只需使用`-i`并跳过扩展名。
**另请参阅:**
* [`perldoc perlrun`:如何执行Perl解释器:命令行开关](https://perldoc.perl.org/perlrun.html#Command-Switches)
* [`perldoc perlre`:Perl正则表达式(regex)](https://perldoc.perl.org/perlre.html)
* [`perldoc perlrequick`:Perl正则表达式快速入门](https://perldoc.perl.org/perlrequick)
英文:
Use this Perl one-liner:
perl -pe 's{^KC\w+\t}{}' infile > outfile
or change the file in-place:
perl -i.bak -pe 's{^KC\w+\t}{}' infile
The Perl one-liner uses these command line flags:
-e
: Tells Perl to look for code in-line, instead of in a file.
-p
: Loop over the input one line at a time, assigning it to $_
by default. Add print $_
after each loop iteration.
-i.bak
: Edit input files in-place (overwrite the input file). Before overwriting, save a backup copy of the original file by appending to its name the extension .bak
. If you want to skip writing a backup file, just use -i
and skip the extension.
See also:
答案3
得分: 1
$line =~ s/^[^\t]*\t//;
print $line;
perl -pe's/^[^\t]*\t//'
参见 Specifying file to process to Perl one-liner。
英文:
This removes everything up to and including the first tab of each line:
$line =~ s/^[^\t]*\t//;
print $line;
As a one-liner:
perl -pe's/^[^\t]*\t//'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论