Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm writing a Perl script. If the text in a specified column (here column 13) matches a certain text namely 'one' or 'two', then the whole line (so all columns) should be copied to another file. My input is a tab-delimited .txt file.
This is what I have so far:
my $table1 = $ARGV[0];
open(my $variants,$table1) || die "$! $table1";
open(my $out,'>',"filtered.txt") || die "Can't write new file: $!";
while(<$variants>){
chomp;
my #line=split(/\t/); #split on tabs
if (($line[12] =~ m/one/) || ($line[12] =~ m/two/)){
print $out "$_";
}
}
Since I'm getting a 'use of uninitialized value' error, I wanted to know what needs to be changed in this code.
What is the problem?
perl -F'\t' -ane'print if $F[11]=~/one|two/' input > output
This is a great example of a program that should be written using the Unix filter model.
#!/usr/bin/perl
use strict;
use warnings;
while (<>) {
my #line = split /\t/;
# Do you mean 12? That's the 13th field
print if $line[12] =~ /one/ || $line[12] =~ /two/;
}
Simpler to write and easier to understand. Oh, and far more flexible (no hardcoded filenames).
Call it like this:
$ ./my_filter < input_file.txt > output_file.txt
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
Wondering if it's possible to write a short script that prints the code itself out in the end with the lines counted. Even something simple like a few lines of
#!/usr/bin/perl
use strict;
print "Hello there";
print "This got me scratching my head";
And the output would be this code itself with the lines counted.
Thanks a lot in advance.
Something like this?
use warnings;
use strict;
print "Hello there";
print "This got me scratching my head";
open (my $f, '<', $0);
while (<$f>){print};
print "read $. lines\n";
The variable $0 or $PROGRAM_NAME holds the name of your program.
Or (with linecount on each line)
use warnings;
use strict;
print "Hello there";
print "This got me scratching my head\n";
open (my $f, '<', $0);
while (<$f>){printf "%03d %s",$., $_};
print "read $. lines\n";
The variable $. or $INPUT_LINE_NUMBER contains the current line number for the last filehandle accessed.
See perlvar
Also see mobs answer for a way to read the file using DATA
The name of the script being executed is in the variable $0, so the straightforward way to accomplish this is
...
open(my $ZERO,"<",$0);
my #lines = <$ZERO>;
close $ZERO;
print #lines, "count = ", 0+#lines, "\n";
...
When $0 is unavailable because you have changed directories or overwritten it, another option is to use the special DATA handle which is opened on a file that contains the special __END__ or __DATA__ tokens.
...
seek DATA, 0, 0; # seek to begin of file, not begin of __DATA__ section
my #lines = <DATA>;
print #lines, "count = ",0+#lines,"\n";
...
__DATA__
...
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I would love to help with expressing that I've been here for a few weeks.
I'm trying to open an XML file in Perl in encoding utf-16.
I am able to create the file in utf-8 but it does not suit me and more than that it causes me problems.
Please see if the following sample code complies with your requirements
use strict;
use warnings;
use Encode qw/encode decode/;
my $filename = 'utf16_1.txt';
open my($out), '>:encoding(UTF-16LE)', $filename
or die "Couldn't open $filename";
my $string = 'Sample data';
print $out $string;
close($out);
other variation
use strict;
use warnings;
use Encode qw/encode decode/;
my $filename = 'utf16_2.txt';
open my($out), '>:raw', $filename
or die "Couldn't open $filename";
my $string = 'Sample data';
print $out encode("UTF-16LE", $string);
close($out);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am trying to get this script to work in regards to two find strings. I am having trouble getting the file to export the format I am looking for.
use strict;
my $find = '^(H0|HT)';
open (NEW, ">", "output.txt" ) or die "could not open:$!";
open (FILE, "<", "input.txt") or die "could not open:$!";
while (<FILE>) {
chomp;
if (/^\h{39}\XX187/){
print NEW join(",","$_\n");
}
if (/$find/){
print NEW join(",","$_")
}
}
close (FILE);
close (NEW);
Input File:
Txn Bch
Account Patient Name Date
--------------------------------------------------------------------------------------------
HT12345678 TEST,TESTNAME 01/01/16
XX187 CLAIM PROCESSED 01/01/16
Output File (Expectation):
HT12345678 TEST, TESTNAME 01/01/16 XX187 CLAIM PROCESSED 01/01/16
Your question is very imprecise, and I'm having trouble understanding exactly what it is that you need. However, this short program may help
use strict;
use warnings 'all';
while ( <DATA> ) {
print if /^(?:H[0T]|\s+XX187\b)/;
}
__DATA__
Account Patient Name Date
--------------------------------------------------------------------------------------------
HT12345678 TEST,TESTNAME 01/01/16
XX187 CLAIM PROCESSED 01/01/16
output
HT12345678 TEST,TESTNAME 01/01/16
XX187 CLAIM PROCESSED 01/01/16
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
This is the data in my music.txt file
Seamus McGuire:The Wishing Tree:09-14-2000:14.95
Pat Kilbride:Loose Cannon:07-02-2000:15.95
Kevin Crawford:Seasons of Mists:06-23-2000:16.95
Prince:Purple Rain:01-01-1995:3.95
Meat Loaf:Bat out of Jell:03-03-1980:11.95
Eddie Money:Two Tickets:09-04-1979:8.98
I am trying to write a Unix Perl script that
Asks for artist's name (either first or last) from user
Displays artist's full name, CD title, date, and price in formatted
output.
Proper heading, with commands of your choice, is expected
can someone help me?
#!/usr/local/bin/perl
use strict;
use warnings;
print "Enter first or last name of artist: ";
chomp(my $input = <STDIN>); #Take input from user
open (my $fh, "<", "music.txt") or die $!; #Open the file in read mode
while(chomp(my $line = <$fh>)){
if($line =~ /$input/){ #Check if input matches with line
my #artist_info = split/:/,$line; #Split the data from line based on `:`
print " Name: $artist_info[0]\n CD Title: $artist_info[1]\n Date: $artist_info[2]\n Price: $artist_info[3]\n";
}
}
close($fh);
DEMO
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to remove every lines in a text file that contain the word "script" and then write whatever left into another file. But keep the originl file.
like this:
open the file
delete any line with the word "script"
then output whatever left after delete to another file.
perl -ne '/script/ or print' file > newfile
grep -v script original.file > new.file
Or if you really need perl:
#!/usr/bin/perl
use strict;
use warnings;
open(my $in, '<', 'input.txt')
or die "Cannot open input.txt: $!";
open(my $out, '>', 'output.txt')
or die "Cannot open output.txt: $!";
while (<$in>) {
print $out $_ unless /script/;
}
close($in);
close($out);
Finally, if you are only looking to match "script" if it is a word (and not part of a bigger string like "prescription" or "scripting") then change:
/script/
To:
/\bscript\b/