How to search pattern in file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to search pattern in file?
# 1  
Old 07-21-2014
How to search pattern in file?

Hi I want to search pattern in file "S12345678B" which start with S and end with B total length is 10,numeric part 12345678 is variable.
e.g S99001229B and S88226901B.
please help
# 2  
Old 07-21-2014
Code:
awk '/S[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]B/' file

---------- Post updated at 06:53 AM ---------- Previous update was at 06:50 AM ----------

If you want to print all the words matching the pattern
Code:
awk '{for(i = 1; i <= NF; i++) {if($i ~ /^S[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]B$/) print $i}}' file

# 3  
Old 07-21-2014
Thanks million

---------- Post updated at 09:30 AM ---------- Previous update was at 06:16 AM ----------

Code:
awk '{for(i = 1; i <= NF; i++) 
{if($i ~ /^S[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]B$/)
 print $i}}'

file,This is not working what is NF?

Last edited by Corona688; 07-21-2014 at 11:59 AM..
# 4  
Old 07-21-2014
Moderator's Comments:
Mod Comment Posting "Does not work" without explanation does not help you or anyone. If a command does not work for you, please show the exact circumstances you used it, and the exact error or malfunction you received. Do not paraphrase errors, or post the text as links, images, or attachments if you can avoid it: Paste the exact message, in code tags, like [code] text [/code] or by selecting the text and using the Image button.

Thank you.

The UNIX and Linux Forums
# 5  
Old 07-21-2014
The below code will print all the lines containing the required pattern,
Code:
awk '/S[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]B/{print}' file.txt

Ex:
file.txt
abcS12345678B hidi
ghdgh S87654321B hi
Hi helloo

output:
abcS12345678B hidi
ghdgh S87654321B hi


If you want the pattern to be a word, use the below code,
Code:
awk '/\<S[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]B\>/{print}' file1.txt

Output will be :
ghdgh S87654321B hi

The characters "\<" "\>" are used to search for a word pattern.
This User Gave Thanks to DHeisenberg For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

2. UNIX for Dummies Questions & Answers

Search pattern in File

Hi I want to search pattern in file "S12345678B" which start with S and end with B total length is 10,numeric part 12345678 is variable. e.g S99001229B and S88226901B. please help (2 Replies)
Discussion started by: ashfaque
2 Replies

3. Shell Programming and Scripting

How to use sed to search a particular pattern in a file backward after a pattern is matched.?

Hi, I have two files file1.txt and file2.txt. Please see the attachments. In file2.txt (which actually is a diff output between two versions of file1.txt.), I extract the pattern corresponding to 1172c1172. Now ,In file1.txt I have to search for this pattern 1172c1172 and if found, I have to... (9 Replies)
Discussion started by: saurabh kumar
9 Replies

4. Linux

Search a file with specified pattern

May I know how to search for a file whose name is embedded with 3007 but it shouldn't begin or end with 3007. For ex. : 3008ab3007cd1007. (10 Replies)
Discussion started by: ravisingh
10 Replies

5. Shell Programming and Scripting

Search for a pattern in a String file and count the occurance of each pattern

I am trying to search a file for a patterns ERR- in a file and return a count for each of the error reported Input file is a free flowing file without any format example of output ERR-00001=5 .... ERR-01010=10 ..... ERR-99999=10 (4 Replies)
Discussion started by: swayam123
4 Replies

6. Shell Programming and Scripting

awk get search pattern from a file.

Here Is a problem I am facing with awk. Query --> I want to search for a string in a file and print next 15 lines below the matched string. 1.We do not have GNU grep so cannot use grep -A or grep -B commands. 2. Instead of passing the search pattern as a string to awk. I want the awk to... (4 Replies)
Discussion started by: togotutor
4 Replies

7. Shell Programming and Scripting

search a pattern and if pattern found insert new pattern at the begining

I am trying to do some thing like this .. In a file , if pattern found insert new pattern at the begining of the line containing the pattern. example: in a file I have this. gtrow0unit1/gctunit_crrownorth_stage5_outnet_feedthru_pin if i find feedthru_pin want to insert !! at the... (7 Replies)
Discussion started by: pitagi
7 Replies

8. Shell Programming and Scripting

File search for pattern - script

Hi All, I have two directories as 1) mi/job -> job1.sh, job2.sh, job3.sh 2) mi/sysin -> sysin1, sysin2, sysin3,sysin4 I want to wrrite a script such that it should accept two parameters as directory paths. $ myscript mi/sysin mi/job. The script should be able to scan all files... (3 Replies)
Discussion started by: rahulrathod
3 Replies

9. UNIX for Dummies Questions & Answers

Search a file for pattern?

Hi, I'm looking for a single command that will be able tell me which files in a directory don't contain a certain pattern e.g. regular expression I've tried a combination of ls, xargs and grep but I haven't come up with anything yet. :confused: I would appreciate any help. Thanks (2 Replies)
Discussion started by: davjoyce
2 Replies

10. Shell Programming and Scripting

Search file for pattern and grab some lines before pattern

I want to search a file for a string and then if the string is found I need the line that the string is on - but also the previous two lines from the file (that the pattern will not be found in) This is on solaris Can you help? (2 Replies)
Discussion started by: frustrated1
2 Replies
Login or Register to Ask a Question