Thursday, March 26, 2015

Latex Show page number

\thispagestyle{plain}
\pagestyle{plain}


Most likely you have \pagestyle{empty} in your document. Remove that line and by default you will get page numbers on every page.

Sunday, March 1, 2015

7 different ways to print the last line of a file in Linux

Let us consider a file with the following contents:
$ cat file
Unix
Solaris
Linux
1. The tail is the most common command used. tail command prints the last part of the files. -1 specifies to print one line from the last part.
$ tail -1 file
Linux
2. The END label in awk makes it even more easily. END label is reached once the entire file is parsed. Hence, on reaching END, the special variable $0 will be holding the last line of the file.
$ awk 'END{print}' file
Linux
3. In sed, $ indicates the last line, and $p tells to print(p) the last line($) only.
$ sed -n '$p' file
Linux
4. Another option in sed is to delete(d) all the lines other than(!) the last line($) which in turn prints only the last line.
$ sed '$!d' file
Linux
5. In perl, every line being processed is saved in a variable. Same explanation as of awk. Only difference here is the scope of the variable $_ is only present in main, not in the END label. Hence, every line is stored in a variable and the same variable is used in the END.
$ perl -ne '$x=$_;END{print $x;}' file
Linux
6tac command prints a file in reverse. By printing the first line of the tac output using the head, we will get the last line of the file printed. Not the best of options, but an option nevertheless.
$ tac file | head -1
Linux
7. Solution using a proper shell script.  The file is processed using the while loop where each line is read in one iteration. The line is assigned to a variable x, and outside the loop, x is printed which will contain the last line of the file.
#!/bin/bash

while read line
do
        x=$line
done < file
echo $x

How to remove columns from CSV file based on column number using bash shell

Cite from :http://linuxconfig.org/how-to-remove-columns-from-csv-based-on-column-number-using-bash-shell

In the following example we are going to show how to remove columns from CSV file based on the column number. Consider a following command separated file containing 10 columns:
$ cat temp.csv 
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10

In the first example we are going to remove second column. The best tool for this job is cut command:
$ cut -d, -f2 --complement temp.csv 
1,3,4,5,6,7,8,9,10
1,3,4,5,6,7,8,9,10
1,3,4,5,6,7,8,9,10
1,3,4,5,6,7,8,9,10
1,3,4,5,6,7,8,9,10
1,3,4,5,6,7,8,9,10
1,3,4,5,6,7,8,9,10
Next, we will remove all columns in range 2-4 and 7,9:
$ cut -d, -f2-4,7-9 --complement temp.csv 
1,5,6,10
1,5,6,10
1,5,6,10
1,5,6,10
1,5,6,10
1,5,6,10
1,5,6,10
Remove the --complement option to remove columns 1,5,6,10:
$ cut -d, -f2-4,7-9 temp.csv 
2,3,4,7,8,9
2,3,4,7,8,9
2,3,4,7,8,9
2,3,4,7,8,9
2,3,4,7,8,9
2,3,4,7,8,9
2,3,4,7,8,9
In the last example we will remove columns 1,5,7 using bash variable:
$ remove='1,5,7'
$ cut -d, -f$remove --complement temp.csv 
2,3,4,6,8,9,10
2,3,4,6,8,9,10
2,3,4,6,8,9,10
2,3,4,6,8,9,10
2,3,4,6,8,9,10
2,3,4,6,8,9,10
2,3,4,6,8,9,10