Perl 使用正则表达式在80列后添加后缀

Perl 使用正则表达式在80列后添加后缀,perl,sed,awk,notepad++,gawk,Perl,Sed,Awk,Notepad++,Gawk,我想在程序的第80列之后添加一个字符串和行号。我可以使用贪婪匹配(.*)匹配一行中的所有内容,如果我只需要添加后缀,就可以用\1后缀替换它。但是,如何将空格/空格填充到第80列,然后添加string和行号#。当我使用sed-e“s/\(.*\)/\1字符串/g“infle>outfile时。我只能添加后缀,但不能在第80列之后添加,并且没有行号。我正在使用sed,可以通过unxutil在windows上使用gawk。先谢谢你 awk '{$0=$0"suffix"NR}1' your_file

我想在程序的第80列之后添加一个字符串和行号。我可以使用贪婪匹配
(.*)
匹配一行中的所有内容,如果我只需要添加后缀,就可以用
\1后缀替换它。但是,如何将空格/空格填充到第80列,然后添加
string
和行号
#
。当我使用
sed-e“s/\(.*\)/\1字符串/g“infle>outfile
时。我只能添加后缀,但不能在第80列之后添加,并且没有行号。我正在使用sed,可以通过unxutil在windows上使用gawk。先谢谢你

awk '{$0=$0"suffix"NR}1' your_file


注意:当您说GNU的代码为80个字符时,我想您的意思是行尾:

尝试:

更新:

添加一些选项

Usage: script.pl [-start=1] [-end=0] [-pos=80] [-count=1] <file> ...

为什么要使用正则表达式呢?我想你说的80个字符是指行尾。@米希克还有其他选择吗?你想如何处理超过80个字符的行?@Adrianhh或者任何一行的字符数都不能超过80,或者这些行不能是代码行,而应该是注释行。如果有超过80个字符的行,则这些行可以断开并在第80列插入后缀和行号,或者在末尾添加后缀和行号。两者都可以接受。第一个选项在后缀1816OD(ICASE,3)的开头添加后缀
然后添加后缀1817D NO
。第二个在行首和行尾添加<代码>SUFFEX1840C CT=0.0SUFFEX1841 SUFFEX1841C RT=0.0SUFFEX1842 SUFFEX1842C RETURNSUFFEX1843
。这两个都不是从第80列开始的。@sencert…显示您的示例文件!它起作用了。我很抱歉要添加额外的内容,但请您解释一下,如果我只需要处理文件中有限的行数,例如1003到2034,我需要做哪些更改。即脚本在文件的第1003到2034行中添加
后缀
和行号1到1031。我是否可以将行号中的数字(如00001)改为1。非常感谢。如果我需要单独提问,只处理有限数量的行,请指导我。谢谢。你需要比一行更复杂的东西。见以上编辑。@captha。它没有添加行号。@sincerenice是的,你完全正确:我忘记了行号:(因此我现在添加了这个:)@captha它工作正常。你能解释一下发生了什么事吗?(我在sed中主要使用s///g loke命令)。
sed ':a s/^.\{1,79\}$/& /;ta;s/$/& suffix/;=' file|sed 'N;s/\(.*\)\n\(.*\)/\2 \1/'
perl -ple's{\A(.*)\z}{$1.(" "x(80-length($1)))." # $."}ex'
Usage: script.pl [-start=1] [-end=0] [-pos=80] [-count=1] <file> ...
#!/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 <>