I am trying to remove the specific text from the file. I have tried using sed:

sed 's/.*,/{/' beta.txt | head -n5

But didn't got the expected output.
Here is the input and output I got:

{"url":"example1.com", jkdshsjkfhdj,"dsd":"sdsd","original":"ex1.com","tech":"d"}

The output I got is:


What I expect is:


I just want to have original and everything before it should be replaced with {.

How I can achieve it?

  • 3
    Well, you can achieve it with echo '{"original":"ex.com","tech":"c"}' but that's probably not what you want ;). Please edit your question and explain what you are after. Do you want the last 2 comma-separated fields? Everything except the url (no matter how many fields you have)? Only lines matching tech?
    – terdon
    Feb 8 '17 at 12:06

You weren't getting what you expected because regular expressions are greedy. Match anything but a comma [^,] instead of any character:

$ sed 's/[^,]*,/{/' beta.txt

Since your edit though, apparently there's nothing consistent to match on all lines except original explicitly, so we'll have to do that... not much fun.

sed 's/.*,"original/{"original/'
  • is it possible if I do as [^,"] in your regex? I want that it should remove before ,"original Feb 8 '17 at 12:31
  • depends what you are trying to avoid matching @JafferWilson - like this? sed 's/[^,]*,"/{"/'
    – Zanna
    Feb 8 '17 at 12:38
  • Yes, I am trying to have only original. Let me edit the question input Feb 8 '17 at 12:43
  • @JafferWilson since ," occurs before and after the field you want you'll have to match it explicitly - is that OK? sed 's/.*,"original/{"original/'
    – Zanna
    Feb 8 '17 at 12:57

Another one:

# sed 's/\".*com,//' sedtest

Edit: Use -i flag if you want to make changes to some file. Like:

sed -i.bak 'regex_above' filename

Putting a dot on -i parameter creates a backup named filename.bak in the same dir as well.


The proper question to ask here would be "How to keep only specific items in json dictionary" , because that's exactly what is seen in your examples. Of course it is possible to do it via other text processing tools, but if we're working with json, then lets use proper tools for that.

Using python's json module, we can have the small script shown below, where you have list of items you want to keep. Assuming your input file is structured with one json dictionary per line, we can read the file line-by-line, and load each line as python's dictionary . From there it's straight road to victory - iterate over all the keys in the dictionary and see if they're on the list of items we want to keep; if they're not on that list - pop them out.

#!/usr/bin/env python
import json,sys
keep = ['tech','original']
with open(sys.argv[1]) as f:
    for line in f:
        for key in data.keys():
           if key not in keep: 
        print ""

Test run:

$ cat input.json                                                                                      
$ ./remove_json_items.py input.json                                                                   
{"tech": "c", "original": "ex.com"}
{"tech": "d", "original": "ex1.com"}

Another option is search all the patern and repeat the part that you are interested in. with (....) you can use the content between ( ) and after, in the substitution field use \ and the number of content.

In this example I've made two text and the first \1 the second \2 so you are interested in \2 and your answer is:

sed 's/^{\(.*\),\(.*\),/{\2/' sedtest

You can also include the "original" tag:

's/{.*,"original"/{"original"/' beta.txt



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.