The solution is two step:
expression==y'''' + y'' + x^2 y' - 6 y + 8 Cos[x]
For the coeffecients of the function and the derivatives:
If[Length[#] > 1, #[[2]],
0] & /@ (CoefficientList[expression, #] & /@ {y, y', y'', y''',
y''''})
For the inhomogeneity:
expression - (If[Length[#] > 1, #[[2]],
0] & /@ (CoefficientList[expression, #] & /@ {y, y', y'', y''',
y''''}).{y, y', y'', y''', y''''})
There remains the problem whether the the highest order should be detect programmatical or not. This can be done by using the first solution of @Michael-e2.
Length@CoefficientList[
y'''' + y'' + x^2 y' - 6 y + 8 Cos[x] /.
Derivative[n_][y] :> y^(n + 1), {y}]-2
(*4*)
With that
dlist=Table[Derivative[n_][y],{n,0,Length@CoefficientList[
y'''' + y'' + x^2 y' - 6 y + 8 Cos[x] /.
Derivative[n_][y] :> y^(n + 1), {y}]-2}]
(*{y, Derivative[1][y], y^\[Prime]\[Prime],
\!\(\*SuperscriptBox[\(y\),
TagBox[
RowBox[{"(", "3", ")"}],
Derivative],
MultilineFunction->None]\),
\!\(\*SuperscriptBox[\(y\),
TagBox[
RowBox[{"(", "4", ")"}],
Derivative],
MultilineFunction->None]\)}*)
The two inputs are for the coefficients of the function and the derivative:
coeffs = If[Length[#] > 1, #[[2]],
0] & /@ (CoefficientList[expression, #] & /@
Table[Derivative[n][y], {n, 0,
Length@CoefficientList[
expression /. Derivative[n_][y] :> y^(n + 1), {y}] - 2}])
and for the arbitrary not on y dependent part:
expression - (If[Length[#] > 1, #[[2]],
0] & /@ (CoefficientList[expression, #] & /@
Table[Derivative[n][y], {n, 0,
Length@CoefficientList[
expression /. Derivative[n_][y] :> y^(n + 1), {y}] -
2}]).Table[
Derivative[n][y], {n, 0,
Length@CoefficientList[
expression /. Derivative[n_][y] :> y^(n + 1), {y}] - 2}])
An intermediate coefficient function for a derivative order is allowed to be zero and the highest order of the derivative is set arbitrary. The type of the inhomogeneity can be arbitrary too. It is not investigated and derived but substraction only.
The premises are linearity and ODE and there is not functional dependences for the function or the derivatives in the suppose left or right hand side of the ODE or in the expression, term.
CoefficientList[expression /. y -> (Exp[α #] &) /. (q_. Exp[α #] &) -> q, α]
orCoefficientList[expression /. y -> (Exp[α #] &) /. Function[q_] -> q Exp[-α] /. Slot[_] -> 1, α]
. I'm sure there are better ways though. $\endgroup$