Friday, March 28, 2014

Do you need Linux command line power Right Now? - SimpleShell



- Often we may not have access to a linux system, but we will need to check a man page or command's options or a sed or awk or a perl one-liner...
 
-Do you need Linux command line power Right Now?
-Want to try some tool or programming language without hassle of installing it?

SimpleShellbeta is here for you!
-Free terminal emulator + Linux shell account accessible with plain web browser, no registration required.
-Only major limitation is 15 minutes session timeout, ideal for one-time tasks. Just click Start my session. 



~ Live in Linux~

Wednesday, March 19, 2014

S&P 4 – loops in backgrond

Came to know recently that even loops, similar to running a command, can run on background.
Adding a "&" makes a command to run in background.                                                                     Similar to that, even loops can be run on background.
#!/bin/bash
# background-loop.sh
for i in 1 2 3 4 5 6 7 8 9 10 # First loop.
  do
    echo -n “$i “
    sleep 1 # we will need to test with this sleep and without this sleep
done & # Run this loop in background.
# Will sometimes execute after second loop.
 echo first loop # This ‘echo’ sometimes will not display.
 
 for i in 11 12 13 14 15 16 17 18 19 20 # Second loop.
    do
       echo -n “$i “
       sleep 1  # we will need to test with this sleep and without this sleep
    done
echo # This ‘echo’ sometimes will not display.
 
# ======================================================
# without the sleep 1, the expected output from the script:
# 1 2 3 4 5 6 7 8 9 10
# 11 12 13 14 15 16 17 18 19 20
 
# Sometimes, though, you get:
# 11 12 13 14 15 16 17 18 19 20
# 1 2 3 4 5 6 7 8 9 10 bozo $
# (The second ‘echo’ doesn’t execute. Why?)
 
# Occasionally also:
# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
# (The first ‘echo’ doesn’t execute. Why?)
 
# Very rarely something like:
# 11 12 13 1 2 3 4 5 6 7 8 9 10 14 15 16 17 18 19 20
# The foreground loop preempts the background one.
exit 0
 
with that sleep 1, its really funny.

Wednesday, March 12, 2014

S&P 3 - tail and head to show lines middle of the file / head - tail command to show lines in between x and y

A close friend of mine asked me - which command will show the lines in between 90 and 100?

looks like a simple question, isnt. but its a tricky one.

He gave me the choices as well:
1. tail -n 90-100 file
2. head -n 100 | tail -n 10
3. head -n 90-100 file
4. list -n 90-100 file

after seeing the choices, it is confusing, isnt?!?!

ok, lets remember these 5 rules:

rule 1. head -20 <filename> ---will show first 20 lines of the <filename>.
rule 2. tail -20 <filename> ---will show the last 20 lines of the <filename>
 

3. remember a rule, head or tail, dont work with just the numbers. the numbers should have a minus sign or plus sign.
(if you try head 20 filename, it will look for a filename 20 and secondfile filename, and it will show first ten lines from both file, provided that a file

exists with the name "20". or it will say, "20" file does not exist and it will show the first ten lines of the filename)
head <N> <filename>
tail <N> <filename>

4. in case of head, avoid "+n"

to summarize:

tail n <filename>  ==> it wont work
tail -n <filename>  =======> will show last "n" lines
tail +n <filename> (rule 5, please check below)

head -n <filename>  =======> will show first "n" lines
head +n <filename> ==> it wont work
head n <filename>  ==> it wont work
 

rule 5. tail +n <filename> will show the file starting from line n, till the end of the file. please note it looks like it works only on Posix standard tail. so,
better to avoid this as well.


ok now, to show the lines in between 90 to 100:
-- show first 100 lines, cut out and show only last ten lines(90 to 100)
head -100 <filename> | tail -10

Monday, March 10, 2014

regex - Character Classes or Character Sets and alternatives(this or that)

Good day to all...
Lets learn two basic and important RegEx..

Character Classes  or Character Sets -  [ ]
As seen on regular-expressions dot info/charclass.html:

