AWK : Commands

 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 AWK

AWK stands for "Aho, Weinberger, and Kernighan". It is named after its creators Alfred Aho, Peter Weinberger, and Brian Kernighan, who developed the language while working at Bell Labs in the 1970s.


replace a word with another word

awk '{gsub(/sreejith/, "Balakrishnan"); print}' filename  // This will replace all the sreejith to Balakrishnan

Print a particular word if that appears in the file

awk '{for(i=1;i<=NF;i++) if ($i == "word") print $0}' filename

Print only once if a word was found in the file

awk '{for (i=1; i<=NF; i++) if ($i == "sreejith" && !seen[$i]++) print $i}' test-file.txt 

 Print the last word that has "america" as its last word

awk '$NF ~ /america$/' test-file.txt
or
awk '$NF ~ /america$/{print $0}' test-file.txt

  1. awk '{print}' filename: This command simply prints the entire contents of the specified file.

    awk '{print $1 " :  "$2}' filename

  2. awk '/pattern/' filename: This command prints all lines in the file that match the specified pattern.

  3. awk '/pattern/ {print $1}' filename: This command prints the first field (i.e., the first column) of all lines in the file that match the specified pattern.

  4. awk '{print $1,$3}' filename: This command prints the first and third fields (i.e., columns) of all lines in the file.

  5. awk '{sum += $1} END {print sum}' filename: This command calculates the sum of the first field in the file and prints the result at the end of processing.

  6. awk -f "," 'NR! =1{print $1} filename -- This will skip the first row NR - stands for no of rows. skipping the header

  7. awk 'NR!=1{print $1 " : " $2 " : " $3}' FS=" " test-file.txt

  8. awk 'NR==3 {print}' filename: This command prints the third line of the file.

  9. awk '{print NR,$0}' filename: This command prints the line number and the entire line for each line in the file.

  10. awk -F':' '{print $1}' /etc/passwd: This command prints the first field (i.e., username) of each line in the "/etc/passwd" file, where fields are separated by colons.
    awl '{print $1} FS="|" filename

  11. awk '/pattern/{print $0 > "outputfile"}' filename: This command writes all lines in the file that match the specified pattern to an output file named "outputfile".

  12. awk '{gsub(/oldpattern/, "newpattern")} {print}' filename: This command replaces all occurrences of "oldpattern" with "newpattern" in each line of the file and then prints the modified line.

 


Sure! Here are some more AWK commands:

  1. awk 'BEGIN{print "Start processing file"}{print $0}END{print "End of file"}' filename: This command prints a message at the beginning and end of processing a file, and prints each line in between.

  2. awk '/pattern/{print "Match found: " $0}' filename: This command prints a message before each line that matches the specified pattern.

  3. awk '$1 > 50{print $2}' filename: This command prints the second field (i.e., column) of all lines in the file where the first field is greater than 50.

  4. awk '{if($1 == "pattern"){print}}' filename: This command prints all lines in the file where the first field is equal to "pattern".

  5. awk '{print NF,$0}' filename: This command prints the number of fields (i.e., columns) and the entire line for each line in the file.

  6. awk '{for(i=1;i<=NF;i++){if($i ~ /pattern/){print $i}}}' filename: This command prints all fields that contain the specified pattern for each line in the file.

  7. awk '{print length($0)}' filename: This command prints the length of each line in the file.

  8. awk 'NR==FNR{a[$1]=$2;next}{if($1 in a){print a[$1],$2}}' file1 file2: This command reads file1 and creates an associative array "a" with the first field as key and second field as value. It then reads file2 and prints the value corresponding to the first field in file1 and the second field in file2 if the first field is present in file1.

  9. awk 'length($0) > 10{print substr($0,1,10) "..."}' filename: This command prints the first 10 characters of each line in the file, followed by ellipsis ("..."), for lines longer than 10 characters.

  10. awk '{sum += $1} NR%5 == 0{print "Sum of every 5 lines: " sum; sum=0} END{print "Final sum: " sum}' filename: This command calculates the sum of the first field for every 5 lines in the file, and prints the partial sums and the final sum at the end of processing.




====UDMY

We are going to fetch data from a file where each column is seaperated by space

Fetching any specific column from a file.


awk '{print $1}' <file_name>   

In this case we are not giving any seperator - by default space is the seperator.


Fetching multiple column at a time.

awk '{print $1 " , " $2}' filename


Fetching data in any specific order

change the order 

awk '{print $3 " , " $2 " , " $1 }' filename

Fetch complete data from a file (All rows and all column)

awk '{print $0}' 


How can we fetch data by giving some separator value.

awk -F  " , "  '{print $1}' filename


--> awk -F " , "  -- if the file is separated by comma

-- > awk -F " | " -- if the file is separated by pipe symbol 

you can even use FS=" | "  instead of awk -F  -- this will also work


