0

I want to split "vulnid": "CVE-2018-17435 (hdf5)splitmehere" on the second space. How can I accomplish that using sed?

MY INPUT:

"vulnid": "CVE-2018-17435 (hdf5)splitmehere"
"stuff":"stuffhere(somedata)"
"vulnid": "CVE-2018-17435 (hdf5)splitmehere"
"stuff":"stuffhere"
"vulnid": "CVE-2018-17435 (hdf5)splitmehere"
"stuff":"stuffhere"

EXPECTED OUTPUT:

"vulnid": "CVE-2018-17435"
"product:" (hdf5)splitmehere"
"stuff":"stuffhere(somedata)"
"vulnid": "CVE-2018-17435"
"product:" (hdf5)splitmehere"
"stuff":"stuffhere"
"vulnid": "CVE-2018-17435"
"product:" (hdf5)splitmehere"
"stuff":"stuffhere"

I have tried is to use a variant of this expression:

sed -r 's/(*.*) (*.*) (*.*)/\1 \2 \3/'
  • That exact sed expression is invalid, and gives an error (using GNU sed). If it were "fixed" by removing the erroneous *s, it would do nothing (giving the same output as input). Could you show the "variant" that you actually tried? – JigglyNaga Nov 13 '18 at 11:34
  • I lost it from terminal history. However, I have another expression: sed -e "s/ /\n/2". Splits on the second space occurence. Later I will do what I want to do on the line that starts from (, and { tr '\n' ' ' < ${datarss}input.txt; echo; } | sed '$s/ $//' – creed Nov 13 '18 at 11:39
1

One possible solution:

$ sed 's/\("vulnid":\) \(.*\) \(.*\)"/\1 \2"\n"product": "\3"/' file
"vulnid": "CVE-2018-17435"
"product": "(hdf5)splitmehere"
"stuff":"stuffhere(somedata)"
"vulnid": "CVE-2018-17435"
"product": "(hdf5)splitmehere"
"stuff":"stuffhere"
"vulnid": "CVE-2018-17435"
"product": "(hdf5)splitmehere"
"stuff":"stuffhere"    
  • Thank you kind sir! I had something similar but with typo. – creed Nov 13 '18 at 11:50
1

Your own suggestion

sed -e "s/ /\n/2"

nearly does what you describe. All that remains is to put the extra "product:" text around the linebreak:

sed -e 's/ /"\n"product:" /2'

(I changed the outer quotes to single-quotes, in order to use double-quotes inside the expression.)

Some versions of sed may not interpret \n as a newline. The sed specification states:

A line can be split by substituting a <newline> into it. The application shall escape the <newline> in the replacement by preceding it by a <backslash>.

ie.

sed -e 's/ /"\
"product:" /2'

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.