With a "character class", also called "character set", you can tell the regex engine to match only one out of several characters. Simply place the characters you want to match between square brackets. If you want to match an a or an e, use [ae]. You could use this in gr[ae]y to match either gray or grey. Very useful if you do not know whether the document you are searching through is written in American or British English.

[aA]pple -- will match for apple or Apple
[^apple]  -- will match for anything other than apple
[0-9] -- will match for 0 to 9
[0-9][A-F] -- will match for a number and a char from A to F
[0-9A-F] -- is equal to [0-9][A-F]
[0-9A-Fa-f] -- if u want to include lowercase a to f as well.

Alternatives -- this or that - |
if you want to match this or that, you can use this|that

That's either 'this' or 'that'. But what if we wanted either 'yes' or 'yet'? To get alternatives on
part of an expression, we need to group the options. In a regular expression, grouping is always
done with parentheses:
To match yes or yet, you can use - ye(t|s)

Please let me know how its going on... unless you say it, i cant understand your mind ;)
thanks, happy coding. happy regex!.

Saturday, March 8, 2014

SP2 - Regex basic quiz

Testing ..testing testing...will update this with detailed RegEx information and much more soon... Stay tuned. thank you...

Perl RegEx

Perl Basics RegEx quiz

  1. print "It matches\n" if "Hello World" =~ /World/; -- will it print?

  2. yes, it will print
    no, it wont print
    Error. print and regex cant be on the same line.

  3. $greeting = "world"; print "It matches\n" if "Hello World" REGEX; ...what will be REGEX here?

  4. =~ /"$greeting"/
    =~ /$greeting/i
    !~ /$greeting/i

  5. If you're matching against $_, the $_ =~ part can be omitted:

  6. true
    false
    depends

Friday, March 7, 2014

Shell and Perl 1 - Unix/Linux's wc command implementation using Perl while loop & regex

#!/usr/bin/perl -w
# invent sekar  - 3/5/2014 - wednesday
# Perl implementation of Linux wc command, using a while loop and of course, the great regex ;).

print "Please enter line1:\n";
my $line1=<STDIN>;
chomp $line1;

$line2="A quiet bubble floating on a sea of noise. - The god of small things - Arundhati Roy";
$line3="Hello how are you";

################################
# Finding number of words: wc -w
#################################
print "Finding numer of words: wc -w\n";
print "---------------------------------\n";
$nWords=0;

while ($line1 =~ /\b\w+\b/g){ $nWords++; }
print "line1 is $line1\n";
print "\$line1 contains $nWords words\n";

$nWords=0;
while ($line2 =~ /\b\w+\b/g){ $nWords++; }
print "line2 is $line2\n";
print "\$line2 contains $nWords words\n";

$nWords=0;
while ($line3 =~ /\b\w+\b/g){ $nWords++; }
print "line3 is $line3\n";
print "\$line3 contains $nWords words\n\n";

################################
# Finding number of characters: wc -m
#################################
print "Finding numer of characters : wc -m\n";
print "---------------------------------\n";

$nChar=0;
while($line1 =~ /./g){$nChar++;}
print "line1 is $line1\n";
print "\$line1 contains $nChar characters\n";

$nChar=0;
while($line2 =~ /./g){$nChar++;}
print "line2 is $line2\n";
print "\$line2 contains $nChar characters\n";

$nChar=0;
while($line3 =~ /./g){$nChar++;}
print "line3 is $line3\n";
print "\$line3 contains $nChar characters\n";

A Sample Run:
# ./wc-for-loop-test.pl
Please enter line1:
hiiiiiiii
Finding numer of words: wc -w
---------------------------------
line1 is hiiiiiiii
$line1 contains 1 words
line2 is A quiet bubble floating on a sea of noise. - The god of small things - Arundhati Roy
$line2 contains 16 words
line3 is Hello how are you
$line3 contains 4 words

Finding numer of characters : wc -m
---------------------------------
line1 is hiiiiiiii
$line1 contains 9 characters
line2 is A quiet bubble floating on a sea of noise. - The god of small things - Arundhati Roy
$line2 contains 84 characters
line3 is Hello how are you
$line3 contains 17 characters