Visit Our UNIX and Linux User Community

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  
Old 10-11-2009
Syntax Help | unix | grep | regular expression | repetition


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.

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.

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:
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:
grep: Unmatched \{
I'm probably missing something simple but I can't figure it out.

The repetition code was taken from 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  
Old 10-11-2009
Hi MykC, You were just one \ away Smilie
grep '\b[1-9][0-9]\{3,\}\b'

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

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

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

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

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

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

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

or filter out lines that start with two spaces:
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 07:59 PM..
# 5  
Old 10-11-2009
Hey, thanks for the help, such a silly error.

Previous Thread | Next Thread
Test Your Knowledge in Computers #960
Difficulty: Medium
The WannaCry ransomware attack was a May 2017 worldwide cyberattack by the WannaCry ransomware cryptoworm, which targeted computers running Windows by encrypting data and demanding ransom payments in the Bitcoin.
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

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

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

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