简体   繁体   中英

Using Sed to search and replace JSON using regex from stdin

How would I use sed to search for the regex "pk": [0-9]+ in stdin and replace it with "pk": null ?

eg convert "pk": 123 to "pk": null

I've tried something like:

cat mydata.json | sed -e s/\"pk\"\:\s[0-9]+/\"pk\"\:\snull/g

but this has no effect. Cutting the regex back, it seems to break at [0-9]+ but I'm not sure why as this is both a simple and valid regular expression. What am I doing wrong?

这应该工作:

sed -e 's/"pk": [0-9]\+/"pk": null/g' mydata.json

the cat is not necessary.

sed 's/\(.*:\s*\)[0-9]\+/\1null/' file

if you need exactly pk :

sed 's/\("pk":\s*\)[0-9]\+/\1null/' file

eg

kent$  echo '"pk": 123'|sed 's/\(.*:\s*\)[0-9]\+/\1null/'
"pk": null

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM