User Tools

Site Tools


wiki:basicbash

Basic bash scripts

Getitor installation

Exercise:
Script: open by rstudio ~/ost4sem/exercise/basic_adv_bash/basic_bash.sh
Data: ~/ost4sem/exercise/basic_adv_bash/aver_month_nuts3_fire.asc
Directory: ~/ost4sem/exercise/basic_adv_bash

Search a command

Change directory and create a little file from a large file:

rstudio ~/ost4sem/exercise/basic_adv_bash/basic_bash.sh &
cd ~/ost4sem/exercise/basic_adv_bash/
head -1000 aver_month_nuts3_fire.asc > input.txt 

Read/explore the input.txt file

Run the following commands one by one.

head input.txt
more input.txt
tail input.txt 

Count the line/word/character in a input.txt

wc input.txt

Search for a word in a file

grep "2007" input.txt

I want to search for a command able to sort the input.txt table based on the Year column (YYYY).

man -k  sort

The last lines contain:
sort (1) - sort lines of text files
So i will search how to use the sort command:

man sort

Sorting a file

The -k option identify the column of sorting:

  • Sorting based on column number 2 ( -k 2,2)
  • sorting based on column number 2 and then number 1 ( -k 2,1)

See again man sort for more options like -n -g
Alfa numeric sorting:

 sort -k 2,2 input.txt

general numerical sorting

 sort -k 2,2 -g  input.txt

string numerical sorting

 sort -k 2,2 -n  input.txt

Save the result of a command in a file by '>' symbol

sort -k 2,2 -g input.txt > input_s.txt
wc -l input_s.txt

Which is the first and last year of observations?

Append the command result to a file

Add the result of a command in the already existing “output” file by '»' symbol

sort -k 3,3 -g input.txt >> input_s.txt
wc -l input_s.txt

Concatenate commands

We want to count how many observations exist in year 2007 in the input.txt.
Concatenate command by the “|” symbol Search for the word “2007” and count the line/word/character

grep "2007" input.txt | wc

Use the variable

Define the value of the variable, print it by putting it in front of the $ symbol

var=21 ; echo $var

Define the value of the variable using the result of a command

var=`grep "2007" input.txt | wc -l`
echo $var

Pay attention to differences between

`

and

  '

If the variable setting is equal to a command result and not equal to a string, use single back-quotation ` to include the command.

var=varname
var=$(command)

Remember var is space sensitive.
var=variable is correct

var= variable is not correct

For loop

We want to automatically count how many observations exist in the years 2007, 2006 2005 in the input.txt file.
To solve this we can use the variable and list word/number loop function

for var in 2005 2006 2007; do
    grep $var input.txt | wc -l     
done 

Now we want to automatically count how many observations exist from year 2000 to 2008 in input.txt file.
For this use the serial number list loop function.

rm input_wc.txt
for ((var=2000 ; var<=2008 ; var++)); do
    grep $var input.txt | wc -l  >> input_wc.txt  
done 
wiki/basicbash.txt · Last modified: 2018/06/01 11:05 (external edit)