Search for words starting and ending with

Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Search for words starting and ending with
# 1  
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
Code:
 
 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
Code:
 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.

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

Kindly assist in getting the o/p
# 2  
Try
Code:
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.

# 3  
I take it this is COBOL so the first six columns don't count and column 7 must be a space.
# 4  
Quote:
Originally Posted by anijan
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.
[SNIP]
I tried the below but doesn seems to work. and I do not know to implement multiple search patterns for a single word.
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:

Quote:
im trying to search for a WORD in a file
OK: Note that not every grep understands "-w" but since you already used it i suppose yours does:

Code:
grep -w

Quote:
begins with a number
Code:
grep -w '[0-9][0-9]*'

Quote:
followed by a hypen
Code:
grep -w '[0-9][0-9]*-'

Quote:
follwed multiple words
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:

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

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

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

I hope this helps.

bakunin

Last edited by bakunin; 07-11-2017 at 10:50 PM..
This User Gave Thanks to bakunin For This Post:
# 5  
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?
This User Gave Thanks to RudiC For This Post:
# 6  
Quote:
Originally Posted by RudiC
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?
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
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #1010
Difficulty: Easy
The number 200 in base 8 equals 128 in base 10.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search words in any quote position and then change the words

hi, i need to replace all words in any quote position and then need to change the words inside the file thousand of raw. textfile data : "Ninguno","Confirma","JuicioABC" "JuicioCOMP","Recurso","JuicioABC" "JuicioDELL","Nulidad","Nosino" "Solidade","JuicioEUR","Segundo" need... (1 Reply)
Discussion started by: benjietambling
1 Replies

2. Shell Programming and Scripting

Count the words starting with 3-

OS : Oracle Linux 6.5 Shell : bash I have a file whose contents look like below. I want to count the number of occurences of strings starting with 3-. How can I do this ? I couldn't wordwrap the below line. Hence it looks long. '3-90892405251', '3-90892911050', '3-90893144163',... (8 Replies)
Discussion started by: John K
8 Replies

3. Shell Programming and Scripting

Text to column starting/ending with special character in each row

Hello, Here is my text data excerpted from the webpage: input My target is to get: What i tried is: sed 's/.*\(connector\)/1/' input > output but all characters coming before the word "connector" are deleted which is not good for me. My question: (9 Replies)
Discussion started by: baris35
9 Replies

4. Shell Programming and Scripting

Print words starting at particular positions

I have three words say abc def and ghi. I want to write them in a text file each starting (not ending) at particular positions say 1, 42 and 73. It is guaranteed that they will not overwrite. Length of the three variable may vary. Any help? (5 Replies)
Discussion started by: Soham
5 Replies

5. Shell Programming and Scripting

Sorting a list of words one per line by their ending

Hello, My OS is Windows and therefore DOS. Hence I have no access to Unix tools. I am trying to sort a file in Urdu by the character by which it ends. Each word is on a separate line. As input, an example in English would help: fruit banana apple pear house I need the sort to be on the... (5 Replies)
Discussion started by: gimley
5 Replies

6. Shell Programming and Scripting

Extract words starting with a pattern from a file

Hi Guys.. I have a file and i want to extract all words that starts with a pattern 'ABC_' or 'ADF_' For example, ABC.txt ---- INSERT INTO ABC_DLKFJAL_FJKLD SELECT DISTINCT S,B,C FROM ADF_DKF_KDFJ_IERU8 A, ABC_LKDJFREUE9_FJKDF B WHERE A.FI=B.EI; COMMIT; Output : ABS_DLKFJAL_FJKLD,... (5 Replies)
Discussion started by: Pramod_009
5 Replies

7. UNIX for Advanced & Expert Users

Pring starting and ending numbers using UNIX

Hi all, I need to do scrip for printing starting and ending numbers along with count in given file.:wall: Input: a.txt 10000030 10000029 10000028 10000027 10000026 10000024 10000023 10000021 10000018 10000018 10000017 10000016 10000015 10000014 (2 Replies)
Discussion started by: jackbell2013
2 Replies

8. Shell Programming and Scripting

if statement to check files with different ending but same starting name

I am trying to check if files staring with filename but ending with diffent dates e.g. filename.2011-10-25. The code I am using is below if It works find only if one file is present but returns binary operator expected when there are mulptiple files. Please help me correcting it. I... (5 Replies)
Discussion started by: ningy
5 Replies

9. Shell Programming and Scripting

How to remove all words starting from a matching word in a line

Hi Guys, I have a file like this: wwwe 1 ioie ewew yyy uuu 88 erehrlk 4 ihoiwhe lkjhassad lkhsad yyy mmm 45 jhash lhasdhs lkhsdkjsn ouiyrshroi oihoihswodnw oiyhewe yyy ggg 77 I want to remove everything after "yyy" and including "yyy" from each line in the file. So I want:... (2 Replies)
Discussion started by: npatwardhan
2 Replies

10. Shell Programming and Scripting

perl: reg.expr: combine starting and ending removal in one exprecion

Hello, I am new in perl and in regular exprecion; so I am looking for help (or an experienced advise.) The target is a triming spaces from a string: i.e., remove spases from begining and from end of a string. One of main point of a searched solution is performance: for current task it is... (2 Replies)
Discussion started by: alex_5161
2 Replies