[Solved] How to lowercase and replace spaces in bash? [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

[Solved] How do I replace a line in a file using Perl?

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

[Solved] How do I replace the middle of a string?

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

[Solved] Output left or right part of a matched string [closed]

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

[Solved] Using SFTP to transfer images from HTML form to remote linux server using PERL/CGI.pm

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

[Solved] When is it advantageous to use curly braces for dereferencing in Perl?

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

[Solved] Fast multiple search and replace in Perl

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

[Solved] perl + numeration word or parameter in file

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

[Solved] Converting numbers to K/M/G/T and days/hours/min?

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