Skip to main content
added 215 characters in body
Source Link
Joseph R.
  • 40.5k
  • 8
  • 113
  • 146

NoteNotes

  • Both of the above solutions will print the modified file(s) to standard output. To emulate an edit in place (i.e. edit the files themselves), add a -i switch:

     perl -pi -le '$. % 4 == 1 and print "Exp", ++$i' your_file
    
  • Proceed similarly for the other solution.

  • Make sure you don't put another switch directly after -i as it interprets anything after it as the extension to use in order to create a backup file.

  • I recommend you test without -i first and then add it if the results are satisfactory.

and similarly for the other solution. I recommend you test without -i first and then add it if the results are satisfactory.

Note

  • Both of the above solutions will print the modified file(s) to standard output. To emulate an edit in place (i.e. edit the files themselves), add a -i switch:

     perl -pi -le '$. % 4 == 1 and print "Exp", ++$i' your_file
    

and similarly for the other solution. I recommend you test without -i first and then add it if the results are satisfactory.

Notes

  • Both of the above solutions will print the modified file(s) to standard output. To emulate an edit in place (i.e. edit the files themselves), add a -i switch:

     perl -pi -le '$. % 4 == 1 and print "Exp", ++$i' your_file
    
  • Proceed similarly for the other solution.

  • Make sure you don't put another switch directly after -i as it interprets anything after it as the extension to use in order to create a backup file.

  • I recommend you test without -i first and then add it if the results are satisfactory.

added 215 characters in body
Source Link
Joseph R.
  • 40.5k
  • 8
  • 113
  • 146
  • The $. variable will not reset its value if you're processing multiple files at once. Obviously, this is not what you want. You can work around this by explicitly writing out the implicit loop created by -p in the above one-liner so you can manipulate the continue block.

     perl -le '
         while(<>){
             $. % 4 == 1 and print "Exp ",++$i
         } continue {
             print;             # PrintsPrint the current line
             if(eof){           # If the next line is the end-of-file
                 close ARGV if; eof;  # ClosingClose the current filehandle resetsto reset $.
                 $i = 0     ;   # Reset the experiment counter if you need
             }
         }
     ' list_of_your_files
    
  • Both of the above solutions will print the modified file(s) to standard output. To emulate an edit in place (i.e. edit the files themselves), add a -i switch (just pile on those switches! :) ):

     perl -pilepi -le '$. % 4 == 1 and print "Exp", ++$i' your_file
    

and similarly for the other solution. I recommend you test without -i first and then add it if the results are satisfactory.

  • The $. variable will not reset its value if you're processing multiple files at once. Obviously, this is not what you want. You can work around this by explicitly writing out the implicit loop created by -p in the above one-liner so you can manipulate the continue block.

     perl -le '
         while(<>){
             $. % 4 == 1 and print "Exp ",++$i
         } continue {
             print;             # Prints the current line
             close ARGV if eof; # Closing the filehandle resets $.
         }
     ' list_of_your_files
    
  • Both of the above solutions will print the modified file(s) to standard output. To emulate an edit in place (i.e. edit the files themselves), add a -i switch (just pile on those switches! :) ):

     perl -pile '$. % 4 == 1 and print "Exp", ++$i' your_file
    

and similarly for the other solution.

  • The $. variable will not reset its value if you're processing multiple files at once. Obviously, this is not what you want. You can work around this by explicitly writing out the implicit loop created by -p in the above one-liner so you can manipulate the continue block.

     perl -le '
         while(<>){
             $. % 4 == 1 and print "Exp ",++$i
         } continue {
             print;             # Print the current line
             if(eof){           # If the next line is the end-of-file
                 close ARGV ;   # Close the current filehandle to reset $.
                 $i = 0     ;   # Reset the experiment counter if you need
             }
         }
     ' list_of_your_files
    
  • Both of the above solutions will print the modified file(s) to standard output. To emulate an edit in place (i.e. edit the files themselves), add a -i switch:

     perl -pi -le '$. % 4 == 1 and print "Exp", ++$i' your_file
    

and similarly for the other solution. I recommend you test without -i first and then add it if the results are satisfactory.

added 7 characters in body
Source Link
Joseph R.
  • 40.5k
  • 8
  • 113
  • 146

If the number of data points (I'm assuming) in each of your experiments (again, I'm assuming) is always 4, you can use this Perl snippetone-liner:

perl -ple '$. % 4 == 1 and print "Exp", ++$i' your_file

