Saturday, February 22, 2014

Linux How to concatenate text files

cat file1 file2 filen > outputfile

or if you want to concatenate all text files in directory:
cat *.txt > outputfile
or you can use >> operator to append to file:
cat file1 >> file2

Linux How to count words and lines in a text file

If you want to count words in some text file you could use:
wc -w <file>
example for file 2600.txt (War and Peace from Leo Tolstoy) from Guttenberg Project :
wc -w 2600.txt
output:
566321 2600.txt
 If you want to count lines in some text file you could use:
wc -l <file>
the same file for example
wc -l 2600.txt
output:
65008 2600.txt

Full options list for wc command:

  • -c or --bytes byte counts
  • -m or --chars character counts
  • -l or --lines newline counts
  • -L or --max-line-length lenght of longest line
  • -w or --words word counts

 also:
  • --help
  • --version


Friday, February 1, 2013

Bash Conditionals

Conditionals are used when you must make some decision (to execute some part of code or not) by evaluating expression in your script.
For conditionals in Bash scripting language are used clauses: if, then, else.

if (some expression)
then
(do some code)
else
(do some code)

Simplest example is use only if,then

if ["$Var1" = "$Var2"]; then
echo "Variables Var1 and Var2 are equal!"
fi

with clause fi we close if statement.

Other example:

if ["$Var1" = "$Var2"]; then
echo "Variables Var1 and Var 2 are equal!"
else
echo "Variables Var1 and Var2 are NOT equal!"
fi


Variables in Bash script

Variables in Bash script language have following properties:
  • They haven't data type
  • There is no need to declare variable
So, let's see how they works on simple "Hello World" Example:
#!/bin/bash
Var1 = "Hello World!"
echo $Var1
This simple example will output sentence Hello World!

 Interesting with $ sign in Bash is, if you put something in brackets after $ this will be interpreted as interpreter command.
#!/bin/bash
Var1= $(ls -la)
echo $Var1
This simple example will put output from ls -la command in $Var1 variable and then print content on screen.

Important thing about variables is that you can use local variables in functions, using keyword local.

local Var1="Local variable"

Builtin variables

 

 Bash have Built-in variables, some of them are:
  • $BASH - Path to the Bash binary
  • $BASHPID - Process ID of Bash 
  • $BASH_VERSION - Version of Bash
  • $HOME - Home directory of user
  • $MACHTYPE - Machine type
  • $PATH - Path to binaries
  • $PWD - Working directory
  • $SECONDS - number of second while script is running
  • $UID - User ID number
  • $0,$1 ... - Command line arguments
  • $# - Number of command line arguments
And many more... 





Sunday, October 14, 2012

Bash - Count words in text and make dictionary

In this tutorial i'll try to explain how to count words in some text and make list of used words.
First we could get some text file. I get War and Peace by Leo Tolstoy from Project Gutenberg:
wget http://gutenberg.org/files/2600/2600.txt

After getting file, we must to convert uppercase letters to lowercase:
tr A-Z a-z
then convert everything which is not small letter to new line
tr -cs a-z '\n'
then sort result
sort
finally get unique words and count them:
uniq -c
To get this into working condition we must put those commands into pipeline. To do that we'll make file wordcount:
nano wordcount
and type this:

cat "$@" | tr A-Z a-z | tr -cs a-z '\n'|sort|uniq -c
save and exit nano editor.
Give executable rights to file wordcount:
chmod +x wordcount
and count words with
./wordcount <textfile>
in this case:
./wordcount 2600.txt
and we'll get words used in this great book and their number of appearances.

If we want to get only dictionary used in this book, we make another file:
nano makedict
and type this:
cat "$@" | tr A-Z a-z | tr -cs a-z '\n'|sort|uniq
take note that only difference between wordcount and makedict scripts is -c switch in uniq command.
of course, we must give execution rights to file:
chmod +x makedict
and use:
./makedict 2600.txt









Saturday, August 4, 2012

Mandelbrot fractal zoom animation

Experiment with fractal. Zooming around (-0.234107006,-0.861307916).