Reputation: 69
Display the Employee details of all those working in a location which ends with ore
in its location name
EmpID:Name:Designation:UnitName:Location:DateofJoining:Salary
1001:Thomson:SE:IVS:Mumbai:10-Feb-1999:60000
1002:Johnson:TE::Bangalore:18-Jun-2000:50000
1003:Jackson:DM:IMS:Hyderabad:23-Apr-1985:90000
1004:BobGL::ETA:Mumbai:05-Jan-2004:55000
1005:Alice:PA:::26-Aug-2014:25000
1006:LilySE:IVS::Bangalore:17-Dec-2015:40000
1007:Kirsten:PM:IMS:Mumbai:26-Aug-2014:45000
1004:BobGL::ETA:Mumbai:05-Jan-2021:55000
Expected output:
1002:Johnson:TE::Bangalore:18-Jun-2000:50000
1006:LilySE:IVS::Bangalore:17-Dec-2015:40000
Here's the code I tried, it's only showing the location but I want full details
cut -d ":" -f4 employee.txt | grep 'ore\>'
EDIT: SOLVED
grep "`cut -d ":" -f5 employee.txt | grep 'ore\>'`$" employee.txt
got output:
1002:Johnson:TE::Bangalore:18-Jun-2000:50000
1006:LilySE:IVS::Bangalore:17-Dec-2015:40000
Thanks everyone for help :)
Upvotes: 4
Views: 258
Reputation: 53
awk -F: '{if (substr($5,1)~"ore")print $0}' emp.txt
This works for exact col match
grep -Ri "ore" emp.txt
This works for whole doc match
Upvotes: 0
Reputation: 1207
fgrep "ore:" employee.txt
f(ast)grep is exact string match no regex (same as grep -F
)
It would not know anything about which column it was matching, so that filter would have to happen before or after.
Upvotes: 0
Reputation: 133610
We could go with this simple awk
solution here. Without regex approach as per OP's requirement. Simple explanation would be: check if 5th field last 3 characters are ore then print that line.
awk 'BEGIN{FS=OFS=":"} substr($5,length($5)-2)=="ore"' Input_file
Generic answer: As per Ed sir's nice suggestion adding here more generic solution. Where one could set value of tail as per string needs to be looked upon.
awk 'BEGIN{FS=OFS=":"; tail="ore"} substr($5,length($5)-length(tail)+1)==tail' Input_file
Upvotes: 5
Reputation: 2981
Just in case you would change your mind to use regex.
Using awk:
$ awk -F: -v ends="ore" '$5~".*"ends' file.txt
1002:Johnson:TE::Bangalore:18-Jun-2000:50000
1006:LilySE:IVS::Bangalore:17-Dec-2015:40000
Using grep:
$ grep 'ore:' file.txt
1002:Johnson:TE::Bangalore:18-Jun-2000:50000
1006:LilySE:IVS::Bangalore:17-Dec-2015:40000
Or this:
$ grep -E '(.*:){4}.*ore:' file.txt
1002:Johnson:TE::Bangalore:18-Jun-2000:50000
1006:LilySE:IVS::Bangalore:17-Dec-2015:40000
Upvotes: 4
Reputation: 785541
Here a non-regex approach using awk
:
awk -F: -v s="ore" '(n=index($5,s)) && (n + length(s)-1) == length($5)' file
1002:Johnson:TE::Bangalore:18-Jun-2000:50000
1006:LilySE:IVS::Bangalore:17-Dec-2015:40000
Details:
index($5,s)
function finds position of input string ore
in fifth column i.e $5
of each line(index($5,s) + length(s)-1) == length($5)
check is to ensure that ore
is the ending substring of $5
A regex approach would be simpler:
awk -F: -v s="ore" '$5 ~ s "$"' file
Upvotes: 5
Reputation: 52529
Using just grep
(With a regular expression; the only way you can avoid them in grep
is using grep -F
, which does literal string matching):
$ grep -E '^([^:]*:){4}[^:]*ore:' input.txt
1002:Johnson:TE::Bangalore:18-Jun-2000:50000
1006:LilySE:IVS::Bangalore:17-Dec-2015:40000
Explanation:
Using Extended instead of Basic Regular Expression syntax for readability:
Starting at the beginning of the line, matches four fields (0 or more non-:
characters followed by a :
), and then a fifth field that ends in ore (Again, 0 or more non-:
characters, then o
, r
, e
, and finally the :
at the end of the field).
Upvotes: 4