Syntax Help | unix | grep | regular expression | repetition


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Syntax Help | unix | grep | regular expression | repetition
# 1  
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.
# 2  
Hi MykC, You were just one \ away Smilie
Code:
grep '\b[1-9][0-9]\{3,\}\b'


Last edited by Scrutinizer; 10-11-2009 at 07:42 PM..
# 3  
Hi.

Given that no number starts with 0 with ps -e, you could simply say:

Code:
ps -e | grep "^ *[0-9]\{4,\}"

Awk is great for this kind of thing:
Code:
ps -e | awk '$1*1 >= 1000'

(*1 removes the heading by turning into a number, which is then undefined)
# 4  
And then you could leave out the comma too:
Code:
ps -e | grep "^ *[0-9]\{4\}"

or even 4 characters that are no space.
Code:
ps -e | grep "^ *[^ ]\{4\}"

( and since the first field is right aligned and the max pid is 99999:
Code:
ps -e|grep '^.[^ ]'

or filter out lines that start with two spaces:
Code:
ps -e|grep -v '^  '

but that is stretching it a little and probably not very portable Smilie )

Last edited by Scrutinizer; 10-11-2009 at 08:59 PM..
# 5  
Hey, thanks for the help, such a silly error.
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #725
Difficulty: Medium
Barbara Liskov developed the Liskov substitution principle, which guarantees semantic interoperability of data types in a hierarchy.
True or False?

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Grep + Regular expression or

Hi , I have few lines like A20120101.ANU.ZIP A20120401.ABC.ZIP A20120105.KJK.ZIP A20120809.JUG.ZIP A20120101.MAT.ZIP B20120301.ANU.XIP I want to filter by 1. Files starting with A and Ending With Z ( ^A.*.ZIP$) 2. And either ANU, or KJK or MAT in the file name. Hope my... (6 Replies)
Discussion started by: Anupam_Halder
6 Replies

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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

Featured Tech Videos