Skip to main content
edited body
Source Link
user373503
user373503
$n++ ;
$tmp = $tmp . $_  if $n < 11  ; 
print  $_ . $tmp  if $n == 11 ;
print    $_         if $n > 11  ;
$n++ ;
$tmp = $tmp . $_  if $n < 11  ; 
print  $_ . $tmp  if $n == 11 ;
print             if $n > 11  ;
$n++ ;
$tmp = $tmp . $_  if $n < 11  ; 
print  $_ . $tmp  if $n == 11 ;
print  $_         if $n > 11  ;
added 570 characters in body
Source Link
user373503
user373503

AWK (stolen from Ed (not the editor!))

NR < 11 { 
    buf[NR] = $0; next 
}
NR >=11 {
    print
    if (NR == 11) {
        for (i=1; i<11; i++) { print buf[i] }
    }
}

I used NR instead of incrementing n, and made the flow more explicit. Same "trick": the next simplifies downstream.


With perl -n

$n++ ;
$tmp = $tmp . $_  if $n < 11  ; 
print  $_ . $tmp  if $n == 11 ;
print             if $n > 11  ;

This is the best format. Symmetrical.


AWK (stolen from Ed (not the editor!))

NR < 11 { 
    buf[NR] = $0; next 
}
NR >=11 {
    print
    if (NR == 11) {
        for (i=1; i<11; i++) { print buf[i] }
    }
}

I used NR instead of incrementing n, and made the flow more explicit. Same "trick": the next simplifies downstream.


With perl -n

$n++ ;
$tmp = $tmp . $_  if $n < 11  ; 
print  $_ . $tmp  if $n == 11 ;
print             if $n > 11  ;

This is the best format. Symmetrical.

added 52 characters in body
Source Link
user373503
user373503

And as some script, which takes a file name and needs no option:

$lnum=11$want=11 ;
while (<>) {
    $n++ ;
    if ($n < $lnum$want) {  $sto .= $_  }
    else { # before $want: store line
        { $lowlines .= $_ ;  
          next }               # lnum isnext reached
line (avoids 'else')   
    if ($n == $lnum$want) { print $_ . $sto  }      # at line #$want: append stored lines
  to $_     
  else      { $_ .= $lowlines }   {
    print $_;  }                # just print# (neitherprint <lnum$_ norfor ==lnum)
$n not less than }$want
}

An if less but a nested else more.

And as some script:

$lnum=11 ;
while (<>) {
    $n++ ;
    if ($n < $lnum) {  $sto .= $_  }
    else {                                            # lnum is reached
        if ($n == $lnum) { print $_ . $sto  }         # append stored lines
         else             { print $_  }                # just print (neither <lnum nor ==lnum)
    }
}

An if less but a nested else more.

And as some script, which takes a file name and needs no option:

$want=11 ;
while (<>) {
    $n++ ;
    if ($n < $want)            # before $want: store line
        { $lowlines .= $_ ;  
          next }               # next line (avoids 'else')   
    if ($n == $want)           # at line $want: append stored lines to $_     
        { $_ .= $lowlines }   
    print ;                    # print $_ for $n not less than $want
}
now the perl script looks nice
Source Link
user373503
user373503
Loading
added 444 characters in body
Source Link
user373503
user373503
Loading
added 117 characters in body
Source Link
user373503
user373503
Loading
added 56 characters in body
Source Link
user373503
user373503
Loading
added 634 characters in body
Source Link
user373503
user373503
Loading
added 692 characters in body
Source Link
user373503
user373503
Loading
added 692 characters in body
Source Link
user373503
user373503
Loading
added 217 characters in body
Source Link
user373503
user373503
Loading
Source Link
user373503
user373503
Loading