Search for a pattern in part of the line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search for a pattern in part of the line
# 1  
Old 09-10-2008
Search for a pattern in part of the line

Hi guys,

I need to search for a particular pattern within the first 5 chars of the line. If the pattern is found then output the whole line. Unfortunately grep only searches the whole line

For example if i am searching for aaaaa in these lines

aaaaabbbbbccccc
bbbbbcccccaaaaa

grep would return both lines but i only want the first line. Also the search pattern (aaaaa) needs to be read from an input file as there could be more than one search patern

Any tips!!!
# 2  
Old 09-10-2008
Code:
egrep '^.{0,4}aaaaa' file

... assuming you mean it can start in any position from 0 through 5. If the first five characters need to be precisely aaaaa then you don't even need egrep.

Code:
 grep '^aaaaa' file

To read the patterns from a file, something like

Code:
sed -e 's%^%^.{0,4}%' patternfile | egrep -f - file

If your egrep doesn't permit -f - or doesn't have the -f option, there are workarounds, of course.

Last edited by era; 09-10-2008 at 09:25 AM.. Reason: Reading patterns from a file
# 3  
Old 09-10-2008
Quote:
Originally Posted by pineapples
Hi guys,

I need to search for a particular pattern within the first 5 chars of the line. If the pattern is found then output the whole line. Unfortunately grep only searches the whole line

For example if i am searching for aaaaa in these lines

aaaaabbbbbccccc
bbbbbcccccaaaaa

grep would return both lines but i only want the first line. Also the search pattern (aaaaa) needs to be read from an input file as there could be more than one search patern

Any tips!!!
Grep can evaluate based on regular expressions as well. See the -E flag.

Code:
grep -E "^aaaaa" <file.to.search>

I think you can combine the -f flag as well to read inputs from a file.

Using sed
Code:
sed -n "/^aaaaa/p" <file.to.search>

# 4  
Old 09-10-2008
So if i wanted to read in the input file (ie file full of patterns) it would be something like this i assume :-

cat input file || grep '^aaaaa' file

What does the ^ do exactly ?
# 5  
Old 09-10-2008
Quote:
Originally Posted by vino
Grep can evaluate based on regular expressions as well. See the -E flag.

Code:
grep -E "^aaaaa" <file.to.search>

I think you can combine the -f flag as well to read inputs from a file.

Using sed
Code:
sed -n "/^aaaaa/p" <file.to.search>

sorry being dum vino - how do i pass in the pattern file ? Would they below work ?

would grep -f {pattern.file} -E "^" {file.to.search}
# 6  
Old 09-10-2008
Code:
while read -r line
do
 case $line in
    aaaaa* ) echo $line;;
 esac
done < file

# 7  
Old 09-10-2008
The sed script I posted above modifies each line in patternfile suitably, and feeds the result to egrep -f, so it should be a complete solution to your problem. The ^ in a regular expression is an "anchor" which matches beginning of line. Read an introduction to regular expressions to learn more. Here's that script again, adjusted to my current understanding of your requirements.

Code:
sed 's/^/^/' patternfile | grep -f - file


Last edited by era; 09-10-2008 at 11:44 AM.. Reason: Add a rehash of the sed script for grep
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search a pattern in a line and remove another pattern

Hi, I want to search a pattern in a text file and remove another pattern in that file. my text file look like this 0.000000 1.970000 F 303 - 1.970000 2.080000 VH VH + 2.080000 2.250000 VH VH + 2.250000 2.330000 VH L - 2.330000 2.360000 F H + 2.360000 2.410000 L VL - 2.410000 ... (6 Replies)
Discussion started by: sreejithalokkan
6 Replies

2. Shell Programming and Scripting

Search for a pattern that is in the middle of the line.

Hello, How do I use sed to search for a pattern in the middle of a line and delete the whole line. The command I have is as below. sed -i '/soft nofile/ d' /etc/security/limits.conf I have some *'s and spaces in front of the string. With the above command I'm not able to delete it. Any... (3 Replies)
Discussion started by: jawaugh
3 Replies

3. Shell Programming and Scripting

Search a pattern and add new line below

Hi, I have 2 files like below. File A: apple mango File B: start abc def apple ghi end start cba fed (4 Replies)
Discussion started by: jayadanabalan
4 Replies

4. Shell Programming and Scripting

How to search pattern and add that pattern in next line

Hi All, I am new to shell scripting and need help in scripting using CSH. Here is what I am trying to so, 1. Search a specific string e.g. "task" from "task (input1, out1)". 2. Extract the arguements "input1" and "out1" 3. Add them in separate lines below. eg. "int input1" , " integer out1" ... (7 Replies)
Discussion started by: deshiashish
7 Replies

5. Shell Programming and Scripting

search more than one pattern with perl on same line

Hi friends, I want to search for some hex error codes in some files. After the hex error code is found, the occurences would be counted. Afterwards the found hex errorcode would be cat into a separate file. Here is my code: #!/usr/bin/perl use File::Basename; my $find = $ARGV; my... (2 Replies)
Discussion started by: sdohn
2 Replies

6. Shell Programming and Scripting

find the line starting with a pattern and save a part in variable

Hi i have a file which has mutiple line in it. inside that i have a pattern similar to this /abc/def/hij i want to fine the pattern starting with "/" and get the first word in between the the symbols "/" i.e. "abc" in this case into a variable. thanks in advance (13 Replies)
Discussion started by: kichu
13 Replies

7. Shell Programming and Scripting

search a pattern and replace the whole line

Hi All, I have a requirement where I have to find a pattern in a file and comment the whole line containing the search pattern. Any ideas in shell is welcome. Thanks in advance, Regards, Arun (3 Replies)
Discussion started by: arun_maffy
3 Replies

8. UNIX for Dummies Questions & Answers

modify a particular pattern starting from second line of the search pattern

Hi, I think you ppl did not get my question correctly, let me explain I have 1.txt with following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433 ** ** ** In file 2.txt I have the following entries as shown: ... (1 Reply)
Discussion started by: imas
1 Replies

9. UNIX for Dummies Questions & Answers

modify a particular pattern starting from second line of the search pattern

Hi, I am new to this forum and i would like to get help in this issue. I have a file 1.txt as shown: apple banana orange apple grapes banana orange grapes orange .... Now i would like to search for pattern say apple or orange and then put a # at the beginning of the pattern... (2 Replies)
Discussion started by: imas
2 Replies

10. Solaris

How do i grep a pattern from part of LINE

Hi how do i grep only a part of the line from a file from all the lines. file: asdfgh 543212376 nag lkjh abhilash 543757858 How do i grep and print only 543212376 and 543757858 Can i grep something like 543* and print only that. (3 Replies)
Discussion started by: 123nagendrabhi
3 Replies
Login or Register to Ask a Question