Input File:F_Data_File.txt
1001|A1|10|Y
1002|A2|20|Y
1003|A3|30|N
1004|A4|10|Y
1005|A5|10|N
1006|A6|20|N
1007|A7|20|N
1008|A8|30|N
Syntax: awk '/Regular_Expression/ '
Find the record which contains A1
$ awk '/A1/ {print}' F_Data_File.txt
(Or)
$ awk '/A1/ {print $0}' F_Data_File.txt
(Or)
$ awk '$0 ~ /A1/{print}' F_Data_File.txt
OutPut:
1001|A1|10|Y
1002|A2|20|Y
1003|A3|30|N
1004|A4|10|Y
1005|A5|10|N
1006|A6|20|N
1007|A7|20|N
1008|A8|30|N
Syntax: awk '/Regular_Expression/ '
Find the record which contains A1
$ awk '/A1/ {print}' F_Data_File.txt
(Or)
$ awk '/A1/ {print $0}' F_Data_File.txt
(Or)
$ awk '$0 ~ /A1/{print}' F_Data_File.txt
Output:1001|A1|10|Y
Above commands will return the rows which have A1 available inthe file. 2nd way of writing says, If the line($0) contains(~) the pattern A1. ~ is used for matching.
Display the 3rd field of a record which contains A1
$ awk '/A1/ {print $3}' F_Data_File.txt
Output: Blank
awk will not understand $3 in this case as we have not provided file field separator. Below command will return desired output.
$ awk -F"|" '/A1/ {print $3}' F_Data_File.txt
(or)
$ awk 'BEGIN{FS="|"} /A1/ {print $3}' F_Data_File.txt
Output: 10
Find the records from the file where A1,A3,A5 and A8 is available
$ awk -F"|" '/A1|A3|A5|A8/ {print $0}' F_Data_File.txt
OutPut:
1001|A1|10|Y
1003|A3|30|N
1005|A5|10|N
1008|A8|30|N
1005|A5|10|N
1008|A8|30|N
Find record between two expression, record between A1 and A4
$ awk -F"|" '/A1/,/A4/ {print $0}' F_Data_File.txt
OutPut:
1001|A1|10|Y
1002|A2|20|Y
1003|A3|30|N
1004|A4|10|Y
Find record which doesn't have A1
$ awk -F"|" '!/A1/ {print $0}' F_Data_File.txt
(Or)
$ awk -F"|" '$0 !~ /A1/ {print $0}' F_Data_File.txt
OutPut:
1002|A2|20|Y
1003|A3|30|N
1004|A4|10|Y
1005|A5|10|N
1006|A6|20|N
1007|A7|20|N
1008|A8|30|N
Find record which doesn't have A1, A3 and A5
$ awk -F"|" '!/A1|A3|A5/ {print $0}' F_Data_File.txt
(Or)
$ awk -F"|" '$0 !~ /A1|A3|A5/ {print $0}' F_Data_File.txt
Find record which doesn't have A1
$ awk -F"|" '!/A1/ {print $0}' F_Data_File.txt
(Or)
$ awk -F"|" '$0 !~ /A1/ {print $0}' F_Data_File.txt
OutPut:
1002|A2|20|Y
1003|A3|30|N
1004|A4|10|Y
1005|A5|10|N
1006|A6|20|N
1007|A7|20|N
1008|A8|30|N
Find record which doesn't have A1, A3 and A5
$ awk -F"|" '!/A1|A3|A5/ {print $0}' F_Data_File.txt
(Or)
$ awk -F"|" '$0 !~ /A1|A3|A5/ {print $0}' F_Data_File.txt
OutPut:
1002|A2|20|Y
1004|A4|10|Y
1006|A6|20|N
1007|A7|20|N
1008|A8|30|N
No comments:
Post a Comment