Hot answers tagged

13

This perl script builds a hash with words (read one per line from stdin, and/or from any filenames listed on the command line) as keys, and syllable counts as the values. Then it prints the hash keys, sorted by the syllable counts. #! /usr/bin/perl use strict; use Lingua::EN::Syllable; my %words = (); while(<>) { chomp; $words{$_} = ...


7

Use perl: perl -pe 's/(?<=\[)(\d+)(?=\])/$1+1/ge' prova.txt Explanation: -p means loop over every line and print the result after every line -e defines the expression to execute on every line s/from/to/ does a simple substition s/(\d+)/$1+1/ge matches one or more digits, captures it into $1, and then the e modifier on the end tells perl that the ...


4

In perl: #!/usr/bin/perl my (%hotu, %hpc)=(); while(<>){ my($pc,$otu,$v)=split; $hpc{$pc}=1; ($hotu{$otu} or $hotu{$otu}={})->{$pc}+=$v; } #headers my @apc = sort keys %hpc; print join ("\t", 'OTU_ID', @apc) . "\n"; #values foreach my $otu (sort keys %hotu) { print join ("\t", $otu, map {$_=0 unless defined; $_} @{$hotu{$otu}}{@apc}) . ...


4

If your perl script produces no other output than the value of $circle, you can use command substitution to store that output in a variable. For example: circle=$(perl perlscript.pl) If the perl script produces other output as well (or not output at all), you'll have to either: extract only the value you want from the output using the usual text ...


4

You could also use awk: awk -F '[\\[\\]]' '{if ($2) { sub($2, $2 + 3)}} 1' prova.txt In fact, this can be slightly shortened to: awk -F '[\\[\\]]' '$2 { sub($2, $2 + 3)} 1' prova.txt


3

perl -p -e 's/\[(\d+)\]/"[" . ($1+3) . "]"/ge' dad85.txt This perl one-liner replaces any positive integer number (\d+) inside square brackets with that number incremented by 3. It uses the e regexp modifer to make perl evaluate the replace portion of the s/// operation as an expression. Output: (freqBiasL2[30]) (SatBiasL1[30]) (defSatBiasL2_L1[30]) ...


3

In awk: { data[$2, $1] = $3; } END { split("OTU0 OTU1 OTU2 OTU3", rows); split("OTU_ID PC.354 PC.355 PC.356", cols); for (i = 1; i <= 4; i++) { printf("%10s", cols[i]); } print ""; for (i = 1; i <= 4; i++) { printf("%-10s", rows[i]); for (j = 2; j <= 4; j++) { item = data[rows[i], ...


3

I know you've already solved your problem, but FYI you could have solved it with a very simple modification to your code : by inverting the sequence you loop over. Using {107..27..-1} (or more concisely {107..27}) would have been enough to solve your problem, since when replacing 30, only the original 30 would have been found, the 27 having not been ...


2

After the line my @titlewords = split /\s+/, $title; #/ get words add the code to remove the words from the array: my @new; foreach my $t (@titlewords){ push(@new, $t) if $t !~ /^(and|if|the)$/i; } @titlewords = @new;


2

For each line you can just use substr($line,30,2) to get the two characters you want. So something like #!/usr/bin/perl use warnings; use strict; open(my $file1,">file1"); open(my $file2,">file2"); open(my $file3,">file3"); while(<>) { my $ch=substr($_,30,2); if ($ch eq '0A') { print $file1 $_; } elsif ($ch eq '0B') { print $file2 ...


2

You can do it by adding its path to the PERL5LIB environment variable : export PERL5LIB=/opt/dwimperl-linux-5.20.1-10-x86_64/perl/lib/site_perl/5.20.1


1

Easy in Perl: use a hash to store the running sums. perl -laF/,/ -ne ' $h{ $F[0] } += $F[1]; }{ print "$_,$h{$_}" for keys %h; ' input-file > output-file -n reads the input line by line -l adds newlines to print -a splits each input line into the @F array -F/,/ tells -a to split on commas }{ is the "Eskimo greeting" operator, it separates ...


1

Using sed: sed -i.bak 's#^\(GLOBAL_PATH=/export/\)pop/PLW5$#\1lld/sat#' /var/userTN/applo/* The original file(s) will be backup up with .bak extension, if you don't want backup(s): sed -i 's#^\(GLOBAL_PATH=/export/\)pop/PLW5$#\1lld/sat#' /var/userTN/applo/* Example: $ sed 's#^\(GLOBAL_PATH=/export/\)pop/PLW5$#\1lld/sat#' ...



Only top voted, non community-wiki answers of a minimum length are eligible