0

プログラムの 80 桁目以降に文字列と行番号を追加したい。貪欲な一致を使用して、行内のすべてを一致させ、接尾辞を追加するだけである場合に(.*)置き換えることができます。\1 suffixしかし、列 80 までの空白/スペースを埋めてからstring行番号を追加するにはどうすればよいですか#。私が使用するときsed -e "s/\(.*\)/\1 string/g" infile > outfile。サフィックスを追加することしかできませんが、列 80 の後に行番号を追加することはできません。unxutilを介してWindowsで利用可能なsed、gawkを使用しています。よろしくお願いします。

4

3 に答える 3

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

また

perl -pe 's/$/suffix$./g' your_file

注:80文字と言ったのは行末を意味していたと思います

于 2013-07-23T12:06:05.033 に答える
0

試す:

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 <>
于 2013-07-23T13:15:14.703 に答える
0

GNU のコード:

sed ':a s/^.\{1,79\}$/& /;ta;s/$/& suffix/;=' file|sed 'N;s/\(.*\)\n\(.*\)/\2 \1/'
于 2013-07-23T13:14:28.640 に答える