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
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
awk '{print}' filename
: This command simply prints the entire contents of the specified file.
awk '{print $1 " : "$2}' filenameawk '/pattern/' filename
: This command prints all lines in the file that match the specified pattern.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.awk '{print $1,$3}' filename
: This command prints the first and third fields (i.e., columns) of all lines in the file.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.awk -f "," 'NR! =1{print $1} filename -- This will skip the first row NR - stands for no of rows. skipping the header
awk 'NR!=1{print $1 " : " $2 " : " $3}' FS=" " test-file.txt
awk 'NR==3 {print}' filename
: This command prints the third line of the file.awk '{print NR,$0}' filename
: This command prints the line number and the entire line for each line in the file.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="|" filenameawk '/pattern/{print $0 > "outputfile"}' filename
: This command writes all lines in the file that match the specified pattern to an output file named "outputfile".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:
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.awk '/pattern/{print "Match found: " $0}' filename
: This command prints a message before each line that matches the specified pattern.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.awk '{if($1 == "pattern"){print}}' filename
: This command prints all lines in the file where the first field is equal to "pattern".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.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.awk '{print length($0)}' filename
: This command prints the length of each line in the file.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.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.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.
printf is used for formatting the results
Comments
Post a Comment