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?
Tools that include regular expression capabilities include sed, grep, awk, Perl, Python, to mention a few. Even newer version of Bash have regex capabilities. All you need to do is look up the docs on how to use them.
It can be, but not necessary.
Yes it can. you will just construct similar regex as you would if you use sed, or others. Note that grep just does what it does, and if you want to modify any files, it will not do it for you.
Of course. regex can be powerful, but its not necessarily the best tool to use everytime. It also depends on what you mean by "easier/appropriate". The other method to use with minimal fuss on regex is using the fields/delimiter approach. you look for patterns that can be "splitted". for eg, in your case(i have downloaded the 165297.json file instead of using curl..(but its the same)
output:
Blindly:
Similar (the field separator can be a regex):
Smarter (look for the key and print its value):
You may be interested in using Perl for such tasks. As a demonstration, here is a Perl script which prints the number you want:
This script requires you to install the JSON module, which you can do with just the command
cpan JSON
.sed
is a perfectly valid command for your task, but it may not be the only one.grep
may be useful too, but as you say it prints the whole line. It's most useful for filtering the lines of a multi-line file, and discarding the lines you don't want.Efficient shell scripts can use a combination of commands (not just the two you mentioned), exploiting the talents of each.
My proposition:
I put two commands in sed argument:
s/,//g
is used to remove all commas, in particular the ones that are present in the reputation value.s/^.*reputation...\([0-9]*\).*$/\1/
locates the reputation value in the line and replaces the whole line by that value.In this particular case, I find that
sed
provides the most compact command without loss of readability.Other tools for manipulating strings (not only regex) include:
grep
,awk
,perl
mentioned in most of other answerstr
for replacing characterscut
,paste
for handling multicolumn inputsbash
itself with its rich$(...)
syntax for accessing variablestail
,head
for keeping last or first lines of a file