Sponsored Content
Top Forums Shell Programming and Scripting perl - how come this script works? Post 302136329 by mjays on Monday 17th of September 2007 06:28:19 AM
Old 09-17-2007
perl - how come this script works?

Code:
#!/usr/bin/perl
open (DATA, file.txt);
@array = <DATA>;
close (DATA);

open (DATA, ">$file.txt");
for (@array) {
      s/text/replace text/;
      push(@contents,$_);
}
seek(DATA, 0, 0);
print DATA (@contents);

close(DATA);

could someone please explain how this works. i've been trying for awhile now to find out how to open a file, replace some text and then save the file. i eventually found the code above.

however, as far as i knew 'push' placed an item onto the end of an already defined array, but in the code above @contents isn't defined.

what's going on?

regards,

matt
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script works with bash 3.0 but not 3.2.

Hello, So my knowledge of bash scripting is not that great and I have been trying to solve this problem on my own for awhile to no avail. Here's the error I get when running it with an OS that uses bash 3.2.x: testagain.sh: line 10: *-1: syntax error: operand expected (error token is... (2 Replies)
Discussion started by: forkandspoon
2 Replies

2. Shell Programming and Scripting

perl oneliner not works .pl script

I am trying to take first 3 columns in a file which matches the word "abc", but i am getting the below error, <error> Global symbol "@F" requires explicit package name at ./new.pl </error> whereas when i give the below,grep abc /home/test/file.txt|perl -lane 'print \"$F $F $F\" in unix prompt... (4 Replies)
Discussion started by: anspks
4 Replies

3. Shell Programming and Scripting

perl/unix: script in command line works but not in perl

so in unix this command works works and shows me a list of directories find . -name \*.xls -exec dirname {} \; | sort -u | > list.txt but when i try running a perl script to run this command my $query = 'find . -name \*.xls -exec dirname {} \; | sort -u | > list.txt';... (2 Replies)
Discussion started by: kpddong
2 Replies

4. Shell Programming and Scripting

Script works but doesn't?

Hi everyone I'm new here so and I'm just starting to learn a bit of Solaris and I'm working on repairing 10 year old scripts for our system here at work. When I execute the commands at my prompt everything go's through smooth. I'm using gedit to edit my code because I'm still getting used to the... (4 Replies)
Discussion started by: 82280zx
4 Replies

5. Shell Programming and Scripting

perl: Command works in terminal, but not in shell script

Hi, the following command works in the terminal no problem. samtools view -h rawlib.bam | perl -ne '{ @line = split( /\s+/ ); $match = 0; while( $line =~ /(\d+)M/g ) { $match = $match + $1 } if( $match >= 80 || $_ =~ /^\@/ ) { print $_ } }' | java -Xmx12G -jar... (8 Replies)
Discussion started by: jdilts
8 Replies

6. Shell Programming and Scripting

Script almost works... one error

Helo, i have written a bash script for running my calculations. anyway, according to shellcheck the error is somewhere in the while loop or in the if condition. so the idea of this script is: submit a job with a name. while it is in the queue it has a job ID number. after the job is done i... (2 Replies)
Discussion started by: carborane
2 Replies

7. Shell Programming and Scripting

Perl error in batch command but works one at a time

In the below perl executes if one file is processed perfect. However, when multiple files are processed in batch which is preferred I get the below error that I can not seem to fix it as the '' necessary for the command to execute, but seem to only work for one -arg option. Thank you :). ... (2 Replies)
Discussion started by: cmccabe
2 Replies

8. Shell Programming and Scripting

Need to understand how the line in perl program works as shell commend

I have a file with two line, one is header, the other actual value: TYPCD|ETID2|ETID|LEG ID|PTYP|PTYP SUB|TRD STATUS|CXL REASON|CACT|CACTNM|ENCD|ENC D NM|TRDR|ASDT|TRDT|MTDT|STDT|LS|SECID|SECID TYP|SECNM|PAR|STCC|MARKET PRICE|DIS MARKET PRICE|MARKET PRICE CURRENCY|SRC OF SETTLEMENT... (2 Replies)
Discussion started by: digioleg54
2 Replies

9. Shell Programming and Scripting

Shell script works fine as a standalone script but not as part of a bigger script

Hello all, I am facing a weird issue while executing a code below - #!/bin/bash cd /wload/baot/home/baotasa0/sandboxes_finance/ext_ukba_bde/pset sh UKBA_publish.sh UKBA 28082015 3 if then echo "Param file conversion for all the areas are completed, please check in your home directory"... (2 Replies)
Discussion started by: ektubbe
2 Replies

10. Shell Programming and Scripting

Script works, but I think it could be better and faster

Hi All, I'm new to the forum and to bash scripting. I did some stuff with VB.net, Batch, and VBScripting in the past, but because I shifted over to Linux, I am learning to script in Bash at this moment. So bear with me if I seem to script like a newbie, that's just because I am ;-) OK, I... (9 Replies)
Discussion started by: cornelvis
9 Replies
Output(3pm)						User Contributed Perl Documentation					       Output(3pm)

NAME
SGMLS::Output - Stack-based Output Procedures SYNOPSIS
use SGMLS::Output; To print a string to the current output destination: output($data); To push a new output level to the filehandle DATA: push_output('handle',DATA); To push a new output level to the file "foo.data" (which will be opened and closed automatically): push_output('file','foo.data'); To push a new output level to a pipe to the shell command "sort": push_output('pipe','sort'); To push a new output level appending to the file "foo.data": push_output('append','foo.data'); To push a new output level to an empty string: push_output('string'); To push a new output level appending to the string "David is ": push_output('string',"David is "); To push a new output level to The Great Beyond: push_output('nul'); To revert to the previous output level: pop_output(); To revert to the previous output level, returning the contents of an output string: $data = pop_output(); DESCRIPTION
This library allows redirectable, stack-based output to files, pipes, handles, strings, or nul. It is especially useful for packages like SGMLS, since handlers for individual SGML elements can temporarily change and restore the default output destination. It is also particu- larly useful for capturing the contents of an element (and its sub-elements) in a string. Example: sgmls('<title>', sub{ push_output('string'); }); sgmls('</title>', sub{ $title = pop_output(); }); In between, anything sent to output (such as CDATA) will be accumulated in the string returned from pop_output(). Example: sgmls('<tei.header>', sub { push_output('nul'); }); sgmls('</tei.header>', sub { pop_output(); }); All output will be ignored until the header has finished. AUTHOR AND COPYRIGHT
Copyright 1994 and 1995 by David Megginson, "dmeggins@aix1.uottawa.ca". Distributed under the terms of the Gnu General Public License (version 2, 1991) -- see the file "COPYING" which is included in the SGMLS.pm distribution. SEE ALSO
: SGMLS. perl v5.8.8 2004-02-22 Output(3pm)
All times are GMT -4. The time now is 09:29 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy