-1

I want to replace and remove a substring from a text file line by line. The info I want to change is under a specific field. For example, I have the following text:

{name:x, version:1.0, info:"test", ...}
{name:y, version:0.1, info:"test again", ...}
{name:z, version:1.1, info:"test over", ...}
{name:x, info: "test", ..., version:1.2, ...}

The field could in random order. I want to remove all the version value and replace all the info value by some specific string, we say "foo".

The expect result:

{name:x, info:"foo", ...}
{name:y, info:"foo", ...}
{name:z, info:"foo", ...}
{name:x, info:"foo", ...}

Is there smart way to do this by shell script? I have no idea how could I replace a substring as above?

I know some stupid command as

sed 's/info:\([^,}]*\)/info:\"foo\"/' file
sed 's/version:\([^,}]*\)//' file
eleven
  • 359
  • 1
  • 5
  • 14
  • 1
    what's wrong with the `sed` you have posted? Best to include current output and a description of what is wrong. Good luck. – shellter Sep 23 '14 at 03:41
  • 1
    Use a json tool for json data (assuming that's what that is). – Etan Reisner Sep 23 '14 at 03:45
  • json tool? could you please provide more information? @EtanReisner – eleven Sep 23 '14 at 03:46
  • the current sed is correct. I just want to find a smarter way @shellter – eleven Sep 23 '14 at 03:47
  • python, perl, ruby, [jq](http://stedolan.github.io/jq/), almost any of the other things listed on http://stackoverflow.com/questions/3858671/unix-command-line-json-parser – Etan Reisner Sep 23 '14 at 03:55
  • no other tools, I just want to use the bash shell script @EtanReisner – eleven Sep 23 '14 at 04:01
  • 1
    @eleven If you are looking for a _smarter way_ then you need to follow Etan's suggestion. Parsing JSON with bash and family is **extremely** error prone. If your current `sed` command works then you should stick with it. However I have a feeling that your `sed` solution is barfing at some input, hence your question. – jaypal singh Sep 23 '14 at 04:05
  • Right, but that's the wrong way to go about this sort of thing. Just as it is the wrong way to handle xml data. You can generally get it to work sort of but it will fall down quickly as things get more complicated. (`info="some string, with a comma"` for example will break your `sed`.) – Etan Reisner Sep 23 '14 at 04:05
  • I suppose there is a smarter way to solve this by sed or awk. FYI, there is no comma in the info or version @EtanReisner – eleven Sep 23 '14 at 04:08
  • if you preprocess it to a valid json-line format. `jq` is perfect for your task. – kev Sep 23 '14 at 04:11
  • If you are writing a one-off job using sed/awk is fine. If you are writing something that needs to last then using a proper tool is a better idea. But sed/awk will work until they hit something that causes them to explode... at which point you get to pick up the pieces and hope nothing went too horribly wrong. – Etan Reisner Sep 23 '14 at 04:15
  • the smarter `sed` way is to concatentate your 2 sed scripts into one. No need to process the file twice, i.e. `sed -i 's/w/x/g;s/y/z/g' file`. All the other advice about `json` processing and maintainability still apply. It depends on your situation. Good luck. – shellter Sep 23 '14 at 14:24

1 Answers1

1
sed 's/\(version:[^,}]*,\)*\(.*\)info:[^,}]*\(,\{0,1}\)\(.*\)\(, *version:[^,}]*,\)*/\2info:"foo"\3\4/' file

(not tested) should do the trick (--posix for GNU sed)

NeronLeVelu
  • 9,908
  • 1
  • 23
  • 43