Search for words starting and ending with

im trying to search for a WORD in a file which
begins with a number
followed by a hypen
follwed multiple words
and end with a dot "."
and pront the entire line which matches the above.

Please note that there is a space at the begining of each line

i/p file

 
 19458 00000-CONTROL-PARA.
 19453 LNK-EDCOMON-PARMS   LNK-EDCOMON-PARMS.
 19497     COPY EDPC.
 26531 50048-CHECK-IF-FS-FAIL-4-QR7.
 26862 50053-CHECK-INDV-RCV-TFS.
 26862 50053-CHECK-INDV-RCV-TFS.
 19497     COPY EDPC.

o/p should be like

 19458 00000-CONTROL-PARA.
 26531 50048-CHECK-IF-FS-FAIL-4-QR7.
 26862 50053-CHECK-INDV-RCV-TFS.
 26862 50053-CHECK-INDV-RCV-TFS.

I tried the below but doesn seems to work. and I do not know to implement multiple search patterns for a single word.

grep -w "\b[0-9]*\.$" input
grep -w "[0-9]*\.$" input

Kindly assist in getting the o/p

Try

grep -E "[[:digit:]]+(-[[:alnum:]]+)+\." file
 19458 00000-CONTROL-PARA.
 26531 50048-CHECK-IF-FS-FAIL-4-QR7.
 26862 50053-CHECK-INDV-RCV-TFS.
 26862 50053-CHECK-INDV-RCV-TFS.

I take it this is COBOL so the first six columns don't count and column 7 must be a space.

Such a consistent formulation of a question deems a longer answer: you won't need multiple search patterns, you will only need one - and because you have already exactly specified what you need it is quite simple. Let us try:

OK: Note that not every grep understands "-w" but since you already used it i suppose yours does:

grep -w
grep -w '[0-9][0-9]*'
grep -w '[0-9][0-9]*-'

It is not clear what you mean by "words" here because it is definitely not the same meaning as you used above. I take it (from your example) that you mean sequences of capitalized characters, digits or hyphens, yes? If so:

grep -w '[0-9][0-9]*-[A-Z0-9-]*'

We need to escape the dot, because it has a special meaning to grep ("any character"):

grep -w '[0-9][0-9]*-[A-Z0-9-]*\.'

I hope this helps.

bakunin

1 Like

Nice approach. You might want to add a plus sign or a star to allow for multiple words. And, not sure if two or more adjacent minus signs are acceptable?

1 Like

You are right, the missing asterisk was a typo, which i have corrected now. Thank you for spotting it.

You are right about adjacent hyphens maybe being not allowed, but that is a lack of information from the threads o/p. If he can better specify his needs i can construct better suited regexps. The point, though, was to show how a regexp can be constructed from a specification at all.

all the best
bakunin