Grep with ... matching more than 3 characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep with ... matching more than 3 characters
# 1  
Old 06-02-2014
Ubuntu Grep with ... matching more than 3 characters

I am trying to understand what the grep command in ubuntu is trying to do here.
The contents of my test file is given below
Code:
harsha@harsha-H67MA-USB3-B3:~/Documents$ cat data
abcd
efghi
jklmno
pqr
stuv
wxyz

When I grep for 3 dots (...) without the parenthesis as follows I would expect the output to match the first 3 characters in the above file. But the output I am seeing shows that it matches more than 3 characters in some lines. Can anyone help me here understand why the 3rd line is being shown to match more than 3 characters.

Code:
harsha@harsha-H67MA-USB3-B3:~/Documents$ grep ... data
abcd
efghi
jklmno
pqr
stuv
wxyz

# 2  
Old 06-02-2014
Grep 3 char from beginning... would this solve your problem ?
Code:
$ grep '^...' data

OR

$ grep -E '^.{3}' data

abcd
efghi
jklmno
pqr
stuv
wxyz

# 3  
Old 06-02-2014
Yes it solves.. but why does'nt
Code:
...

work only for the 3rd line Smilie
# 4  
Old 06-02-2014
Quote:
Originally Posted by sreeharshasn
Yes it solves.. but why does'nt
Code:
...

work only for the 3rd line Smilie
just ... match every 3 suppose if your line contains 6 or 9 char string then also it matches, 3 char as one set.

Execute this on your terminal, you will come to know

Code:
# considers 5 digits pairs  
$ echo '1234567.1234567' | grep -E '[[:digit:]]{5}'
1234567.1234567

# considers 5 digits from beginning 
$ echo '1234567.1234567' | grep -E '^[[:digit:]]{5}'
1234567.1234567

This User Gave Thanks to Akshay Hegde For This Post:
# 5  
Old 06-03-2014
If you are looking for just three character on a line (not zero, one or two & not four or more) then you need to mark the beginning and end of the line in your expression. The carat ^ marks the beggining and the dollar $ is the end of a record. With these you can set the expression and search exactly:-
Code:
grep "^...$" data

Using grep is great, but you have to be strict with your expression if that's what you need. You have to refine your expression(s) so that:-
  1. you don't miss data that you want
  2. you don't get data that you don't want

There is a good book by O'Reilly for sed & awk which has good explanations for expressions. It is worth a read. You don't have to get full into sed & awk but it's great to know as it will help in so many ways.




Robin
This User Gave Thanks to rbatte1 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Matching first n characters in a line in a file

hi to all, i am writing a simple file program in C which has to take a string of length n as input and match that input string with first n character of the line in file. if it does not match it has to compare with first n characters of next line. i am finding it difficult to know how exactly to... (2 Replies)
Discussion started by: ntrikoti
2 Replies

2. Shell Programming and Scripting

Matching a pattern 250 characters up and down stream

hii all i have a file a which contains some thing like this strand smthg position + yyx 3020 - yyw 10,000 now i have another file (file2) which contains the data starting from 1 to n positions i want to refer first file if + ... (4 Replies)
Discussion started by: anurupa777
4 Replies

3. Homework & Coursework Questions

Matching and Replacing Characters

I need to write an Unix script to report the number of SQL files in my home directory, ending with .sql . The script should, also, be checking whether there is a file with an underscore in its name. If that is the case, the underscore should be converted to a dash (‐); for example... (1 Reply)
Discussion started by: ronoz-4
1 Replies

4. UNIX for Dummies Questions & Answers

Matching the [] characters

I want to check where our programmers are using "delete" instead of "delete" in their C++ code: grep -r delete *cpp | grep -v However, this statement and variations thereof (escaping with backslash (\), single quotes ('), double quotes ("), accolades ({)) always lead to the following message:... (2 Replies)
Discussion started by: figaro
2 Replies

5. Homework & Coursework Questions

Check 2 variables for matching characters

hi i am writing a hangman script and am having trouble checking the correct letters against the word i need the script to compare the word against the letters guessed that are correct so once all the letters within the word have been guessed it will alow me to create a wining senario this must... (1 Reply)
Discussion started by: lsecer
1 Replies

6. Shell Programming and Scripting

Help with checking that 2 variables contain matching characters

hi i am writing a hangman script and am having trouble checking the correct letters against the word i need the script to compare the word against the letters guessed that are correct so once all the letters within the word have been guessed it will alow me to create a wining senario eg ... (3 Replies)
Discussion started by: lsecer
3 Replies

7. UNIX for Dummies Questions & Answers

Matching numbers of characters in two lines

Dear all, I'm stuck on a certain problem regarding counting the number of characters in one line and then adjusting the number of characters of another line to this number. This was my original input data: @HWI-ST471_57:1:1:1231:2079/2... (4 Replies)
Discussion started by: DerSeb
4 Replies

8. Shell Programming and Scripting

Matching a pattern between two characters (sed)

Hi there, I have a file with lines like these: 0105:ffff0000:001b:01f4:25:434 0299:ffff0000:0009:01f4:2:319 02d2:ffff0000:000e:01f4:2:507 The above values are split up using ":" characters. I would like capture each value, no matter what length. Take for example the first line... (8 Replies)
Discussion started by: MastaG
8 Replies

9. UNIX for Dummies Questions & Answers

pattern matching w/ unexpected characters

how do i check if the first character is a parenthese in pattern matching? if ] obviously doesnt work. How do I use it to compare so it doesnt try to use it to group. (5 Replies)
Discussion started by: questionasker
5 Replies

10. UNIX for Dummies Questions & Answers

matching characters between strings

please send the logic or program to find the matching characters between two strings for ex string1 :abc string2 :adc no .of matching characters is 2(a,c) (9 Replies)
Discussion started by: akmtcs
9 Replies
Login or Register to Ask a Question