[bash] zcat does not work on MACOS (.Z bug)

sudo mv /usr/bin/zcat /usr/bin/broken-zcat
sudo ln -s /usr/bin/gzcat /usr/bin/zcat

[bash] awk insert blank line every n lines

  awk '{ if ((NR % 5) == 0) printf("\n"); print; }'

for n == 5, of course. Substitute whatever your idea of n is.

[bash] convert windows source file to unix

dos2unix *.py

[bash] remove first column from a file

cut -f2- filename

[bash] replace delete with backspace in screen

You can define an alias in your ~/.bashrc file like

alias screen='TERM=screen screen'

[java] generate classpath

[OLD use maven ;)] all the jars in a directory:

JAR=$(for i in $(ls); do  echo $(pwd)/$i; done | tr "\n" ":").

recorsive:

JAR=$(find . -type f | sed "s#^.#$(pwd)#" | egrep ".*jar$" | tr "\n" ":" ).

[maven] create a jar with all the dependecies in maven

add this snippet to the code

	<build>
	    <plugins>
	        <plugin>
	            <artifactId>maven-assembly-plugin</artifactId>
	            <configuration>
	                <archive>
	                    <manifest>
	                        <mainClass>fully.qualified.MainClass</mainClass>
	                    </manifest>
	                </archive>
	                <descriptorRefs>
	                    <descriptorRef>jar-with-dependencies</descriptorRef>
	                </descriptorRefs>
	            </configuration>
	        </plugin>
	    </plugins>
	</build>

compile using

mvn  assembly:assembly

produce a jar (..with-dependences) with all the dependecies

[bash] sort using tab as separator

sort -t$'\t' -k 2 file

[java] change java heap size

java -Xmx<size> set maximum Java heap size
e.g. java -Xmx4196m Pippo

[bash] resolve redirected url

URL=...
REDIRECT_URL=$(curl -w %{redirect_url} URL) 

my bashrc

[bash] get a txt file edited from pirate pad

curl http://piratepad.net/ep/pad/export/$id/latest?format=txt

[java] does not find JAVA_HOME

export JAVA_HOME=a/usr/lib/jvm/java-6-sun-1.6.0.22/a

ornellaia export JAVA_HOME=/usr/lib/jvm/java-6-sun-1.6.0.24/

[bash] dump a website

wget -r -H -l1 -k -P $targetdir --exclude-domains ${comma-seperated domain name} --user=xxx --password=xxx $url

[bash] get a webpage with autentication

url= ""
outpage = ""
user=""
pass=""
curl -u $user:$pass $url >  $outpage

[bash] number of results for a query on google

query=pippo+pluto
results=$(curl -A "Firefox/3.0" http://www.google.com/search?q=$query | egrep -o "About [1234567890,]+ results" | tr -d ' ,Aboutresults')

[linux] 10 nice hints

[bash] how to get the sum of all the lines in the file

awk '{a+=$0}END{print a}' abc

[python] remove the garbage collector in python

when you are working with big data structure in python the garbage collector can slow down the exection, sometime is better to disable it

import gc
gc.disable()

[bash] how to query solr from the shell

[bash] sum rows with the some field

dictionary in awk:

$ cat nayan.out
saman 1
gihan 2
saman 4
ravi 1
ravi 2

$ awk '{arr[$1]+=$2} END {for (i in arr) {print i,arr[i]}}' nayan.out > nayan.out.tmp

$ cat nayan.out.tmp
ravi 3
saman 5
gihan 2

[maven] resources in a jar

[bash] get all pdf in a page

wget -m --accept=pdf -nd  $url

[bash] iterate over the lines of a jar

while read line
do
echo $line
done < file_to_read

[bash] remove blank lines in a file

grep -v '^[[:space:]]*$'  file

[bash] change tmp folder where sort put temporary files

sort -T dir ...

[bash] print number of a specific char for each line of a file

awk -F'#' '{ print NF-1}' 

prints how many chars '#' per line

[bash] replace a substring

 
i=pippo.txt
echo ${i/txt/tex}
#stamp pippo.tex

[bash] sort on more than one field

sort -k 1,1 -k 3,3nr file.tsv > tmp

sort on the first field and if two elements are equals on the first, on the third field in reverse integer order

[bash] remove all files of size 0

find . -type f -size 0k -exec rm {} \; | awk '{ print $8 }'

[bash] remove all chars in a particular position from a file

cut -c44- file

removes the first 44 characters from all the lines of file

[bash] view what is appended on file

tail -f file -n 

[bash] get words distribution from a file

cat file | tr '[A-Z]' '[a-z]' | tr -sc '[a-z]' '\n' | grep -v '^[^a-z]*$' | grep -v '^[\]'  |    sort | grep -vxf topwords-ita.txt | uniq -c   | sort -nrk1

[bash] tr complement

tr -sc '[a-zA-Z]' '\n' < file

replace all chars that are not alphabetic in new lines

[bash] remove first or last lines

 
 sed '1d' filename 

the first line

 
 sed '$d' filename 

the last line

[bash] check bash parameters

# Check for proper number of command line args.

EXPECTED_ARGS=1
E_BADARGS=65

if [ $# -ne $EXPECTED_ARGS ]
then
  echo "Usage: `basename $0` {arg}"
  exit $E_BADARGS
fi

[bash] Generate all the couples of terms

tr -sc '[a-zA-Z0-9\n]' '\t'  < inputFile | awk '{for (i = 1; i < NF; i++) print $i"\t"$(i+1); }'

[bash] Find a process listening on a particular port

sudo netstat -lpn |grep :8080

[bash] Check for proper number of command line args

EXPECTED_ARGS=1
E_BADARGS=65

if [ $# -ne $EXPECTED_ARGS ]
then
  echo "Usage: `basename $0` {arg}"
  exit $E_BADARGS
fi

[bash] Sort in numeric order with scientific notation allowed

sort -g

[bash] create ssh pub key

<ssh-keygen -t rsa -C "your_email@example.com"
# Creates a new ssh key, using the provided email as a label
# Generating public/private rsa key pair.
# Enter file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]

Now you need to enter a passphrase.

Enter passphrase (empty for no passphrase): [Type a passphrase]
# Enter same passphrase again: [Type passphrase again]

copy iid_rsa.pub in .ssh/authorized_key

[bash] gzip a folder

tar -czf folder_name.tar.gz folder_name/