Pages

Monday, April 8, 2019

rename Command

About rename

The rename command is used to change the name of multiple files.This command is slightly more advanced than mv because it requires the knowledge of, or at least a basic familiarity with, regular expressions.

We have two versions of rename package, a simpler version and a perl based version. perl based version has more advanced features.

If you have the simpler rename installed, you will see something like this instead.

       
$ rename --version 

rename from util-linux 2.33.1      
 

Perl based rename has below type of version.

       
$ rename --version

perl-rename 1.9
In this post, we are going to see the example for simpler version.

Syntax : rename 'old pattern' 'new pattern' input files

example : If you want to replace txt to text, below is the syntax
       
 rename 'txt' 'text' *.txt
       

Sunday, April 7, 2019

What is .Vimrc file

What is .vimrc

A file that contains initialization commands is called a "vimrc" file

Types of .vimrc
  • User vimrc 
  • System vimrc

Location of vimrc
  • User vimrc in $HOME (The user vimrc file often does not exist until created by the user. If you cannot find $HOME/.vimrc (or $HOME/_vimrc on Windows) then you can, and probably should, just create it.)
  • The system vimrc is normally under /etc in linux. The system vimrc should normally be left unmodified and it is not a good place you keep your personal settings. If you modify this file your changes may be overwritten if you ever upgrade vim. Also, changes here will affect other users on a multi-user system. In most cases, settings in the user vimrc will override settings in the system vimrc.

TIPS

1) :scriptnames list all the .vim files that Vim loaded for you, including your .vimrc file.

2) :e $MYVIMRC open & edit the current .vimrc that you are using, then use Ctrl + G to view the path in status bar.

To create your vimrc, start up vim and do one of the following:

3) :e $HOME/.vimrc  " on Unix, Mac or OS/2

Examples of vimrc contents

# Show line numbers
set number

# Show file stats
set ruler

Thursday, April 4, 2019

AWK match and substr function

MATCH Function

match(string, regexp [, array])

Search string for the longest, leftmost substring matched by the regular expression and return the character position (index) at which that substring begins (one, if it starts at the beginning of string). If no match is found, return zero.

The regexp argument may be either a regexp constant (/…/) or a string constant ("…"). 

Note: The order of the first two arguments is the opposite of most other string functions that work with regular expressions, such as sub() and gsub(). It might help to remember that for match(), the order is the same as for the ‘~’ operator: ‘string ~ regexp’.

The match() function sets the predefined variable RSTART to the index. It also sets the predefined variable RLENGTH to the length in characters of the matched substring. If no match is found, RSTART is set to zero, and RLENGTH to -1.

SUBSTR Function

substr(string, start [, length ])

Return a length-character-long substring of string, starting at character number start. The first character of a string is character number one.49 For example, substr("washington", 5, 3) returns "ing".

If length is not present, substr() returns the whole suffix of string that begins at character number start. For example, substr("washington", 5) returns "ington". The whole suffix is also returned if length is greater than the number of characters remaining in the string, counting from character start.

example:

AB: 20190131  13 J-1|19:30:00.000000000 18:06:00.000000000 123466  50 @TEST . "" 1234 - I . ".." "" "" "TEST TEXT 1" "TEXT 2: SAMPLE TEXT I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find f.==Required file.csv.gz FIELD*SERVER-TIME*05:29:51.981378000" "" NoTime

       
Command : awk 'match($0,/==.*?.csv.gz/){print $3","substr($0, RSTART+2, RLENGTH-2)}'  Sample file


Output  : 13,Required file.csv.gz