[Solved] Perl tr usage of regexp [closed]
tr/// has nothing to do with regular expressions. tr/// is documented in perlop. my $x = “aabbccddee!”; $x =~ tr/abc/xyz/; say $x; # xxyyzzddee 2 solved Perl tr usage of regexp [closed]
tr/// has nothing to do with regular expressions. tr/// is documented in perlop. my $x = “aabbccddee!”; $x =~ tr/abc/xyz/; say $x; # xxyyzzddee 2 solved Perl tr usage of regexp [closed]
perl -wpe’s!”([^”]*)”!”<blue>$1</blue>”!g’ filename 4 solved Search and replace value in-between quotes [closed]
Requires bash4 (tested with 4.3.48). Assuming you never want a upper case character in the value of the variable output, I would propose the following: typeset -l output output=${input// /_} typeset -l output: Defines the variable to be lowercase only. From man bash: When the variable is assigned a value, all upper-case characters are converted … Read more
The only actual problem with your code was that you were printing the line to the new file in the wrong place in the loop. You need to print every line from the old file into the new file. Having tidied your file a little and updated some of the idioms, I end up with … Read more
You can use the operating system to signal an ALRM (alarm), which you can then exit your process on after a set number of seconds. So for instance, if at the top of your script you put: alarm 300; This will cause your process to receive an ALRM signal. This will kill your process immediately, … Read more
You need look-around assertions. $a =~ s|(?<=<no> ).*(?= </no>)|000|gi; # $a is now “<no> 000 </no> ” Have you considered reading a Perl book or two? You are not learning effectively if you have to come to Stack Overflow to ask that sort of questions that can be easily answered by reading the fine documentation. … Read more
You’ll have much more succes using an XML parser, for example, XML::Parser. Parsing XML using regular expressions is very difficult (impossible?) and unless your use case is trivial, a proper XML parser is the reliable solution. 1 solved How can I match XML tags and attributes with a regular expression in Perl?
Here are a couple ways to only keep the text that comes before your pattern, if it exists a <- “GCUGUGGAGAUAACUGCGC” b <- “CUGGCUGAGGUAGUAGUUUGUGCUGUUGGUCGGGUUGUGACAUUGCCCGCUGUGGAGAUAACUGCGCAAGC” strsplit(b, a)[[1]][1] sub(paste0(a, “.*$”), “”, b) Now, you just need to read the files into R and loop over each pattern. I’m not exactly sure what you are looking for, but … Read more
use CGI qw(:standard); use File::Basename; my ( $name, $path, $extension) = fileparse ( $productimage, ‘..*’ ); $productimage = $name . $extension; $productimage =~ tr/ /_/; $productimage =~ s/[^$safechars]//g; if ( $productimage =~/^([$safechars]+)$/ ) { $productimage = $1; } else { die “Filename contains invalid characters”; } $fh = upload(‘image’); $uploaddir = “../../.hidden/images”; open ( UPLOADFILE, … Read more
You are conflating two concepts. The first thing you need to learn is that there is there are two syntaxes for deferencing. Circumfix Postfix “Block syntax” “Arrow syntax” $BLOCK EXPR->$* @BLOCK EXPR->@* $BLOCK[$i] EXPR->[$i] &BLOCK() EXPR->() … … Furthermore, both syntax have a simplification available to them. Your array example is an example of a … Read more
Use a hash. Use the old strings as keys, replacement strings as values. #!/usr/bin/perl use warnings; use strict; my %map; open my $MAP, ‘<‘, ‘map.txt’ or die $!; while (<$MAP>) { my ($pattern, $replacement) = /(.*) { (.*) };/; $map{$pattern} = $replacement; } open my $IN, ‘<‘, ‘input.txt’ or die $!; while (<$IN>) { s/”(.*)”https://stackoverflow.com/”$map{$1}”/g; … Read more
This piece makes it: $ awk ‘{a[$1,$2]=a[$1,$2]$3} END{for (i in a) {print i, a[i]}}’ file B118791136 x A118791136 XxXX B23456433 XXx Just stores the result in an array, having 1st and 2nd fields as indexes. At the end, it prints the result. The result gives B23456433 instead of B 23456433, trying to split it… sed … Read more
It can be condensed to a one line perl script pretty easily, though I don’t particularly recommend it if you want readability: #!/usr/bin/perl s/(.*)=/$k{$1}++;”$1$k{$1}=”/e and print while <>; This version reads from a specified file, rather than using the command line: #!/usr/bin/perl open IN, “/tmp/file”; s/(.*)=/$k{$1}++;”$1$k{$1}=”/e and print while <IN>; 6 solved perl + numeration … Read more
This was discussed on Stackoverflow using Time::Piece. One of the answers comes close to calculating days hours minutes. From what I’ve read about this question before, I think you can easily code it up like this: sub dhms { my $seconds = shift; my $days = int $seconds / 86400; $seconds %= 86400; my $hours … Read more
(\S+)\.| will match and capture any number (one or more) of non-space characters, followed by a dot character. (\S+) | will match and capture any number (one or more) of non-space characters, followed by a space character (assuming the regular expression isn’t modified with a /x flag). In both cases, these constructs appear to be … Read more