awk  '{print $1}' FS=","  filename 
 
FS=" "  -- is given after the print command and awk -F is given before the print


How to display row , or choose rows of your choice where you can modify in such a way that your
chosen row and column is chosen

I do not want to display the header but I want to display all the record alone.

awk -F " , "  'NR!=1{print $1 "   " $2" filename

-- here NR -- No of rows not equal to 1 

If you want to print only the header 

awk -F ", " 'NR==1{print $1 "    " $2}' filename

if you want to display only row number 5

awk -f  " , " 'NR==5{print $1  "      "  $2}' filename

If you want to display all rows which are below the row number 7 or equal to 7

awk -f  " , " 'NR<=7{print $1  "      "  $2}' filename

And command NR>1 && NR

awk -F " ," 'NR>1 && NR<=7{print $1 "   " $3 "      "  $4}' filename

OR command NR>1 ||  NR

awk -F " ," 'NR<5  ||  NR>=407{print $1 "   " $3 "      "  $4}' filename

Directing the output to a file

awk -F " ," 'NR<5  ||  NR>=407{print $1 "   " $3 "      "  $4}' filename >  awk_result.txt


Conditional select data 

awk -F " , "  '$1==9{print $1 "  , " $2}' filename


awk -F " , "  '$1>30{print $1 "  , " $2}' filename



Display only , this will display column 1 where the name is Roger Nick

awk -F " , " "$1=="Roger Nick"{print $1 "   " $2  "    "  $3} filename

The below will display the complete line where the condition of the name is met.

awk -F " , " "$1=="Roger Nick"{print $0} filename


Using substring to print all the row wherever you find the substring


























Display all the rows that do not have gmail.com in them


awk -R "," '!/gmail.com/{print $0}  filename


I believe it it tild symbol but he say acute symbol

awk -R "," '$1~"U"{print $0}  filename

The above will display all the lines that has U

Not equal -- to search substring

awk -R "," '$1!~"U"{print $0}  filename

====

AWK -- Find text at the start of a line










awk -F  ","  '$1~/a/{print $0} filename

what if you want to print out all the rows that begin with "a"

awk -F  ","  '$1~/^a/{print $0} filename


















display all rows where "n" is the last character 









AWK with If statement


awk -F " | " '{if ($3=> 25000) print $0;}' filename







AWK IF condition with && 





















IF & ELSE WITH AWK 

awk -F " | "  '{ if  ($3>20000) print $2;
else print "*****" ; }' filename

IF ELSE / IF ELSE















BLOCKS AND LOOPS : AWK 














Here we need a header to be displayed and therefore we are using BEGIN BLOCK



Multiplying salary twice 




AWK END Block














AWK : While :



























































FOR LOOP AWK 

All the lines in the file is displayed twice. 












































Storing a AWK result to a variable


Splitting the file into different files bases on the column number 3

awk -F " " '{print > $3}' test-file.txt 

Splitting the files into multiple files by column data

awk -F " " '{print $1 " "$2 " " $3 > $6 }' filename  -- on the basis of column 6 the file will be split.


length of the string values.

awk -F " "  '{print $12 "       "  length($2}' filename

$ awk -F " " '{print $2 "            " length($2)}' test-file.txt 
Phone            5
7989849494            10
7989849494            10
7989849494            10

Functions : touppper / to lower

$ awk -F " " '{print $2 "            " length($2)}' test-file.txt 
Phone            5
7989849494            10
7989849494            10
7989849494            10

string functions



$ awk -F " " '{sub(/sreejith/,"SEETA", $1);print $1}' test-file.txt

Name
RAVANAN
RAVANAN
RAVANAN
RAVANAN
Name
SEETA
SEETA
SEETA
SEETA
SEETA
SEETA
SEETA

$ awk -F " " '{sub("sreejith","RAVANAN", $1);print $1}' test-file.txt
Name
RAVANAN
RAVANAN
RAVANAN

awk '{gsub(/sreejith/, "Balakrishnan"); print}' filename  // This will replace all the sreejith to Balakrishnan

gsub -- subsitutes globally

$ awk -F " " '{gsub("sreejith","RAVANAN", $1);print $1}' test-file.txt

Find Index number

awk -F " " '{print index($3 , "L")}' filename  //here it find the index of 3rd column wherever there is "L" character. index postion on the word is displayed.

$ awk -F " " '{print index($3 , "L")}' test-file.txt 
0
1
1
1
1


Substr:

$ awk -F "," '{print substr($2, 1, 4)}' filename


$ awk -F " " '{print substr($3, 1, 4)}' test-file.txt
addr
Lond
Lond

AWK Printf 

printf is used for formatting the results



%s - string
%d - integer data
















change the alignment to left 

















Zero before the value




























Comments

Popular posts from this blog

CURL Commands

$$$$ Symbols in shell scripting