Nick Elias
Nick Elias

Reputation: 61

Not able to match colon using grep regexp

I have a large ASCII file. Each line contains a field like:

"id":"N119PM-1442267121-144-0"  

The double quotes are actually in the file, not my addition. The fields are delimited by commas but they do not necessarily appear in the same order from line to line, which means that using cut is not a viable option.

I have been using:

grep -o '"id":"[A-Aa-z0-9-]\+' <filename>  

and it works for the type of field shown above. But, there is a problem. A large number of these fields look like

"id":"JBU19-1442091600-schedule-0000:4"  

In other words, they have an extra colon and number at the end. I have not been able to select fields with these extra characters.

I've tried:

grep -o '"id":"[A-Aa-z0-9:-]\+' <filename>  
grep -o '"id":"[A-Aa-z0-9\:-]\+' <filename>  
grep -o '"id":"[A-Aa-z0-9-]\+\(:[0-9]\+\)' <filename>  

without success. Any help would be appreciated.

EDIT: I have also tried changing the : to % first then search on %, but this didn't work, either.

Upvotes: 0

Views: 988

Answers (1)

Ali Nikneshan
Ali Nikneshan

Reputation: 3502

If you are using GNU GREP, you can use -P in grep command

grep -oP '"id":"[A-Za-z0-9-:]+"' <filename>
"id":"N119PM-1442267121-144-0"
"id":"JBU19-1442091600-schedule-0000:4"

-P, --perl-regexp PATTERN is a Perl regular expression

Upvotes: 1

Related Questions