[awk] Display every (n+1)-th line of a file (n=0,1,2….)

Tags

,

As an example, the following displays every first line and every 3rd line after that in a file file.txt:

$ awk 'NR%3==1' file.txt

So, if file.txt has 1 in the first line, 2 in the second and so on, then the above command will display 1 in the first line, 4 in the second, 7 in the third line and so on until it reaches the end of the file.

[regex] Reqular expressions: basics reference card

Tags

, , ,

[a-d] – Match one character with in a-d i.e. a, b, c, d
[^a-d] – Match one character not in the range a-d
\<test\> – Match whole word test
test\> – Match words that ends with test
\<test\> \1ing – Match following text “test testing”, \1 maps to first tag i.e \(\)
x\{5,\} – Match at least 5 occurrences of x
x\{5,9\} – Match between 5 to 9 times occurrences of x
^test – Looks for test at the beginning of a line
test$ – Looks for test at the end of the line
^test$ – Looks for test on a line by itself
th.t – “.” matches one character i.e. 4 letters has th + any character and ends with t. Example: this, that are valid matches
\. – Look for period, using “\” one can escape metacharacters

Search and replace:
:s/\(square\) and \(fair\)/\2 and \1/ – searches for “square and fair” and replaces it with fair and square

[cli] Selectively break a string into pieces and display in new lines

Tags

, , , , , , , ,

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

[unix] Compare the contents of two directories in the command line

Tags

, , , , , , ,

The first thing that comes to mind when you try to do any comparison, e.g. between two files or directories, is diff. Here is how it is used to compare two directories, dir-1 and dir-2:
$ diff /path_to_dir-1/ /path_to_dir-2/

But if the files are very big in size, diff may take quite a while.

Another useful utility is comm which is relatively fast for comparison of two files. It prints out the unique lines of file-1 in column 1, that of file-2 in column 2 and the common lines in column 3. The column 1, 2 and 3 may be suppressed by using the flags -1, -2 and -3, respectively. So, for example, if we want to see which files are common in dir-1 and dir-2, we may use comm with the “-1” and “-2” flags on the output of the ls command on the directories:
$ comm -1 -2 <(ls /path_to_dir-1/) <(ls /path_to_dir-2/)

Note: Of course, you can do a diff (or better vimdiff) of the outputs of ls as above, but comm still wins over diff/vimdiff because of the option to selectively output common or unique lines. Also notice that these forms are different from the diff mentioned at the top which does the comparison byte-by-byte.

[fortran] Create a fortran 77 library and link it

Tags

, , , , , , , , ,

If you have main.f and as the main fortran 77 program and it calls upon subroutines in other files (say, files 1.f, 2.f,…. 7.f), you can compile as (we’ll use GNU Fortran compiler as an example)

$ gfortran main.f [1-7].f

But if you change only one of them (say main.f) and do not touch others at all, you can make a library out of the unchanging ones and link to the library during compilation:

First, create the library:

$ rm -f *.o
$ gfortran -c [1-7].f
$ ar -rcs libmylib.a [1-7].o

This will create the library libmylib. You may put it in convenient location (might I suggest /usr/local/lib/ directory?). You can list out the object files by using:
$ ar t libmylib.a

Next while compiling main.f, you can link to the library in following three ways:
$ gfortran main.f /path_to_the_library/libmylib.a -o executable
or
$ gfortran main.f -L/path_to_the_library/ -lmylib -o executable
or if the library is in a “well-define” library search path, e.g. /usr/local/lib, then
$ gfortran main.f -lmylib

Reference: Ubuntu forum.

Design a site like this with WordPress.com
Get started