-1

I have a file with text like these:

"id":1519539,"description":"xxxxxxxx","severity":"yyy","pasahelpdesk":null,"subSource":"","dateReception":{"timezone":{"name":"Europe\/France","timestamp":1549493320},"sg_datatables_editable":[
false,false,false,false,false,false,false,false,false,false,false]},{
"id":1519540,"description":"xxxxxxxx","severity":"yyy","pasahelpdesk":null,"subSource":"","dateReception":{"timezone":{"name":"Europe\/France","timestamp":1549493340},"sg_datatables_editable":[
false,false,false,false,false,false,false,false,false,false,false]},{

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:

1549493320},"sg_datatables_editable":[
false,false,false,false,false,false,false,false,false,false,false]},{
1549493340},"sg_datatables_editable":[
false,false,false,false,false,false,false,false,false,false,false]},{

How can I get only:

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

2 Answers 2

1

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

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

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'
7
  • 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, 2019 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, 2019 at 9:03
  • @user650034: I suggest you look -n and -E up in the manual
    – Thor
    Feb 7, 2019 at 9:15
  • Do you kwno one manual sed specified?
    – user650034
    Feb 7, 2019 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, 2019 at 9:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .