Username and password with wget/curl

 

To download a url with basic authentication we need to pass username & password.

To acheive this functionality with wget or curl observer following examples:

With curl:

$ curl -u 'username:password' 'http://www.example.com'

With wget:

$ wget --user='username' --password='password' 'http://www.example.com'

Wget with cookie:

Login to server & save cookie to a file:
$ wget --save-cookies cookiesFile.txt --post-data 'user=username&password=password' http://www.example.com/login.html

Download a page by using saved cookie file:
$ wget --load-cookies cookiesFile.txt -p http://www.example.com/somecontent.html

Advertisements

awk print Nth line after matching a pattern

 

By using awk we can print only Nth line after matching a pattern.

Lets say I have a file test.txt with following content and I want to extract 5 line after matching pattern.

$ cat test.txt

1
2
3
4
5
6
7
8
9

Use following awk code to extract Nth line:

Syntax:

$ awk 'c&&!--c;/pattern/{c=N}' file

where “pattern” is your input pattern and N is a line number to extract after matching pattern.

$ awk 'c&&!--c;/1/{c=5}' test.txt

Output:

6

In above example I am trying to extract 5th line after matching pattern, here my input pattern is 1 and 5th line after matching pattern is 6. So our output is 6.

Bash divide floating numbers

 

Dividing floating point number with bash/shell is bit tricky task.

To divide floating point number  we can use bc command.

Lets try with example, here I will divide 1.1 with 2 that is 1.1/2

By simply using bc you will get output as 0, try following command you should get output as 0.

$ echo "1.1/2" | bc

Output:

0

To get output as floating point number we need to use -l option with bc as shown below:

$ echo "1.1/2" | bc -l

Output:

.55000000000000000000

To truncate length of output we can use scale option with above expression.

Lets try to truncate length to 2 with following example:

$ echo "scale=2;1.1/2" | bc -l

Output:

.55

Lets try to truncate length to 2 with following example:

$ echo "scale=5;1.1/2" | bc -l

Output:

.55000