Missing space when using sed

Viewed 112

So I'm using lsof in combination with jq to convert to json. The original output example:

  [
    "blah",
    "1234",
    "yoda",
    "561u",
    "IPv4",
    "297229000",
    "0t0",
    "TCP",
    "15.60.74.17:1511",
    "(LISTEN)"
  ]
]

When using this with sed as:

sed -e "s/(//" -e "s/)//" | sed 's/.*:\([0-9]\+\).*/"\1",/g'

The output comes out as:

  [
    "blah",
    "1234",
    "yoda",
    "561u",
    "IPv4",
    "297229000",
    "0t0",
    "TCP",
"1511",
    "LISTEN"
  ]
]

Notice the space is not kept for "1511" not sure what i'm missing, i'm sure something silly, anyone know what i'm missing here?

3 Answers

It is because of the first .* in your second sed command. You could preseve whitespaces with something like this:

sed -e "s/(//" -e "s/)//" | sed 's/\S*:\([0-9]\+\).*/"\1",/g'

What I did is... I can explain, but it would be more fun if you figure it out yourself ;)

Edit: Okay, I say it: The regular expression of the second sed matches with the first non-whitespace character (\S means not whitespace, it is the opposite of \s). So, your second sed does not match the whitespaces before the values.

If I were in your position, I wouldn't use sed for this task. Unmarshalling the JSON input with a tool like Python and working with the values in that way would be a more confident method. That said, if I had to do it with sed, I would rewrite the second sed like this:

sed -e "s/(//" -e "s/)//" | sed 's/".*:\([0-9]\+\)",$/"\1",/g'

You could use a register, but it's easier to just match less:

echo '    "15.60.74.17:1511",' | sed -e "s/(//" -e "s/)//" -e 's/[0-9\.]*:\([0-9]\+\).*/\1",/g'

BTW, you don't need 2 sed's - just one with two -e's.

Would you please try the following:

sed -e 's/(\([^)]*\))/\1/' -e 's/[[:digit:].]*:\([[:digit:]]\+\)/\1/'
  • The 1st regex (\([^)]*\)) matches a substring surrounded by parens and the substring is captured in \1.
  • The 2nd regex [[:digit:].]*:\([[:digit:]]\+\) matches a substring composed of numbers and/or dots, followed by a colon : then followed by another sequence of numbers. The leading portion of numbers, dots, and a colon is removed.
Related