How to get a substring at words boundaries in PHP? For example, for a string $a = “ab cc dde ffg ff”;, I would like to get its substring of length at most 7 from position 0 without breaking words. A simple substring($a, 0, 7) will break the word dde. Here, we consider “words” are
Read more
Tag: Programming
How to get the full request URL in PHP?
Posted onHow to get the full request URL of the page being processed in PHP? If you are sure the request is a http or https one, and the PHP script is executed according to (e.g. by a load balancer or apache reverse proxy) the REQUEST_URI/HTTP_HOST which are set by the client, the PHP script can
Read more
How to print all fields after a certain field with awk on Linux?
Posted onHow to print all fields after a certain field with awk on Linux? Say, I want to print out all fields after field $3: a b c d e f a b b b a a c d should be transformed to d e f b d You may use a for loop in awk
Read more
How to send a POST request in Go?
Posted onHow to send a POST request in Go? For example, send a POST of content like ‘id=8’ to a URL like https://example.com/api. In Go, the http package provides many common functions for GET/POST. Related to POST requests, the package provides 2 APIs: Post Post issues a POST to the specified URL. func (c *Client) Post(url,
Read more
How to add a prefix string at the beginning of each line in Bash shell script on Linux?
Posted onHow to add a prefix string at the beginning of each line in Bash shell script on Linux? For example, assume we have a file a.txt: line 1 line 2 I want to have, pre line 1 pre line 2 You may use sed, the stream editor for filtering and transforming text: sed -e ‘s/^/pre
Read more
How to detect whether a file is being written by any other process in Linux?
Posted onHow to detect whether a file is being written by any other process in Linux? Before a program open a file to processes it, it wants to ensure no other processes are writing to it. Here, we are sure after the files are written and closed, they will not be written any more. Hence, one-time
Read more
How to count the number of reads in each chromosome in a bam file?
Posted onHow to count the number of reads in each chromosome in a bam file? The bam file is already sorted by the chromosome names. If the bam file is indexed, you may quickly get these info from the index: samtools idxstats in.bam | awk ‘{print $1″ “$3}’ If the bam file is not indexed, you
Read more
Killing Running Bash Script Process Itself and All Child Processes In Linux
Posted onIn Linux, how to kill a process and all its child processes? For example, a Bash script A starts B, B starts C and C calls rsync. I would like to kill A and all its child processes all together. How to do this? There are possibly many answers to this question. One of the
Read more
How to get the mtime of a file on Linux?
Posted onHow to get the mtime of a file on Linux from the file’s path? You can use stat to get the file status including the mtime: %y time of last modification, human-readable %Y time of last modification, seconds since Epoch As an example, $ stat -c %y ./file 2017-06-26 13:33:06.764042064 +0800 $ stat -c %Y
Read more
How to print a line to STDERR and STDOUT in C++?
Posted onIn C++, how to print a string as a line to STDOUT? That is, the string and the newline character, nicely? And similarly, how to print the line to STDERR? In C++, you may print the string and then ‘n’ or std::endl to STDOUT by operating on the std::cout stream: std::cout << your_string << std::endl;
Read more
How to print a line to STDERR and STDOUT in OCaml?
Posted onIn OCaml, how to print a string as a line to STDOUT? That is, the string and the newline character, nicely? And similarly, how to print the line to STDERR? In OCaml, you may print a string with the end line character to STDOUT using the function in the Pervasives module: val print_endline : string
Read more
How to print a line to STDERR and STDOUT in Perl?
Posted onIn Perl, how to print a string as a line to STDOUT? That is, the string and the newline character, nicely? And similarly, how to print the line to STDERR? In Perl, to nicely print a new line to STDOUT, you can use the “say” feature which “Just like print, but implicitly appends a newline”:
Read more
How to print a line to STDERR and STDOUT in Java?
Posted onIn Java, how to print a string as a line to STDOUT? That is, the string and the newline character, nicely? And similarly, how to print the line to STDERR? In Java, you can print string in a new line by ‘System.out.println()’ to STDOUT: System.out.println(“my msg here”); In Java, print a string str to STDERR
Read more
How to print a line to STDERR and STDOUT in C?
Posted onIn C, how to print a string as a line to STDOUT? That is, the string and the newline character, nicely? And similarly, how to print the line to STDERR? In C, to print to STDOUT, you may do this: printf(“%sn”, your_str); For example, $ cat t.c #include <stdio.h> void main() { printf(“%sn”, “hello world!”);
Read more
How to print a line to STDERR and STDOUT in PHP?
Posted onIn PHP, how to print a string as a line to STDOUT? That is, the string and the newline character, nicely? And similarly, how to print the line to STDERR? In PHP, you may print a line to STDOUT using echo by appending the PHP_EOL to the string: echo $your_msg . PHP_EOL; For example, $
Read more
How to print a line to STDERR and STDOUT in Bash?
Posted onIn Bash, how to print a string as a line to STDOUT? That is, the string and the newline character, nicely? And similarly, how to print the line to STDERR? In Bash, you can simply use the echo command: echo “your message here” or echo your message here Examples: $ echo the message here the
Read more
Printing a Line to STDERR and STDOUT in Python
Posted onIn Python, how to print a string as a line to STDOUT? That is, the string and the newline character, nicely? And similarly, how to print the line to STDERR? In Python, to print a string str with a new line to STDOUT: print str In Python to print a line to STDERR: import sys
Read more
How to print a line to STDERR and STDOUT in Go?
Posted onIn Go, how to print a string as a line to STDOUT? That is, the string and the newline character, nicely? And similarly, how to print the line to STDERR? In Go, you can use the ‘Println()’ func from the ‘fmt’ package to print a line to STDOUT: import(“fmt”) fmt.Println(“my msg here”) In Go, os.Stderr
Read more
How to get the running process’ parent process’ ID in Python?
Posted onHow to get the running process’ parent process’ ID in Python? In Python, you can get the parent process’ pid by calling os.getppid(): import os ppid = os.getppid() One example: The shell’s pid: $ echo $$ 21779 Start a python REPL and get its parent pid (the shell’s): $ python Python 2.7.5 (default, Nov 6
Read more
How to get the running process’ parent process’ ID in Go?
Posted onHow to get the running process’ parent process’ ID in Go? In Go, you may call the os.Getppid() func from the os package to get the parent process’ pid. func Getppid() int Getppid returns the process id of the caller’s parent. One example is as follows. $ gore gore version 0.2.6 :help for help gore>
Read more