How to fill all empty valued cells within a CSV file with sed and bash shell

The following bash script can be used to fill empty cells within a CSV file. We assume that your file is evenly distributed so that it contains same number of columns in each row separated by comma. If your file is TABseparated use a following linux command to convert it to comma separated value file before you proceed.
Example:

$ cat test 
1       2       4       4
2       3       3       3
$ sed 's/\t/,/g' test 
1,2,4,4
2,3,3,3
OR
$ cat test | tr '\t' ','
1,2,4,4
2,3,3,3

Read more

-bash: /bin/rm: Argument list too long – Solution

Symptoms

This error message appears when you try to remove, move or copy a long list of files. When using your shell a command can only accept a limited number of arguments. When the number of arguments supplied to the command exceeds the permitted number of arguments an error message will appear:

-bash: /bin/rm: Argument list too long

linux command to find your limit for maximum arguments:

# getconf ARG_MAX
2097152

Example:

# rm *
-bash: /bin/rm: Argument list too long

Read more

How to store all shell commands immediately after execution into .bash_history file

When using a bash shell all your entered commands are remembered by history library. The history library will keep track of every command you have entered. This is a default for most if not all Linux systems. However, the commands you enter are first temporarily stored into an internal memory and are written to your ~/.bash_history  only after you properly exit your shell session.

Depending on your shell usage this may cause some undesired results. For example, if your connection to a remote host gets disconnected, your history file will not get updated and thus you will lose all you previously entered commands. Furthermore, while your commands for one session are temporarily stored within system’s internal memory you would not be able to access it from another shell session.

Use the following linux command to force your shell to append every command entered during a current shell session into ~/.bash_history file:

shell 1: $ history -a

Read more

How to count occurrence of a specific character in a string or file using bash

Below you can find some hints on how to count an occurrence of specific character in a file or in a string. Le’s say we have a string “Hello Bash”:

$ STRING="Hello Bash"
$ echo $STRING
Hello Bash

Using bash shell we can now count an occurrence of any given character. For example let’s count number of occurrences of a character l:

$ echo $STRING | sed -e 's/\(.\)/\1\n/g' | grep l | wc -l
2

Read more

BASH Scripting: Parenthesis Explained

Author: Tobin Harding
Here we briefly outline some of the major use cases for brackets, parenthesis,
and braces in BASH scripting, see bottom of page for definition of
these three terms.

Double parentheses (( )) are used for arithmetic:

((var++))
((var = 3))
for ((i = 0; i < VAL; i++))
echo $((var + 2))

Read more

How to access and print command line arguments with Python

The following is an example on how to pass and access command line arguments which a Python script. Save the following python script to a file eg. python-arguments.py

from sys import argv

name, first, second, third, fourth = argv

print "Script name is:", name
print "Your first argument is:", first
print "Your second argument is:", second
print "Your third argument is:", third
print "Your fourth argument is:", fourth


# Alternatively we can access "argv" argument list directly using range. For exmaple:

# Print all arguments except script name
print argv[1:]

# Print second argument
print argv[2]

# Print second and third argument
print argv[2:4]

# Print last argument
print argv[-1]

Read more

R programming software environment Docker image deployment and usage

About

The automated build Docker image of The R Project for Statistical Computing “linuxconfig/cran-r” can be used to instantly deploy R programming software environment on any hosts given that you have docker already installed on your system.

Configuration

The docker image with the R Project is compiled and runs on Debian GNU/Linux system.

Usage

Here we assume that docker is already installed on your system.First pull docker image:

# docker pull linuxconfig/cran-r

Read more

Extract all URLs using Beautiful Soup and Python3

The following link will extract all URL’s for a given web page.

#!/usr/bin/env python3

# Python Version:  3.4.2
# bs4 version: 4.3.2-2

from urllib.request import urlopen
from bs4 import BeautifulSoup

html = urlopen("http://gnu.org") # Insert your URL to extract
bsObj = BeautifulSoup(html.read());

for link in bsObj.find_all('a'):
    print(link.get('href'))

Read more

An overview of GNU R programming language

Introduction

The aim of this article is to provide an overview of the GNU R programming language. It starts a series of articles devoted to programming with R. Its objective is to present, in an organized and concise manner, the elementary components of the R programming language. It is designed to help you understand R code and write your own. It is assumed that the reader has already some basic programming knowledge of R. If you are not familiar with any of R features it is recommended that you first read A quick GNU R tutorial to basic operations, functions and data structures.

Expressions

An R expression is an elementary component of R code. Expression in R can be:

  • assignment statement;
  • conditional statement;
  • arithmetic expression.

Examples of R expressions:

> y<-100
> if (1==1) 1 else 0
[1] 1
> 100/5
[1] 20

R expression are constructed from objects or functions. It is common to separate them with a new line, however, you can also separate expressions with semicolons as below.

Read more

How to Make a Basic Intrusion Detection System with Bash

Introduction

For most of us WEP encryption has become a joke. WPA is quickly going the same way thanks to many tools such as Aircrack-ng. On top of this, wired networks are no strangers to unwanted guests as well. Anyone serious about security should have a good Intrusion Detection system in their toolbox.

There are already some very good IDS’s (Intrusion Detection Systems) available. Why would anyone want to re-invent the wheel in Bash??? There are a couple of reasons for this. Obviously Bash scripts can be very light weight. Especially compared to some of the GUI programs that are out there. While programs like Etherape suck us in with pretty colors, they require constant monitoring to know when the network has changed. If you are like most of us, you only use the computer for two things, work and play. By using the system bell to alert for new clients online you can leave this script running and not have to have a constant watch. If you do decide you want to inspect what a suspicious client is doing more closely, you can always open up etherape, wireshark, or your tool of choice. But until you have a problem you can play or work on other things.

Another bonus to this program is that it will only show ip addresses on the networks connected to your computer. If you were hosting a busy server or perhaps downloading the latest Linux distro though a torrent client, an IDS may be flooded with connections. Looking for a new malicious client can be like looking for a needle in a hay stack. While this script may seem simple compared to other IDS’s, simplicity can have its perks too.

What you will need

Nmap is required for this script to work. We will not be doing any port scanning. However, to make this script fast we needed something better than a regular ping. Nmap’s -sP parameter will only use a ping scan to check if a clients up. There were some variations in how Nmap outputs information between versions. So far this script has only been tested using Nmap 5.00 (Debian Squeeze) and 5.21 (Debian Sid). You may have luck with other distros and versions of Nmap. However, with all the possibilities I could only support a couple at this time.

Read more