[f77] Concatenate a string with another string or an integer in fortran 77
Let’s assume that we have defined two strings as
str1 = 'abc'
str2 = 'pqr'
and an integer
integ = 25
(Make sure str1 and str2, and integ are defined as character strings, and integer, respectively)
1. Concatenate str1 and str2 into str3 (make sure you define str3 first as a character string)
str3 = str1//str2
2. Concatenate str1 and integ into str3 (again make sure str3 is defined as a character string)
step a: convert integer to a string first (make sure str4 is defined as a character string and not initialized, i.e. it’s so far an empty string)
write(str4,'(I5)') integ
step b: concatenate str1 and str4 into str3
str3 = str1//str4
[bash] Substitute dots by hyphens using “tr”
If you want to replace dots in a string, for example “a.string.with.dots”, by hyphens use the following trick
$ echo "a.string.with.dots" | tr "." "-"
That’s it!
Needless to say, other characters may also be substituted this way.
[cli] Selectively break a string into pieces and display in new lines
Display each different part of a string separated by a character, say colon (:), in new lines:
Using echo:
$ echo -e ${PATH//:/\\n}
Using printf:
$ printf "%s\n" ${PATH//:/\/* }
Here we chose the system $PATH variable whose different parts are separated by colons.
Credit: Command-line-fu
[bash] Conditional operators in BASH
-afile- True if file exists.
-bfile- True if file exists and is a block special file.
-cfile- True if file exists and is a character special file.
-dfile- True if file exists and is a directory.
-efile- True if file exists.
-ffile- True if file exists and is a regular file.
-gfile- True if file exists and its set-group-id bit is set.
-hfile- True if file exists and is a symbolic link.
-kfile- True if file exists and its “sticky” bit is set.
-pfile- True if file exists and is a named pipe (FIFO).
-rfile- True if file exists and is readable.
-sfile- True if file exists and has a size greater than zero.
-tfd- True if file descriptor fd is open and refers to a terminal.
-ufile- True if file exists and its set-user-id bit is set.
-wfile- True if file exists and is writable.
-xfile- True if file exists and is executable.
-Ofile- True if file exists and is owned by the effective user id.
-Gfile- True if file exists and is owned by the effective group id.
-Lfile- True if file exists and is a symbolic link.
-Sfile- True if file exists and is a socket.
-Nfile- True if file exists and has been modified since it was last read.
- file1
-ntfile2 - True if file1 is newer (according to modification date) than file2, or if file1 exists and file2 does not.
- file1
-otfile2 - True if file1 is older than file2, or if file2 exists and file1 does not.
- file1
-effile2 - True if file1 and file2 refer to the same device and inode numbers.
-ooptname- True if shell option optname is enabled. The list of options appears in the description of the -o option to the
setbuiltin (see The Set Builtin). -zstring- True if the length of string is zero.
-nstring- string
- True if the length of string is non-zero.
- string1
==string2 - True if the strings are equal. ‘=’ may be used in place of ‘==’ for strict posix compliance.
- string1
!=string2 - True if the strings are not equal.
- string1
<string2 - True if string1 sorts before string2 lexicographically in the current locale.
- string1
>string2 - True if string1 sorts after string2 lexicographically in the current locale.
- arg1
OParg2 OPis one of ‘-eq’, ‘-ne’, ‘-lt’, ‘-le’, ‘-gt’, or ‘-ge’. These arithmetic binary operators return true if arg1 is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to arg2, respectively. Arg1 and arg2 may be positive or negative integers.
Reference: Copied directly from here.
[grep] AND, OR, and “these” but NOT “those”
For grepping line-by-line in a file filename, I often find these very useful
Match pattern1 OR pattern2 in the same line:
$ grep -E 'pattern1|pattern2' filename
Match pattern1 AND pattern2 in the same line:
$ grep -E 'pattern1.*pattern2' filename
The above command searches for pattern1 followed by pattern2. If the order does not matter or you want to search them in either order, then use the follwoing
$ grep -E 'pattern1.*pattern2|pattern2.*pattern1' filename
The pipe enables the OR search which we saw earlier. Another option for this situation (i.e., AND search when the order is not important):
$ grep -E 'pattern1' filename | grep -E 'pattern2'
which basically greps the STDOUT of the first grep.
Match pattern1 AND pattern2, but NOT pattern3 in the same line:
$ grep -E 'pattern1.*pattern2' filename | grep -Ev 'pattern3'
when the order of the first two patterns is important. When that order is NOT important:
$ grep -E 'pattern1' filename | grep -E 'pattern2' | grep -Ev 'pattern3'
Match pattern1 OR pattern2, but NOT pattern3 in the same line:
$ grep -E 'pattern1|pattern2' filename | grep -Ev 'pattern3'
N.B. (1) grep -E may be replaced by egrep. I used grep -E everywhere in this post assuming a general case of regular expressions as patterns. Lowercase -e is also used for regex, but this is more “basic” than -E which supports “extended” regex, e.g. regular expression metacharacters like +, ?, | and (). (2) The -v flag is for non-matching grep.

leave a comment