1

I am try to match only the digits from a text file that is in this format:

> 1234

I'm running thru the file with a loop. Every line store in $i

$i | grep "\d{4}"

Output looks like this:

>
1234
>
5678

Why is it still outputing the >? I want to remove those.

6
  • 1
    $i at the start of the line will try to run a command with its value, e.g. a command called 1234. I'm also confused about "what you are doing wrong." Your output doesn't look wrong to me... Commented Feb 26, 2013 at 15:11
  • Do you want to show digits only? Commented Feb 26, 2013 at 15:12
  • Sorry I wasn't clear. I mean why is it outputing the >. I'm trying to get rid of those. Commented Feb 26, 2013 at 15:33
  • Did you expect grep to extract only the number? Grep will display the entire line containing a match. Maybe you want to use sed instead? (I'm deleting my answer since it didn't address the question you meant to ask.) Commented Feb 26, 2013 at 15:37
  • @William "Grep will display the entire line containing a match" not necessarily Commented Feb 26, 2013 at 15:40

1 Answer 1

2

From man grep

   -o, --only-matching
          Print only the matched (non-empty) parts of a matching line, with each such part on a
          separate output line.

Try grep -o ....

Test:

i="> 1234"
$ echo "$i"
> 1234
$ echo "$i" | grep -oP "\d{4}"
1234
Sign up to request clarification or add additional context in comments.

1 Comment

Note to self, check manpages... Obviously things have changed since Solaris 7's grep :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.