Friday, 8 June 2018

Filters and commands in Unix temp temp

Commonly used filter commands 

    awk
    cat
    comm
    cut
    expand
    compress
    fold
    grep
    head
    less
    more
    nl
    perl
    paste
    pr
    sed
    sh
    sort
    split
    strings
    tail
    tac
    tee
    tr
    uniq
    wc
    zcat

 

awk command:

Pattern scanning and text processing language. Finds patterns and Replaces patterns or text, validate, index and database sort
~$ man awk



Built In Variables:

Awk’s built-in variables include the field variables—$1, $2, $3, and so on ($0 is the entire line) — that break a line of text into individual words or pieces called fields.
NR: NR command keeps a current count of the number of input records. Remember that records are usually lines. Awk command performs the pattern/action statements once for each record in a file.
NF: NF command keeps a count of the number of fields within the current input record.
FS: FS command contains the field separator character which is used to divide fields on the input line. The default is “white space”, meaning space and tab characters. FS can be reassigned to another character (typically in BEGIN) to change the field separator.
RS: RS command stores the current record separator character. Since, by default, an input line is the input record, the default record separator character is a newline.
OFS: OFS command stores the output field separator, which separates the fields when Awk prints them. The default is a blank space. Whenever print has several parameters separated with commas, it will print the value of OFS in between each parameter.
ORS: ORS command stores the output record separator, which separates the output lines when Awk prints them. The default is a newline character. print automatically outputs the contents of ORS at the end of whatever it is given to print.

Here, we listed the root directory files detail and saved the same to the tesfile5
Here, awk command retries the field values of the column 1 and 6.



Below will prints only line which are non-empty. First and last line of testfile5 are empty. So, empty line only printed while displaying the  file using cat command.





Below image show some of the functions implemented using the awk command

 

cat command:

The cat ("concatenate") is filter command of operating systems like Linux/Unix. cat command used to create one or more files, view content of file, concatenate different files and can redirect the output to files or terminal.

In this post article, we are going to find out the very basic use  of the cat command and options related to the command with examples in Linux.

Syntax of the command

cat [OPTION] [FILE]...

(1) cat command to Create file and display File content in terminal

~$ cat > testfile1      //command to create file testfile1
~$ cat testfile            //command to display the content of the file to terminal

System creates file opens the blank file. then, user can write and press CNTRL+D to exit the file and the written content will be saved to file test3.





(2) Content Display for Multiple Files in terminal

~$ cat testfile1 testfile2
 Shows content of the testfile1 and at the end content testfile2


(3) Appending Standard Output with Redirection Operator 

~$ cat testfile3 >> testfile2
 Append the content of testfile3 to the end of testfile2. As we can see above the in the content display of filetest2.


(4) Redirection Operator Uses Standard Output to overwrite the file content. ~$ cat testfile3 > testfile4
 content of testfile4 will be overwritten by the testfile3 file content.

 (4) Redirection Operator Uses Standard Output to write Multiple Files content to a file

~$ cat testfile1 testfile2 > testfile4 

It's multiple file contents redirecting to a Single File
Conent of testfile1 and testfile3 overwritten to the testfile4

(5) Redirection Operator Uses Standard input to display content in terminal.
~$ cat < testfile4


Here the content of the file testfile4 will be used as input to the terminal. So, terminal will display the testfile4 content.

Comm Command:
comm compare two sorted files line by line and write to standard output; the lines that are common and the lines that are unique.


  -1 : hide first column (first column shows distinct lines in testfile6 )
  -2 : hide second column (second column shows distinct lines in testfile7 )
  -3 : hide third column (second column shows lines common in both files).





No comments:

Post a Comment

UGC NET Computer Science December 2019 | Question 16

Question 16 In a certain coding language. 'AEIOU' is written as 'TNHDZ'. Using the same coding language. 'BFJPV' wil...

Popular Posts