Tuesday, April 28, 2015

Perl : Using awk type field matching in Perl

Suppose in the Apache log file we have an entry like the following

192.168.1.212 - - [24/Apr/2015:14:28:46 +0900] "POST / HTTP/1.0" 404 974 "-" "-" - 444

By default perl uses white space as field separator like awk
404 is the HTTP status code and it is the eighth field.

From the log file, to print only the lines with HTTP status as 404(which is the eight field)

cat /usr/local/apache/logs/access_log.20150424.gz | perl -lane 'print if @F[8] =~ /404/'

Few flags that make Perl more awk like, with field separators

-l makes each print statement output a record separator that is the same as input record separator (newline by default).
-Fpattern is used to specify input field separator, much like awk's -F option.

-a turns on the autosplit mode, so input fields are placed into @F array

A very common example is viewing fields in /etc/passwd

awk -F':' '{ print $1 }' /etc/passwd

perl -F':' -lane 'print @F[0]' /etc/passwd

Perl fields are @F[0], @F[1],...
awk fields are $1, $2, ...

To match the whole input line
awk - $0
Perl - $_

For more information refer
http://lifecs.likai.org/2008/10/using-perl-like-awk-and-sed.html




SSH : Pass Perl one-liner as argument to ssh

To pass perl one-liner as an argument to ssh command line, it can be done as follows

ssh server_ip_address << HERE zcat /usr/local/apache/logs/access_log.20150424.gz | perl -ne 'print if $_ =~ /" 404/'
HERE


Monday, April 27, 2015

Git : How to keep local repository in sync with remote repository?

To keep our  local repository in sync with the remote repository by fetching the updates from the remote repository, run the following from our local repo machine

git checkout master
git fetch origin
git merge origin/master

In Detail
=========
$ git branch -r                                                                                                            
  origin/HEAD -> origin/master
  origin/blue-feature
  origin/master

Here, "origin" refers to the remote central repo

$ git fetch origin
testuser@localhost's password:
remote: Counting objects: 5, done.                                                                                                                                      
remote: Compressing objects: 100% (3/3), done.                                                                                                                          
remote: Total 3 (delta 0), reused 0 (delta 0)                                                                                                                           
Unpacking objects: 100% (3/3), done.
From ssh://localhost/home/testuser/testgit/remote-repo
   854dc70..1d80eb3  master     -> origin/master

git fetch doesn't touch your local working tree at all. So gives you a little breathing space to decide what you want to do next. To actually bring the changes from the remote branch into your working tree, you have to do a git merge.

Before merging the remote repository into our local repository, to see the differences between our local repo and remote repo

$ git diff master origin/master                                                                                            
diff --git a/third.txt b/third.txt
index 6cfb7aa..6b22503 100644
--- a/third.txt
+++ b/third.txt
@@ -1,3 +1,5 @@
-thirs file
+this file
 saturday started work
 sunday let us see
+again sunday
+one more line

Now merge the remote repo "origin/master" into our local repo

$ git merge origin/master                                                                                                  
Updating 0b5c193..1d80eb3
Fast-forward
 third.txt |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)

Check if the update from remote repo has reflected in the local repo by checking a file which has the changes. In my case, it was a file by name "third.txt"

$ cat third.txt 
this file
saturday started work
sunday let us see
again sunday
one more line