How it works

  • The -p switch tells Perl to loop over the given file(s) line by line (by default) and print each line after executing whatever code you supply.
  • The -l switch tells Perl to automatically append a newline (by default) to each print statement.
  • The -e switch informs Perl that whatever comes after it is code to execute (the code that will be executed before each line of the file is printed).
  • The special variable $. holds the line number currently being processed. If that line number is congruent to 1 modulo 4 ($. % 4 = 1) it means that we need to insert Exp # before it.
  • So we test $. % 4 == 1 and if it's true, we print Exp ++$i\n (where \n was added by the -l switch). This works because an undefined variable is given a value of zero by Perl when it's first used by Perl as an integer.

To make it work with multiple files

  • The $. variable will not reset its value if you're processing multiple files at once. Obviously, this is not what you want. You can work around this by explicitly writing out the implicit loop created by -p in the above snippetone-liner so you can manipulate the continue block.

     perl -le '
         while(<>){
             $. % 4 == 1 and print "Exp ",++$i
         } continue {
             print;             # Prints the current line
             close ARGV if eof; # Closing the filehandle resets $.
         }
     ' list_of_your_files
    

Note

  • Both of the above solutions will print the modified file(s) to standard output. To emulate an edit in place (i.e. edit the files themselves), add a -i switch (just pile on those switches! :) ):

     perl -pile '$. % 4 == 1 and print "Exp", ++$i' your_file
    

and similarly for the other solution.

If the number of data points (I'm assuming) in each of your experiments (again, I'm assuming) is always 4, you can use this Perl snippet:

perl -ple '$. % 4 == 1 and print "Exp", ++$i' your_file

How it works

  • The -p switch tells Perl to loop over the given file(s) line by line (by default) and print each line after executing whatever code you supply.
  • The -l switch tells Perl to automatically append a newline (by default) to each print statement.
  • The -e switch informs Perl that whatever comes after it is code to execute (the code that will be executed before each line of the file is printed).
  • The special variable $. holds the line number currently being processed. If that line is congruent to 1 modulo 4 ($. % 4 = 1) it means that we need to insert Exp # before it.
  • So we test $. % 4 == 1 and if it's true, we print Exp ++$i\n (where \n was added by the -l switch). This works because an undefined variable is given a value of zero when it's first used by Perl as an integer.

To make it work with multiple files

  • The $. variable will not reset its value if you're processing multiple files at once. Obviously, this is not what you want. You can work around this by explicitly writing out the implicit loop created by -p in the above snippet so you can manipulate the continue block.

     perl -le '
         while(<>){
             $. % 4 == 1 and print "Exp ",++$i
         } continue {
             print;             # Prints the current line
             close ARGV if eof; # Closing the filehandle resets $.
         }
     ' list_of_your_files
    

If the number of data points (I'm assuming) in each of your experiments (again, I'm assuming) is always 4, you can use this Perl one-liner:

perl -ple '$. % 4 == 1 and print "Exp", ++$i' your_file

How it works

  • The -p switch tells Perl to loop over the given file(s) line by line (by default) and print each line after executing whatever code you supply.
  • The -l switch tells Perl to automatically append a newline (by default) to each print statement.
  • The -e switch informs Perl that whatever comes after it is code to execute (the code that will be executed before each line of the file is printed).
  • The special variable $. holds the line number currently being processed. If that line number is congruent to 1 modulo 4 ($. % 4 = 1) it means that we need to insert Exp # before it.
  • So we test $. % 4 == 1 and if it's true, we print Exp ++$i\n (where \n was added by the -l switch). This works because an undefined variable is given a value of zero by Perl when it's first used as an integer.

To make it work with multiple files

  • The $. variable will not reset its value if you're processing multiple files at once. Obviously, this is not what you want. You can work around this by explicitly writing out the implicit loop created by -p in the above one-liner so you can manipulate the continue block.

     perl -le '
         while(<>){
             $. % 4 == 1 and print "Exp ",++$i
         } continue {
             print;             # Prints the current line
             close ARGV if eof; # Closing the filehandle resets $.
         }
     ' list_of_your_files
    

Note

  • Both of the above solutions will print the modified file(s) to standard output. To emulate an edit in place (i.e. edit the files themselves), add a -i switch (just pile on those switches! :) ):

     perl -pile '$. % 4 == 1 and print "Exp", ++$i' your_file
    

and similarly for the other solution.

added 839 characters in body
Source Link
Joseph R.
  • 40.5k
  • 8
  • 113
  • 146
Loading
Source Link
Joseph R.
  • 40.5k
  • 8
  • 113
  • 146
Loading