简体   繁体   中英

How can I search a substring on one field by using grep?

Suppose I have a csv file:

31,32,19,James Walker,321,James
21,31,49,Harry Bosh,282,Harry
32,12,03,Chris James,291,Chris

How can I use grep , not awk to search the occurrence of "James" in the fourth field?

How you can do it is by ensuring that your regex requires three commas to occur before the main pattern, and that the pattern (which has to scan within the field) does not itself match commas, which would cause it to match into the fifth or later field:

grep -E '^([^,]*,){3}[^,]*James'

"Match from the start of the line three repetitions of zero-or-more non-commas followed by a comma. That gets us to the fourth field. In the fourth field, match zero-or-more non-commas followed by James ."

What result do you want?

cat filename |grep "James" 

can't meet your requirement?

grep -P --color '(?<=,)James(?= +)' file
31,32,19,**James** Walker,321,James
grep -oP '(?<=,)James(?= +)' file 
James

Thanks guys! I finalize the operation to cut the file first to extract the field I want and then grep the string I want. FYI:

cut -d -f4 name.csv | grep 'James"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM