I have a file with text like these:


I'm trying to get value of timestamp. I'm trying with:

sed "s/.*timestamp\":\(.*\).*$/\1/g"

but it gives the value of timestamp but also all values until end of the line:


How can I get only:

  • If this is json formatted, then I suggest you use a json parser, e.g. jq or jtc – Thor Feb 7 at 8:41

You can do it sample with awk like this

awk -F'[,:}]' '{print $16}' input_file

Also if your file is in JSON format you can use jq

  • I woud like to extract with sed. Not always is in 16th position, and is not a jq file – user650034 Feb 7 at 8:32

There are two issues:

  1. You print non-matching lines
  2. You match too much

The first can be solved with using -n and p, the second by narrowing down what you match, e.g. with ERE:

sed -nE 's/.*"timestamp":([0-9]+).*/\1/p'
  • thanks! it works? But I'm trying to understand your solution. Why "-nE" modifier, why you not use character substituion mask like (xxxxxx) to use then \1 – user650034 Feb 7 at 9:00
  • The command that I was using is : sed "s/.*timestamp\":(.*)}.*/\1/g" and it gives timestamp value but also the values until the end of the line – user650034 Feb 7 at 9:03
  • @user650034: I suggest you look -n and -E up in the manual – Thor Feb 7 at 9:15
  • Do you kwno one manual sed specified? – user650034 Feb 7 at 9:16
  • @user650034: it should be installed with send so running man sed would work. Otherwise put it into Google, that works as well – Thor Feb 7 at 9:26

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.