Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 417711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:36:26+00:00 2026-05-12T18:36:26+00:00

What is the correct way to parse a string using regular expressions in a

  • 0

What is the correct way to parse a string using regular expressions in a linux shell script? I wrote the following script to print my SO rep on the console using curl and sed (not solely because I’m rep-crazy – I’m trying to learn some shell scripting and regex before switching to linux).

json=$(curl -s http://stackoverflow.com/users/flair/165297.json)
echo $json | sed 's/.*"reputation":"\([0-9,]\{1,\}\)".*/\1/' | sed s/,//

But somehow I feel that sed is not the proper tool to use here. I heard that grep is all about regex and explored it a bit. But apparently it prints the whole line whenever a match is found – I am trying to extract a number from a single line of text. Here is a downsized version of the string that I’m working on (returned by curl).

{“displayName”:”Amarghosh”,”reputation”:”2,737″,”badgeHtml”:”\u003cspan title=\”1 silver badge\”\u003e\u003cspan class=\”badge2\”\u003e●\u003c/span\u003e\u003cspan class=\”badgecount\”\u003e1\u003c/span\u003e\u003c/span\u003e”}

I guess my questions are:

  • What is the correct way to parse a string using regular expressions in a linux shell script?
  • Is sed the right thing to use here?
  • Could this be done using grep?
  • Is there any other command that’s more easier/appropriate?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T18:36:26+00:00Added an answer on May 12, 2026 at 6:36 pm

    The grep command will select the desired line(s) from many but it will not directly manipulate the line. For that, you use sed in a pipeline:

    someCommand | grep 'Amarghosh' | sed -e 's/foo/bar/g'
    

    Alternatively, awk (or perl if available) can be used. It’s a far more powerful text processing tool than sed in my opinion.

    someCommand | awk '/Amarghosh/ { do something }'
    

    For simple text manipulations, just stick with the grep/sed combo. When you need more complicated processing, move on up to awk or perl.

    My first thought is to just use:

    echo '{"displayName":"Amarghosh","reputation":"2,737","badgeHtml"'
        | sed -e 's/.*tion":"//' -e 's/".*//' -e 's/,//g'
    

    which keeps the number of sed processes to one (you can give multiple commands with -e).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 220k
  • Answers 221k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You are looking for the imagecopy...() functions like imagecopyresampled(). The… May 13, 2026 at 12:05 am
  • Editorial Team
    Editorial Team added an answer You seem to be looking for a Full-Text search engine… May 13, 2026 at 12:05 am
  • Editorial Team
    Editorial Team added an answer You should meassure your sites performance with Xdebug or similar,… May 13, 2026 at 12:05 am

Related Questions

Duplicate: Is there a way to instantiate objects from a string holding their class
I'm implementing IXMLSerializable in one of my classes. It contains a few numeric properties
I'm trying to serialize some data to xml in a way that can be
I have an aspx page that contains regular html, some uicomponents, and multiple tokens

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.