Sponsored Content
Full Discussion: Pattern matching question
Top Forums Shell Programming and Scripting Pattern matching question Post 302387451 by HobieCoop on Friday 15th of January 2010 10:25:23 PM
Old 01-15-2010
egrep ^"[^0-9]+${PNUM}[^0-9]" /bb/data/firmexbytes.dta

First - ${PNUM} is just a variable that contains a string - any string.

So now, let's break down the egrep command chunk by chunk...

egrep ^
This means start looking for the pattern at the start of each line

egrep ^"[^0-9]+
The ^ inside the bracket before a pattern means NOT. 0-9 mean any digit 0 thru 9. So [^0-9] means look for any single character that is not a digit 0 thru 9. The + means match 1 or more instance of the preceding expression. So ^"[^0-9]+ means look for 1 or more characters in a row that are not digits 0 thru 9 starting at the beginning of each line.

egrep ^"[^0-9]+${PNUM}
${PNUM} is just a variable that contains a string - any string. So ^"[^0-9]+${PNUM} means look for the string ${PNUM} and return the line as long as there are no numbers preceding the string ${PNUM}.

egrep ^"[^0-9]+${PNUM}[^0-9]"
Finally we end with [^0-9] which says look for any single character that is not a digit 0 thru 9. And since there is no + after it, it says to only look at the next character after ${PNUM} is found. And it MUST find at least one non-numeric character.


For the sake of this discussion let's say that:

PNUM=junk

So - the egrep statement will scan the /bb/data/firmexbytes.dta file and return any lines that have the string "junk" in it with 1 or more characters before the string with none of them a number AND a character that exists immediately after "junk" that is not a number.

Here's another example:

PNUM=1234

In this case - the egrep statement will scan the /bb/data/firmexbytes.dta file and return any lines that have the string "1234" in it with 1 or more characters before the string with that are not a number AND a character that exists immediately after "1234" that is not a number.


Want to have fun? Put the following in a file called file1:

Code:
junk
ajunk
0junk
ajunk0
ajunka
abcjunkz
abcjunkxyz
abcjunkx12
a0cjunka
junka
junk0

Then run

Code:
PNUM=junk
egrep ^"[^0-9]+${PNUM}[^0-9]" file1

Of all those values found in the list above, egrep will only return these:
ajunka
abcjunkz
abcjunkxyz
abcjunkx12
because they are the only ones that have the string "junk" in it with 1 or more characters before the string with none of them a number AND a character that exists immediately after "junk" that is not a number.

abcjunkx12 should not be a surprise ...

Now change junk to 1234 both in the file and as the value of PNUM and try the egrep again.

Got it??? Should be clear as mud at this point....
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

pattern matching + perl question

i can only find the first occurance of a pattern how do i set it to loop untill all occurances have changed. #! /usr/bin/perl use POSIX; open (DFH_FILE, "./dfh") or die "Can not read file ($!)"; foreach (<DFH_FILE>) { if ($_ !~ /^#|^$/) { chomp; ... (1 Reply)
Discussion started by: Optimus_P
1 Replies

2. Shell Programming and Scripting

SED Question: Search and Replace start of line to matching pattern

Hi guys, got a problem here with sed on the command line. If i have a string as below: online xx:wer:xcv: sdf:/asdf/http:https-asdfd How can i match the pattern "http:" and replace the start of the string to the pattern with null? I tried the following but it doesn't work: ... (3 Replies)
Discussion started by: DrivesMeCrazy
3 Replies

3. Shell Programming and Scripting

Pattern matching question

Hi, I am writing a simple log parsing system and have a question on pattern matching. It is simply grep -v -f patterns.re /var/log/all.log Now, I have the following in my logs Apr 16 07:33:17 ad-font-dc1 EvntSLog: AD-FONT-DC1/NTDS ISAM (700) - "NTDS (384) NTDSA: Online defragmentation... (5 Replies)
Discussion started by: wpfontenot
5 Replies

4. Shell Programming and Scripting

pattern matching question

Hi guys, I have a file in the following format: 4222 323K 323L D222 494 8134 A023 A024 49 812A 9871 9872 492 A961 A962 A963 491 0B77 0B78 0B79 495 0B7A 0B7B 0B7C 4949 WER9 444L 999O I need to grep the line... (5 Replies)
Discussion started by: aoussenko
5 Replies

5. Shell Programming and Scripting

Pattern matching question

Hi Guys, I am trying to setup a check for the string using an "if" statement. The valid entry is only the one which contain Numbers and Capital Alpha-Numeric characters, for example: BA6F, BA6E, BB21 etc... I am using the following "if" constract to check the input, but it fails allowing Small... (3 Replies)
Discussion started by: aoussenko
3 Replies

