I need to grep all records from a file that has 1072 in 3rd column. 1072 can be prefixed by "SBC.", "CLS." or "DPT.". My search is just based on string 1072 in 3rd column. Delimiter in the file is tab. For example:
Input FIle:
"InvType" "Organization" "SBC.10720101" "CP.BUP.NY"
"InvType" "Organization" "SBC.10980101" "CP.BUP.NY"
"InvType" "Organization" "SBC.10720101" "CP.BUP.NY"
"InvType" "Organization" "SBC.10980101" "CP.BUP.NY"
"InvType" "Organization" "SBC.10980101" "CP.BUP.NY"
"InvType" "Organization" "CLS.107201" "CP.BUP.NY"
"InvType" "Organization" "SBC.10980101" "CP.BUP.NY"
"InvType" "Organization" "SBC.10980101" "CP.BUP.NY"
"InvType" "Organization" "SBC.10980101" "CP.BUP.NY"
"InvType" "Organization" "DPT.1072" "CP.BUP.NY"
"InvType" "Organization" "DPT.1098" "CP.BUP.NY"
Desired Results:
"InvType" "Organization" "SBC.10720101" "CP.BUP.NY"
"InvType" "Organization" "SBC.10720101" "CP.BUP.NY"
"InvType" "Organization" "CLS.107201" "CP.BUP.NY"
"InvType" "Organization" "DPT.1072" "CP.BUP.NY"
Please help....I tried something like awk -F "\t" '($3 == 1072) {print $0}' filename
Yoda
January 9, 2015, 12:22pm
2
Use regular expression comparison operator instead:-
'$3 ~ /1072/'
yale_work:
I need to grep all records from a file that has 1072 in 3rd column. 1072 can be prefixed by "SBC.", "CLS." or "DPT.". My search is just based on string 1072 in 3rd column. Delimiter in the file is tab. For example:
Input FIle:
"InvType" "Organization" "SBC.10720101" "CP.BUP.NY"
"InvType" "Organization" "SBC.10980101" "CP.BUP.NY"
"InvType" "Organization" "SBC.10720101" "CP.BUP.NY"
"InvType" "Organization" "SBC.10980101" "CP.BUP.NY"
"InvType" "Organization" "SBC.10980101" "CP.BUP.NY"
"InvType" "Organization" "CLS.107201" "CP.BUP.NY"
"InvType" "Organization" "SBC.10980101" "CP.BUP.NY"
"InvType" "Organization" "SBC.10980101" "CP.BUP.NY"
"InvType" "Organization" "SBC.10980101" "CP.BUP.NY"
"InvType" "Organization" "DPT.1072" "CP.BUP.NY"
"InvType" "Organization" "DPT.1098" "CP.BUP.NY"
Desired Results:
"InvType" "Organization" "SBC.10720101" "CP.BUP.NY"
"InvType" "Organization" "SBC.10720101" "CP.BUP.NY"
"InvType" "Organization" "CLS.107201" "CP.BUP.NY"
"InvType" "Organization" "DPT.1072" "CP.BUP.NY"
Please help....I tried something like awk -F "\t" '($3 == 1072) {print $0}' filename
Hello yale_work,
Following may help you in same.
awk '($3 ~ 1072)' Input_file
Output will be as follows.
"InvType" "Organization" "SBC.10720101" "CP.BUP.NY"
"InvType" "Organization" "SBC.10720101" "CP.BUP.NY"
"InvType" "Organization" "CLS.107201" "CP.BUP.NY"
"InvType" "Organization" "DPT.1072" "CP.BUP.NY"
Thanks,
R. Singh
ernie
January 9, 2015, 7:15pm
5
adding a line to the sample:
"InvType" "Organization" "SBC.10720101" "CP.BUP.NY"
"InvType" "Organization" "SBC.10980101" "CP.BUP.NY"
"InvType" "Organization" "SBC.10720101" "CP.BUP.NY"
"InvType" "Organization" "SBC.10980101" "CP.BUP.NY"
"InvType" "Organization" "SBC.10980101" "CP.BUP.NY"
"InvType" "Organization" "CLS.107201" "CP.BUP.NY"
"InvType" "Organization" "SBC.10980101" "CP.BUP.NY"
"InvType" "Organization" "SBC.10980101" "CP.BUP.NY"
"InvType" "Organization" "SBC.10980101" "CP.BUP.NY"
"InvType" "Organization" "DPT.1072" "CP.BUP.NY"
"InvType" "Organization" "DPT.1098" "CP.BUP.NY"
"InvType" "Organization" "DPT.10981072" "CP.BUP.NY"
both commands above produce
"InvType" "Organization" "SBC.10720101" "CP.BUP.NY"
"InvType" "Organization" "SBC.10720101" "CP.BUP.NY"
"InvType" "Organization" "CLS.107201" "CP.BUP.NY"
"InvType" "Organization" "DPT.1072" "CP.BUP.NY"
"InvType" "Organization" "DPT.10981072" "CP.BUP.NY"
As I read the OP requirement, the last line should not be included.
The following 2 lines only include lines with .1072
in them
I'm a scripting rookie.
I can't figure out how to get the prefix requirement into awk
or the 3rd element requirement into egrep
.
$ awk '($3 ~ /\.1072/)' Input_file
"InvType" "Organization" "SBC.10720101" "CP.BUP.NY"
"InvType" "Organization" "SBC.10720101" "CP.BUP.NY"
"InvType" "Organization" "CLS.107201" "CP.BUP.NY"
"InvType" "Organization" "DPT.1072" "CP.BUP.NY"
$ cat Input_file | egrep '(CLS|DPT|SBC)'[.]1072
"InvType" "Organization" "SBC.10720101" "CP.BUP.NY"
"InvType" "Organization" "SBC.10720101" "CP.BUP.NY"
"InvType" "Organization" "CLS.107201" "CP.BUP.NY"
"InvType" "Organization" "DPT.1072" "CP.BUP.NY"
Figured it out:
added the following 3 lines to the original sample that get displayed.
"InvType" "Organization" "DPT.10981072" "CP.BUP.NY"
"InvType" "Organization" "ABC.1072" "CP.BUP.NY"
"InvType" "Organization" "CDPT.1072" "CP.BUP.NY"
This line excludes them
awk '($3 ~ /^\"(CLS|DPT|SBC)\.1072/)' Input_file