*NIX Tricks

[f77] Concatenate a string with another string or an integer in fortran 77

Posted in fortran, programming by kousik on March 23, 2011

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”

Posted in bash, cli, csh, zsh by kousik on March 13, 2011

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

Posted in cli, unix by kousik on January 14, 2010

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

Posted in bash, linux, programming, unix, zsh by kousik on October 8, 2009
-a file
True if file exists.
-b file
True if file exists and is a block special file.
-c file
True if file exists and is a character special file.
-d file
True if file exists and is a directory.
-e file
True if file exists.
-f file
True if file exists and is a regular file.
-g file
True if file exists and its set-group-id bit is set.
-h file
True if file exists and is a symbolic link.
-k file
True if file exists and its “sticky” bit is set.
-p file
True if file exists and is a named pipe (FIFO).
-r file
True if file exists and is readable.
-s file
True if file exists and has a size greater than zero.
-t fd
True if file descriptor fd is open and refers to a terminal.
-u file
True if file exists and its set-user-id bit is set.
-w file
True if file exists and is writable.
-x file
True if file exists and is executable.
-O file
True if file exists and is owned by the effective user id.
-G file
True if file exists and is owned by the effective group id.
-L file
True if file exists and is a symbolic link.
-S file
True if file exists and is a socket.
-N file
True if file exists and has been modified since it was last read.
file1 -nt file2
True if file1 is newer (according to modification date) than file2, or if file1 exists and file2 does not.
file1 -ot file2
True if file1 is older than file2, or if file2 exists and file1 does not.
file1 -ef file2
True if file1 and file2 refer to the same device and inode numbers.
-o optname
True if shell option optname is enabled. The list of options appears in the description of the -o option to the set builtin (see The Set Builtin).
-z string
True if the length of string is zero.
-n string
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 OP arg2
OP is 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”

Posted in cli, grep by kousik on September 25, 2009

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.

Tagged with: , , , , , , , , , ,
Follow

Get every new post delivered to your Inbox.