- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- To get script to Grep particular Pattern
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-08-2007 02:53 PM
11-08-2007 02:53 PM
To get script to Grep particular Pattern
I am trying to select records from file (inputfile) based on a file that contains string patterns. The UNIX script must look for these all the patterns in the file in a particular location.
e.g.
Sample Input File
AAAAAA8301010707010703015
C1$$$1107019999990403266F706F70C 4398 C 5220 C 5227 C 5401 C 5423 C 5515 C 5771 C 578402C 5789 C 5796 C 5807 C 5821 C 582501C 5859 C 6003 C 6225 C 7292 C 9770 C 990903C 993009
C1$$$2107019999990403266E476E47C 408703C 4398 C 5220 C 5227 C 5401 C 5423 C 5515 C 5771 C 578402C 5789 C 5796 C 5807 C 5821 C 582501C 5859 C 6003 C 6225 C 6502 C 7292 C 9770 C 990903C 993009
C1$$$3107019999990403266E486E48C 408703C 6502
C1$$$7107019999990403266F706F70C 4398 C 5220 C 5227 C 5401 C 5423 C 5515 C 5769 C 577501C 5815 C 5821 C 5857 C 6003 C 6225 C 7292 C 9770 C 990903C 993009
C1$$$8107019999990403266E476E47C 408703C 4398 C 5220 C 5227 C 5401 C 5423 C 5515 C 5769 C 577501C 5815 C 5821 C 5857 C 6003 C 6225 C 6502 C 7292 C 9770 C 990903C 993009
C1$$$9107019999990403266E486E48C 408703C 6502
C1***49707019999999809156F706F70C 3315 C 4084 C 4398 C 496109C 511303C 5220 C 5227 C 5423 C 5821 C 6003 C 7021 C 729201C 9770 C 990903C 993009
C1***59707019999999809156E476E47C 3315 C 4084 C 408703C 4398 C 496109C 511303C 5220 C 5227 C 5423 C 5821 C 6003 C 6502 C 7021 C 729201C 9770 C 990903C 993009
C1***69707019999999809156F706F70C 3315 C 4084 C 4398 C 496207C 511304C 5220 C 5227 C 5423 C 5821 C 6003 C 7021 C 729201C 9770 C 990903C 993009
C1***79707019999999809156E476E47C 3315 C 4084 C 408703C 4398 C 496207C 511304C 5220 C 5227 C 5423 C 5821 C 6003 C 6502 C 7021 C 729201C 9770 C 990903C 993009
C1*TT19907019999990206036F706F70C 3315 C 4084 C 4397 C 4398 C 496001C 515106C 5220 C 5227 C 5401 C 5423 C 5821 C 6003 C 7021 C 729201C 9770 C 990903C 993009
C1*TT29907019999990206036E476E47C 3315 C 4084 C 408703C 4397 C 4398 C 496001C 515106C 5220 C 5227 C 5401 C 5423 C 5821 C 6003 C 6502 C 7021 C 729201C 9770 C 990903C 993009
C1*TT39907019999990206036E486E48C 408703C 6502
C1*1AL9807019999990608176F706F70C 1220 C 2503 C 2851 C 3920 C 4398 C 5401 C 5537 C 993009
C1*1CS9707019999990608156E476E47C 408703C 4398 C 5401 C 5515 C 5769 C 5859 C 6225 C 6502 C 993009
C1*1DS9807019999990608216E476E47C 1700 C 3315 C 4084 C 408703C 4398 C 496109C 5401 C 551808C 6502 C 993009
C1*1JG9507019999990608156E476E47C 408703C 4398 C 5401 C 5515 C 6225 C 6502 C 993009
Sample Pattern File:
528H
528J
528K
528L
528M
I need to search for 528H in location 3 through 7 only and write the whole record to another file if there is a match.
What I have so far:
egrep -f pattern bi3_forms.txt > inbi3_forms.txt
Problem: This command finds patterns all over the record not just from location 3 through 7.
awk 'substr($0,3,4) == "528H"' bi3_forms.txt > inbi3_forms.txt
Problem: This command finds the pattern location 3 through 7 but does not take a pattern file as input.
Any help is greatly appreciated.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-08-2007 03:22 PM
11-08-2007 03:22 PM
Re: To get script to Grep particular Pattern
while ((getline < "pattFile") > 0)
{a[$0]=1;}
}
{
if ( a[substr($0,3,4)]) print;
}' inputFile > outFile
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-08-2007 03:59 PM
11-08-2007 03:59 PM
Re: To get script to Grep particular Pattern
No good nor bad sample match present.
Is the list always as nice as the example?
If so, you could capture it in a regexpr.
awk 'match(substr($0,3,4),/528[HJLM]/)' data
prior solution as 1 liner and a little shorter:
$ awk 'BEGIN{while ((getline < "list") > 0){a[$0]=1;} } a[substr($0,3,4)]' data
fwiw,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-08-2007 06:40 PM
11-08-2007 06:40 PM
Re: To get script to Grep particular Pattern
grep -f pattern bi3_forms.txt | awk 'substr($0,3,3) == "528"'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-09-2007 04:28 AM
11-09-2007 04:28 AM
Re: To get script to Grep particular Pattern
# grep '^..528[HJKLM].*$' infile > outfile
...above will locate the pattern at cols 3-7 only OR to be somewhat generic use:
# grep '^..[0-9]\{3\}[HJKLM].*$' infile > outfile
...above will locate the pattern that has 3 digits followed by either H/J/K/L/M in cols 3-7. Or you could use the one below to locate the pattern that has 3 digits followed by an uppercase alphabet:
# grep '^..[0-9]\{3\}[A-Z].*$' infile > outfile
Or to locate the pattern that has 3 digits followed by an upper/lower case alphabet use:
# grep '^..[0-9]\{3\}[A-Za-z].*$' infile > outfile
~hope it helps
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-11-2007 07:20 PM
11-11-2007 07:20 PM
Re: To get script to Grep particular Pattern
use this sample pattern file:
..528H
..528J
..528K
..528L
..528M
grep -f pattern b13_forms.txt
and teh grep will look for the pattern starting in position 3.
HTH,
Art
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-11-2007 10:13 PM
11-11-2007 10:13 PM
Re: To get script to Grep particular Pattern
>grep will look for the pattern starting in position 3.
You need to anchor each pattern otherwise it will find it in other columns:
^..528H
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2007 09:55 PM
11-28-2007 09:55 PM
Re: To get script to Grep particular Pattern
If you want to collect 528[HJKLM] from 3rd char or from 4th char or 7th char etc, you can use
grep -e '^..528[HJKLM]' -e '^...528[HJKLM]' -e '^....528[HJKLM]' -e '^.....528[HJKLM]' -e '^......528[HJKLM]'
Rgds
-NKG-