Pages

Saturday, September 5, 2020

Removing Duplicates Using AWK and How it Works.

<script data-ad-client="ca-pub-7841181112240136" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>

As a normal method in unix we need to use sort and then uniq to remove the duplicates from the file. Because without sort, it wont give correct unique values.

Instead of that we can use awk command to remove the duplicates in the file. For this we need to use awk associate array.

Short notes about AWK associative array

Unlike regular arrays in AWK associative arrays the indexes need not to be continuous set of number; you can use either string or number as an array index. Also, there is no need to declare the size of an array in advance – arrays can expand/shrink at runtime.

Its syntax is : array_name[index] = value

Example

Cat tes 

File Contents

Cat test

1

2

3

1

1

3

3a

3a

5

Command to Use: cat tes | awk '!seen[$0]++'  

How it Works

Seen[$0] - Uses the current line as the key to the array a. In our case 1 so array index will become 1 2 3 3a and 5. As we can't have same array index.

Note : Seen is an arbitrary word, it can be any words like a, b or any strings.

if seen[1] is never reference before then a[1] evalutes to empty string as awk will crate empty if it was not initialized before. IN this zero is false. If we negate then we will get true result. if it is non-zero (true) then we will get false result.

uses the current line $0 as key to the array a, taking the value stored there. If this particular key was never referenced before, a[$0] evaluates to the empty string.

!seen[$0]

The ! negates the value from before. If it was empty or zero (false), we now have a true result. If it was non-zero (true), we have a false result. If the whole expression evaluated to true, meaning that a[$0] was not set to begin with, the whole line is printed as the default action.

Also, regardless of the old value, the post-increment operator adds one to a[$0], so the next the same value in the array is accessed, it will be positive and the whole condition will fail.

Below is the actual way how awk with expression works

    awk 'expression' file Is actually a short hand of: awk 'expression {print $0}' file

Whenever the a test with no associated action is true, the default action is triggered. The default action is the equivalent of { print } or { print $0 }, which prints the current record, which for all accounts and purposes in this example is the current unmodified line of input.

Thursday, August 22, 2019

AWK Associative array

Example HTML page

arrayname[string]=value

Syntax:

for (var in arrayname)
actions


Code to identify shell used by how many users

Sample Code

BEGIN {
        FS=":"
}
{user_shells[$7] +=1}
END{
        for (shell in user_shells)
                print shell "\t\t" user_shells[shell]
}




How it Works

We are creating an array name called user_shells for which we are mentioning index item is  present on field 7. If the value of field 7 repeats then the respective index value is increased by 1. 

to access the array we will be using for loop. for loop of awk will work slight differently compare to bash for loop. 

In case of awk for loop, variable mentioned will be used as a index of the array. So printing the declared variable in awk will print the array index and the second part will print the array values for the respective index.

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