6. Shell Programming and Scripting

pattern matching question

Hi Guys I am trying to check if the pattern "# sign followed by one or several tabs till the end of the line" exists in my file. I am using the following query: $ cat myfile | nawk '{if(/^#\t*$/) print "T"}' Unfortunately it does not return the desired output since I know for sure that the line... (4 Replies)
Discussion started by: aoussenko
4 Replies

7. Shell Programming and Scripting

sed pattern matching question

I inherited a script that contains the following sed command: sed -n -e '/^.*ABCD|/p' $fileName | sed -e 's/^.*ABCD|//' | sed -e 's/|ABCD$//' > ${fileName}.tmp What I'm wondering is whether ABCD has a special pattern matching value in sed, such as a character class similar or identical to . ... (9 Replies)
Discussion started by: topmhat
9 Replies

8. Shell Programming and Scripting

pattern matching question

Hi guys I have the following case statement in my script: case $pn.$db in *?.fcp?(db)) set f ${pn} cp ;; *?.oxa?(oxa) ) set oxa $pn ;; esac Can somebody help me to understand how to interpret *?.fcp?(db)) or *?.oxa?(oxa) ? I cannot figure out how in this case pattern maching... (5 Replies)
Discussion started by: aoussenko
5 Replies

9. UNIX for Dummies Questions & Answers

Perl Pattern Matching Question

Hi all, I have a pattern matching problem in which i'm not sure how to attack. Here is my problem: I have a list of strings that appear in the following format: String: LE_(1234 ABC)^2^ABC^DEFG What i need to do is replace all the characters after the first ^ with blank. So the output... (2 Replies)
Discussion started by: WongSifu
2 Replies

10. Shell Programming and Scripting

Bash pattern matching question

I need to check the condition of a variable before the script continues and it needs to match a specific pattern such as EPS-03-0 or PDF-02-1. The first part is a 3 or 4 letter string followed by a hyphen, then a 01,02 or 03 followed by a hyphen then a 0 or a 1. I know I could check for every... (4 Replies)
Discussion started by: stormcel
4 Replies
ZIPGREP(1L)															       ZIPGREP(1L)

NAME
zipgrep - search files in a ZIP archive for lines matching a pattern SYNOPSIS
zipgrep [egrep_options] pattern file[.zip] [file(s) ...] [-x xfile(s) ...] DESCRIPTION
zipgrep will search files within a ZIP archive for lines matching the given string or pattern. zipgrep is a shell script and requires egrep(1) and unzip(1L) to function. Its output is identical to that of egrep(1). ARGUMENTS
pattern The pattern to be located within a ZIP archive. Any string or regular expression accepted by egrep(1) may be used. file[.zip] Path of the ZIP archive. (Wildcard expressions for the ZIP archive name are not supported.) If the literal filename is not found, the suffix .zip is appended. Note that self-extracting ZIP files are supported, as with any other ZIP archive; just specify the .exe suffix (if any) explicitly. [file(s)] An optional list of archive members to be processed, separated by spaces. If no member files are specified, all members of the ZIP archive are searched. Regular expressions (wildcards) may be used to match multiple members: * matches a sequence of 0 or more characters ? matches exactly 1 character [...] matches any single character found inside the brackets; ranges are specified by a beginning character, a hyphen, and an end- ing character. If an exclamation point or a caret (`!' or `^') follows the left bracket, then the range of characters within the brackets is complemented (that is, anything except the characters inside the brackets is considered a match). (Be sure to quote any character that might otherwise be interpreted or modified by the operating system.) [-x xfile(s)] An optional list of archive members to be excluded from processing. Since wildcard characters match directory separators (`/'), this option may be used to exclude any files that are in subdirectories. For example, ``zipgrep grumpy foo *.[ch] -x */*'' would search for the string ``grumpy'' in all C source files in the main directory of the ``foo'' archive, but none in any subdirectories. Without the -x option, all C source files in all directories within the zipfile would be searched. OPTIONS
All options prior to the ZIP archive filename are passed to egrep(1). SEE ALSO
egrep(1), unzip(1L), zip(1L), funzip(1L), zipcloak(1L), zipinfo(1L), zipnote(1L), zipsplit(1L) URL
The Info-ZIP home page is currently at http://www.info-zip.org/pub/infozip/ or ftp://ftp.info-zip.org/pub/infozip/ . AUTHORS
zipgrep was written by Jean-loup Gailly. Info-ZIP 20 April 2009 ZIPGREP(1L)
All times are GMT -4. The time now is 11:10 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy