我想在程序中的第80列之后添加字符串和行号。我可以使用贪婪匹配(.*)
来匹配一行中的所有内容,并用\1 suffix
替换它。如果我只需要添加后缀。但是,如何填充空格/空格到列80,然后添加string
,然后添加行号#
。当我使用sed -e "s/\(.*\)/\1 string/g" infile > outfile
。我只能添加后缀,但不能在第80列之后添加,并且没有行号。我通过unxutil在Windows上使用sed,gawk。提前感谢你。使用正则表达式在80列之后添加后缀
回答
尝试:
perl -ple's{\A(.*)\z}{$1.(" "x(80-length($1)))." # $."}ex'
更新:
添加一些选项。
Usage: script.pl [-start=1] [-end=0] [-pos=80] [-count=1] <file> ...
script.pl
:
#!/usr/bin/env perl
# --------------------------------------
# Pragmatics
use v5.8.0;
use strict;
use warnings;
# --------------------------------------
# Modules
# Standard modules
use Getopt::Long;
use Data::Dumper;
# Make Data::Dumper pretty
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Indent = 1;
# Set maximum depth for Data::Dumper, zero means unlimited
local $Data::Dumper::Maxdepth = 0;
# --------------------------------------
# Configuration Parameters
# Command line arguments
my %Cmd_options = (
count => 1, # where to start the line counting
end => 0, # line to end on, zero means to end of file
pos => 80, # where to place the line number
start => 1, # which line to start on
);
my %Get_options = (
'count=i' => \$Cmd_options{ count },
'end=i' => \$Cmd_options{ end },
'pos=i' => \$Cmd_options{ pos },
'start=i' => \$Cmd_options{ start },
);
# conditional compile DEBUGging statements
# See http://lookatperl.blogspot.ca/2013/07/a-look-at-conditional-compiling-of.html
use constant DEBUG => $ENV{DEBUG};
# --------------------------------------
# Variables
# --------------------------------------
# Subroutines
# --------------------------------------
# Name: get_cmd_opts
# Usage: get_cmd_opts();
# Purpose: Process the command-line switches.
# Returns: none
# Parameters: none
#
sub get_cmd_opts {
# Check command-line options
unless(GetOptions(
%Get_options,
)){
die "usage: number_lines [<options>] [<file>] ...\n";
}
print Dumper \%Cmd_options if DEBUG;
return;
}
# --------------------------------------
# Main
get_cmd_opts();
while(my $line = <>){
# is the line within the range?
if($. >= $Cmd_options{start} && $Cmd_options{end} && $. <= $Cmd_options{end}){
chomp $line;
my $len = length($line);
printf "%s%s # %05d\n", $line, q{ } x ($Cmd_options{pos} - $len), $Cmd_options{count};
$Cmd_options{count} ++;
# else, just print the line
}else{
print $line;
} # end if range
} # end while <>
它完成了这项工作。我很抱歉添加额外的东西,但是请你解释一下,如果我需要处理文件中只有有限数量的行,例如1003到2034,我需要做些什么修改。这是脚本添加'后缀'和行号在文件的行1003至2034中的1至1031。我是否也可以修复行号中的数字,如00001而不是1.非常感谢。 –
请指导我如果我需要单独提出处理有限数量的行的问题。谢谢。 –
你将需要比单线更复杂的东西。请参阅上述编辑。 – shawnhcorey
awk '{$0=$0"suffix"NR}1' your_file
或
perl -pe 's/$/suffix$./g' your_file
注:我假设你的意思行结束时,你说的80个字符
第一个选项在开始后缀后缀1816OD(ICASE,3).EQ.0)THEN 后缀1817D NO'。第二个添加在行的开始和结束。 '后缀1840C CT = 0.0suffix1841 后缀1841C RT = 0.0suffix1842 后缀1842C RETURNsuffix1843'。两者都没有做从列80. –
@sincere ...显示您的示例文件! – Vijay
- 1. Java正则表达式前缀后缀
- 2. 在使用jquery正则表达式之后添加diez标记
- 3. 后缀header_checks在正则表达式
- 4. Java的正则表达式后缀
- 5. 删除后缀的正则表达式
- 6. 正则表达式,负后缀
- 7. 正则表达式:后缀验证
- 8. python正则表达式后缀匹配
- 9. 追加后缀使用PowerShell和正则表达式
- 10. 正则表达式如何找到“unconsecutive后缀”的后缀
- 11. 简单的正则表达式文本替换然后添加后缀
- 12. 正则表达式验证前缀,中缀和后缀表达式
- 13. 如何使用正则表达式在特定前缀之后返回文本?
- 14. 使用正则表达式来自动标上序号后缀
- 15. XSLT正则表达式 - 之后没有
- 16. 加载后正则表达式使用正则表达式Regexp :: Grammars
- 17. 使用正则表达式为特殊字符添加前缀
- 18. 正则表达式前缀和后缀可选
- 19. 在后缀表达式使用$
- 20. 正则表达式在字符串之前和之后添加换行符?
- 21. 添加使用正则表达式
- 22. 正则表达式在Perl中的新行之后加工
- 23. 在正则表达式中转义文件名后缀?
- 24. 在正则表达式中匹配后缀s和ed - javascript
- 25. 正则表达式在比赛前后添加文本。 vb.net
- 26. 在数字前后添加字符的正则表达式
- 27. 后缀添加到列表用foreach
- 28. 如何在regex模式之前和之后的正则表达式搜索后添加换行符返回?
- 29. 使用Java将中缀表达式转换为前缀和后缀表达式
- 30. 如何使正则表达式允许可选的前缀和后缀提取
为什么要使用正则表达式呢? – mishik
我假设你的意思是当你说80个字符时行尾。 – Vijay
@MISHIK其他选项有哪些? –