Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Syntax Help | unix | grep | regular expression | repetition Post 302360954 by MykC on Sunday 11th of October 2009 04:43:57 PM
Old 10-11-2009
Syntax Help | unix | grep | regular expression | repetition

Hello,

This is my first post so, Hello World! Anyways, I'm learning how to use unix and its quickly become apparent that a strong foundation in regular expressions will make things easier. I'm not sure if my syntax is messing things up or my logic is messing things up.

Code:
ps -e | grep '\b[1-9][0-9]\{3,}\b'

So, I want a list of most of the current processes ps -e generates a list similar to the one below but with more lines.

Quote:
5429 tty1 00:00:00 agetty
5430 tty2 00:00:00 agetty
5431 tty3 00:00:00 agetty
5432 tty4 00:00:00 agetty
5433 tty5 00:00:00 agetty
5434 tty6 00:00:00 agetty
9266 ? 00:00:04 smbd
9578 ? 00:00:00 screen
9579 pts/2 00:01:22 rtorrent
12447 ? 00:00:00 sshd
12451 ? 00:00:00 sshd
12452 pts/0 00:00:00 bash
16109 ? 00:00:00 sshd
16112 ? 00:00:00 sshd
16113 pts/1 00:00:00 bash
16118 pts/1 00:00:00 ps
Pattern: I want to reduce the output to only include lines that have numbers greater than 1000.
Program: I'll use grep to print the lines matching the above pattern.
Options: I don't think I need to use any options.
Regular Expression: \b[1-9][0-9]\{3,}\b
File: ps -e (omitted)

Again the fully command line argument(?correct term) is:
Code:
ps -e | grep '\b[1-9][0-9]\{3,}\b'

I read the regular expression as:
\b: Check for world boundary before the string.
[1-9]: The first character in the string must be between 1 and 9.
[0-9]: The second character in the string must be between 0 and 9.
\{3,}: Replaces the above statement with: The characters following the first match must match between 0 and 9 at least 3 more times.
Kinda would look like \b[1-9][0-9][0-9][0-9]\b (for the min)
\bCheck for word boundary at the end of the string.

The return I get is:
Quote:
grep: Unmatched \{
I'm probably missing something simple but I can't figure it out.

The repetition code was taken from http://www.regular-expressions.info/repeat.html and altered a bit to apply to a use that I could create.
I also referenced the grep - Linux Command - Unix Command for info on grep.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

grep : regular expression

guys, my requirment goes like this: I have a file, and wish to filter out records where 1. The first letter is o or O and 2. The next 4 following letter should not be ther I do not wish to use pipe and wish to do it in one shot. The best expression I came up with is: grep ^*... (10 Replies)
Discussion started by: RishiPahuja
10 Replies

2. UNIX for Advanced & Expert Users

regarding grep regular expression

When i do ls -ld RT_BP* i am getting the following list. drwxrwx--- 2 user group 256 Oct 17 10:09 RT_BP809 drwxrwx--- 2user group 256 Oct 17 10:09 RT_BP809.O drwxrwx--- 2 user group 256 Oct 17 10:09 RT_BP810 drwxrwx--- 2user group 256 Oct... (2 Replies)
Discussion started by: ukatru
2 Replies

3. Shell Programming and Scripting

grep with regular expression

Hi, guys. I have one question, hope somebody can give me a hand I have a file called passwd, the contents of it arebelow: *********************** ... goldsimj:x:5008:200: goldsij2:x:5009:200: whitej:x:5010:201: brownj:x:5011:202: goldsij3:x:5012:204: greyp:x:5013:203: ...... (6 Replies)
Discussion started by: daikeyang
6 Replies

4. Shell Programming and Scripting

grep regular expression

please can someone tell me what the following regrex means grep "^aa*$" <file> I thought this would match any word beginning with aa and ending with $, but it doesnt. Thanks in advance Calypso (7 Replies)
Discussion started by: Calypso
7 Replies

5. UNIX for Dummies Questions & Answers

Help | unix | grep | regular expression

I have the following code: ls -al /bin | tr -s ' ' | grep 'x' ls -al: Lists all the files in a given director such as /bin tr -s ' ': removes additional spaces between characters so that there is only one space grep 'x': match all "x" characters that are followed by a whitespace. I was... (3 Replies)
Discussion started by: MykC
3 Replies

6. UNIX for Dummies Questions & Answers

Help | Unix | grep | regular expression | backreference | Syntax/Logic

Hello, I'm working on learning regular expressions and what I can do with them. I'm using unix to and its programs to experiment and learn what my limitations are with them. I'm working on duplicating the regular expression: ^(.*)(\r?\n\1)+$ This is supposed to delete duplicate lines... (2 Replies)
Discussion started by: MykC
2 Replies

7. Shell Programming and Scripting

grep and regular expression

Hi, I am executing a svnlook command to check to see if the following line exists. I need a regular expression to represent the line. A /test/test1/qa/test2/index.html A /test/test1/qa/test3/test.jpg A /test/test1/qa/test3/test1.jpg A /test/test1/qa/test4/test.swf I just need to extract... (9 Replies)
Discussion started by: kminkeller
9 Replies

8. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

9. Shell Programming and Scripting

Help with awk script (syntax error in regular expression)

I've found this script which seems very promising to solve my issue: To search and replace many different database passwords in many different (.php, .pl, .cgi, etc.) files across my filesystem. The passwords may or may not be contained within quotes, single quotes, etc. #!/bin/bash... (4 Replies)
Discussion started by: spacegoose
4 Replies

10. UNIX for Beginners Questions & Answers

Grep regular expression

I want to track only below: I am using below, but it doesn't work: (6 Replies)
Discussion started by: proactiveaditya
6 Replies
re_comp(3C)						   Standard C Library Functions 					       re_comp(3C)

NAME
re_comp, re_exec - compile and execute regular expressions SYNOPSIS
#include <re_comp.h> char *re_comp(const char *string); int re_exec(const char *string); DESCRIPTION
The re_comp() function converts a regular expression string (RE) into an internal form suitable for pattern matching. The re_exec() func- tion compares the string pointed to by the string argument with the last regular expression passed to re_comp(). If re_comp() is called with a null pointer argument, the current regular expression remains unchanged. Strings passed to both re_comp() and re_exec() must be terminated by a null byte, and may include NEWLINE characters. The re_comp() and re_exec() functions support simple regular expressions, which are defined on the regexp(5) manual page. The regular expressions of the form {m}, {m,}, or {m,n} are not supported. RETURN VALUES
The re_comp() function returns a null pointer when the string pointed to by the string argument is successfully converted. Otherwise, a pointer to one of the following error message strings is returned: No previous regular expression Regular expression too long unmatched ( missing ] too many () pairs unmatched ) Upon successful completion, re_exec() returns 1 if string matches the last compiled regular expression. Otherwise, re_exec() returns 0 if string fails to match the last compiled regular expression, and -1 if the compiled regular expression is invalid (indicating an internal error). ERRORS
No errors are defined. USAGE
For portability to implementations conforming to X/Open standards prior to SUS, regcomp(3C) and regexec(3C) are preferred to these func- tions. See standards(5). SEE ALSO
grep(1), regcmp(1), regcmp(3C), regcomp(3C), regexec(3C), regexpr(3GEN), regexp(5), standards(5) SunOS 5.11 26 Feb 1997 re_comp(3C)
All times are GMT -4. The time now is 01:58 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy