Linux Tips and Tricks

Linux Tips and Tricks

Download Facebook Images (friends & pages) through linux commands

February1

1)  Goto facebook photos page.

2) save the html page and save it as tips.html

Sample page : Download from here (Right click and choose “Save Link As” )

3) fire the below command.

1
2
3
sed -e "s/\.jpg\"/\.jpg\n/g" fb.html  \
| awk -F\" '/data-starred-src.*https/{print $NF}' \
| while read a; do curl -s -O "$a"; done

one-liner

1
sed -e "s/\.jpg\"/\.jpg\n/g" fb.html  | awk -F\" '/data-starred-src.*https/{print $NF}' | while read a; do curl -s -O "$a"; done
posted under Uncategorized | No Comments »

facebook hackercup 2013 – Beautiful strings – Qualification Round Problem

January30

 

images

 

Beautiful strings

When John was a little kid he didn’t have much to do. There was no internet, no Facebook, and no programs to hack on. So he did the only thing he could… he evaluated the beauty of strings in a quest to discover the most beautiful string in the world.

Given a string s, little Johnny defined the beauty of the string as the sum of the beauty of the letters in it.

The beauty of each letter is an integer between 1 and 26, inclusive, and no two letters have the same beauty. Johnny doesn’t care about whether letters are uppercase or lowercase, so that doesn’t affect the beauty of a letter. (Uppercase ‘F’ is exactly as beautiful as lowercase ‘f’, for example.)

You’re a student writing a report on the youth of this famous hacker. You found the string that Johnny considered most beautiful. What is the maximum possible beauty of this string?

Input

The input file consists of a single integer m followed by m lines.

Output

Your output should consist of, for each test case, a line containing the string “Case #x: y” where x is the case number (with 1 being the first case in the input file, 2 being the second, etc.) and y is the maximum beauty for that test case.

Constraints

5 ≤ m ≤ 50
2 ≤ length of s ≤ 500

Example input

5
ABbCcc
Good luck in the Facebook Hacker Cup this year!
Ignore punctuation, please :)
Sometimes test cases are hard to make up.
So I just go consult Professor Dalves

Example output

Case #1: 152
Case #2: 754
Case #3: 491
Case #4: 729
Case #5: 646

My Solution to this problem
1
2
3
4
5
6
7
#!/bin/bash
 
awk 'NR>1' input.txt | while read line
do
    c=$((c+1))
    echo $line | awk '{a="[^A-Za-z]";gsub(a,"",$0);print}' | tr '[A-Z]' '[a-z]' | fold -w1 | sort | uniq -c | sort -nr | awk -v c="$c" 'BEGIN{a=26}{sum+=$1*a;a--}END{print "Case #"c": " sum}'
done
posted under Uncategorized | No Comments »

Finding the nth Particular Week in a Month – shell script

December6

I see lot of request posted in internet to find out the day of nth week in a Month.

example:

what is the date of 3rd Sunday in October

What is the date of 2nd Friday in June 2012

what is the date of 4th Saturday in January 2011..etc..

The below shell script is used to find out the days for the nth particular week in a month.

Try it out and shout if you find any issues… (Script is tested in bash shell. OS – linux(ubuntu))

For better readability http://paste.ubuntu.com/1412930/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/bin/bash
#########################################################
# Script Name : finday.sh                #
# Author : Kamaraj Subramanian                #
# Website : www.thelinuxtips.com            #
# Bugs : Send it to kamaraj[at]thelinuxtips[dot].com    #
#########################################################
USAGE()
{
    echo "----------------------------------------------------------------"
    echo "USAGE :: findday YEAR MONTH DAY WEEK"
    echo "Example :"
    echo "findday 2012 10 SU 3"
    echo "The above will tell the date of 3rd sunday in October 2012"
    echo "Valid values for YEAR ( 1 to 9999 )"
    echo "Valid values for MONTH ( 1 to 12 )"
    echo "Valid values for DAY ( SU, MO, TU, WE, TH, FR, SA )"
    echo "Valid values for WEEK ( 1 to 5 )"
    echo "----------------------------------------------------------------"
    exit 1
}
 
YearCheck()
{
    echo "$1" | grep -v "^[0-9]*$" >/dev/null 2>&1 && echo "Please enter the number 1 to 9999" && USAGE
    if [[ ! "${1}" -le "10000" || "${1}" -eq "0" ]]
    then
        echo "Enter the correct Year [1-9999]"
        USAGE
    fi
 
}
 
MonthCheck()
{
    echo "$1" | grep -v "^[0-9]*$" >/dev/null 2>&1 && echo "Please enter the number 1 to 12" && USAGE
    if [[ ! "${1}" -le "12" || "${1}" -eq "0" ]]
    then
        echo "Enter the correct Month [1-12]"
        USAGE
    fi
}
DayCheck()
{
    echo "$1" | egrep -v "^SU$|^MO$|^TU$|^WE$|^TH$|^FR$|^SA$" >/dev/null 2>&1 && echo "Valid values for DAY ( SU, MO, TU, WE, TH, FR, SA )" && USAGE
    [ "$1" == "SU" ] && WNO=7 && PNO=1
    [ "$1" == "MO" ] && WNO=6 && PNO=2
    [ "$1" == "TU" ] && WNO=5 && PNO=3
    [ "$1" == "WE" ] && WNO=4 && PNO=4
    [ "$1" == "TH" ] && WNO=3 && PNO=5
    [ "$1" == "FR" ] && WNO=2 && PNO=6
    [ "$1" == "SA" ] && WNO=1 && PNO=7
 
}
WeekCheck()
{
    if [[ ! "${1}" -le "5" || "${1}" -eq "0" ]]
    then
        echo "Enter the correct WEEK [1-5]"
        USAGE
    fi
}
 
if [ "$#" -ne "4" ]
then
    USAGE
else
    YearCheck $1
    MonthCheck $2
    DayCheck $3
    WeekCheck $4
    echo "---------------"
    echo "Given Inputs"
    echo "---------------"
    echo "YEAR  :: $1"
    echo "MONTH :: $2"
    echo "DAY   :: $3"
    echo "WEEK  :: $4"
    echo "---------------"
    cal $2 $1 | awk -v n="$4" -v WNO="$WNO" -v PNO="$PNO" '
    {
    if(NR==3)
    {
        if(NF==WNO)
        {
            a=2;
        }
        else
        {
            a=3;
        }    
    }
    if(a && NR==(a+n))
    {
        if(length($PNO)>0)
        {
            printf("Output Date : %s\n",$PNO);
        }
        else
        {
            printf("Requested week date is not available for the given month year combination\n");
        }
        exit;
    }
    }'
fi

Output of the script

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
$ ./findday.sh 2012 12 SA 5
---------------
Given Inputs
---------------
YEAR  :: 2012
MONTH :: 12
DAY   :: SA
WEEK  :: 5
---------------
Output Date : 29
 
$ cal 12 2012
   December 2012      
Su Mo Tu We Th Fr Sa  
                   1  
 2  3  4  5  6  7  8  
 9 10 11 12 13 14 15  
16 17 18 19 20 21 22  
23 24 25 26 27 28 29  
30 31                 
 
$ ./findday.sh 2012 12 FR 5
---------------
Given Inputs
---------------
YEAR  :: 2012
MONTH :: 12
DAY   :: FR
WEEK  :: 5
---------------
Requested week date is not available for the given month year combination
posted under Uncategorized | No Comments »

Hoax Message – December 2012 – 5 Saturdays, 5 Sundays and 5 Mondays

November27

There is a Hoax message spreading out in emails. Stating that,

Circulating message claims that December 2012 will feature 5 Saturdays, 5 Sundays and 5 Mondays, a combination of days that occurs only once every 823 years. The message claims that sending on the information will bring good luck to the sender.

Brief Analysis
It is perfectly true that December 2012 has 5 Saturdays, 5 Sundays and 5 Mondays. However, such a combination occurs far more often than every 823 years. The last occurrence was in December 2007 while the next occurrence will take place in December 2018. In fact, any month that has 31 days will have three consecutive days that occur five times in the month. Such combinations are commonplace and occur each and every year. This message is a revamped version of earlier – and equally spurious – chain messages.

Thanks to http://www.hoax-slayer.com/december-2012-money-bags.shtml

Lets find out the years that has 5 Saturdays, 5 Sundays, 5 Mondays

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$ for i in {1900..2050}; do cal 12 $i|awk 'NR==1{year=$NF}NR==3&&NF==1{print year;exit}'; done
1900
1906
1917
1923
1928
1934
1945
1951
1956
1962
1973
1979
1984
1990
2001
2007
2012
2018
2029
2035
2040
2046

 

 

 

posted under Uncategorized | No Comments »

Armstrong number using awk command

November27

Armstrong number

Also known as narcissistic numbers, Armstrong numbers are the sum of their own digits to the power of the number of digits. As that is a slightly brief wording, let me give an example:

153 = 1³ + 5³ + 3³

Each digit is raised to the power three because 153 has three digits.

The  below code is used to find out the Armstrong numbers upto 2000. you can simply increase the value in {1..2000}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
for i in {1..2000};do echo $i | awk '{a=0;l=length($0);for(i=1;i<=l;i++){a+=$i^l}if(a>$0)exit}$0==a{print}' FS= ; done
1
2
3
4
5
6
7
8
9
153
370
371
407
1634
posted under Uncategorized | No Comments »

How to Resize the Picture in shell script / convert commnad

October23

First, we need a convert utility to resize the jpg images.

convert utility is from ImageMagick (open source tool)

If you dont have the convert utility, then install it by using the below command.

1
sudo apt-get install imagemagick

After installation, we are ready to resize the images.

execute the below command to resize the image to 50% from its original size

1
2
syntax:
convert source-image -resize 50% output-image
1
convert linux.jpg -resize 50% resize_linux.jpg

If you want to resize all the pictures in one folder, then try with this for loop

1
for i in *.jpg; do new_file=${i/.*/};convert ${i} -resize 50% ${i} ${new_file}_new.jpg; done

This convert utility converts the image from one format to another format ( Eg. jpg to png )

1
convert linux.jpg linux.png
posted under Uncategorized | No Comments »

Shorten long urls using curl command – shell script

September21

tinyurl is providing a api call to shorten the long url. So, we can make a api call using the curl command and get the short url from the long url.

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash
 
#  This script is used to shorten the long url
#  Author  : Kamaraj Subramanian
#  Website : www.thelinuxtips.com
 
echo -n "Enter the Long URL : "
read url
 
short_url=$(curl -s http://tinyurl.com/api-create.php?url=${url})
 
echo "Short URL is : ${short_url}"

Output

1
2
3
4
5
6
7
8
9
10
11
$ ./shorten.sh
Enter the Long URL : http://www.google.com/finance?cid=694653
Short URL is : http://tinyurl.com/lhu73
 
$ ./shorten.sh
Enter the Long URL : https://sites.google.com/site/gdevelopercodelabs/app-engine/python-codelab
Short URL is : http://tinyurl.com/2vb7e2e
 
$ ./shorten.sh
Enter the Long URL : http://www.flickr.com/services/apps/tags/Linux
Short URL is : http://tinyurl.com/9za9z32
posted under Uncategorized | No Comments »

Retrieve stock value using shell script

September19

we can use wget command to retrieve the stock value of given stock symbol from google finance website.

Shell script

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/bin/bash
 
#  This script is used to retrieve the stock value from finance.google.com
#  Author  : Kamaraj Subramanian
#  Website : www.thelinuxtips.com
 
echo -n "Enter the Stock Symbol : "
read stocksymbol
 
googleURL="http://finance.google.com/finance/info?client=ig\&q=NASDAQ%3a"
 
#Retrieve the stock value
wget $googleURL$stocksymbol -O /tmp/o.txt 2>/dev/null
stockvalue=$(awk '/,\"l\"/{print $NF}' /tmp/o.txt)
 
echo "Stock value of $stocksymbol is : $stockvalue"
 
<span style="text-decoration: underline;"><strong>Output</strong></span>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#GOOGLE
 
$ ./stockvalue.sh
Enter the Stock Symbol : GOOG
Stock value of GOOG is : "724.07"
 
#YAHOO
 
$ ./stockvalue.sh
Enter the Stock Symbol : YHOO
Stock value of YHOO is : "15.89"
 
#MICROSOFT
 
$ ./stockvalue.sh
Enter the Stock Symbol : MSFT
Stock value of MSFT is : "31.14"
 
#APPLE
 
$ ./stockvalue.sh
Enter the Stock Symbol : AAPL
Stock value of AAPL is : "701.23"
 
#FACEBOOK
 
$ ./stockvalue.sh
Enter the Stock Symbol : FB
Stock value of FB is : "22.86"
posted under Uncategorized | No Comments »

Sum of all digits in a given number using unix commands

September18

Using the below commands, we can sum of all the digits in a given number.

Eg:

If a given number is 123. Then the answer should be ( 1 + 2 + 3 ) = 6

1
2
3
4
5
6
7
8
9
10
$ echo "1234567" | grep -o . | awk '{a+=$0}END{print a}'
28
$ echo "1234567" | awk '{for (i=1;i <=length($0);i++)a+=substr($0,i,1);print a}'
28
$ echo "1234567" | fold -w1 | awk '{a+=$0}END{print a}'
28
$ echo "1234567" | sed "s/./&\n/g" | awk '{a+=$0}END{print a}'
28
$ echo "1234567" | awk '{for(i=1;i <=NF;i++){a+=$i;}print a}' FS=
28

 

posted under Uncategorized | No Comments »

Print/Read the $PATH variable in readable format

September17

The $PATH variable is specified as a list of one or more directory names separated by colon (:) characters

If you want to see the variable value, then just echo it.

my PATH variable contains the below directory names.

1
2
3
4
5
6
$ echo $PATH
/opt/sybase/ASE-15_0/jobscheduler/bin:/opt/sybase/ASE-15_0/bin:/opt/sybase/ASE-15_0/
install:/opt/sybase/ASEP/bin:/opt/sybase/DBISQL/bin:/opt/sybase/UAF-2_5/bin:/opt/sybase/
OCS-15_0/bin:/opt/sybase/ASE-15_0/jobscheduler/bin:/opt/sybase/ASE-15_0/bin:/opt/sybase/
ASE-15_0/install:/opt/sybase/ASEP/bin:/opt/sybase/DBISQL/bin:/opt/sybase/UAF-2_5/bin:
/opt/sybase/OCS-15_0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

well, the above output is not much readable format. Lets make/show it as a readable one.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$ echo $PATH | awk -v RS=: '1'
/opt/sybase/ASE-15_0/jobscheduler/bin
/opt/sybase/ASE-15_0/bin
/opt/sybase/ASE-15_0/install
/opt/sybase/ASEP/bin
/opt/sybase/DBISQL/bin
/opt/sybase/UAF-2_5/bin
/opt/sybase/OCS-15_0/bin
/opt/sybase/ASE-15_0/jobscheduler/bin
/opt/sybase/ASE-15_0/bin
/opt/sybase/ASE-15_0/install
/opt/sybase/ASEP/bin
/opt/sybase/DBISQL/bin
/opt/sybase/UAF-2_5/bin
/opt/sybase/OCS-15_0/bin
/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/sbin
/bin
/usr/games

using some more commands, we can make it as readable.

Below is the some of the commands combination.

1
2
3
4
5
6
7
8
$ echo $PATH | tr ":" "\n"
$ echo $PATH | sed "s/:/\n/g"
$ echo $PATH | perl -pe 's/:/\n/g'
$ echo $PATH | awk '{gsub(":","\n")}1'
$ IFS=:; for i in $PATH; do echo $i; done
$ echo $PATH | perl -lane 's/:/\n/g;print $_'
In KSH
print - ${PATH//:/\\n}

In unix, we always have more ways to achieve the same goal. UNIX ROCKS 🙂

posted under Uncategorized | No Comments »
« Older Entries

Recent Comments

    Categories