Wednesday, October 5, 2022

[SOLVED] How to remove values from text file from different position

Issue

I have a file containing different values:

30,-4,098511E-02
30,05,-4,098511E-02
41,9,15,54288

I need to remove values from this file but from different position, for example:

30
30,05
41,9

I tried to do it with sed to remove the last value but my problem is when I encounter the 41,9,15,54288 it does not work. Any idea if there is a way to do it?

I tried this

echo "30,-4,098511E-02" | sed 's/,.*/,/'

Solution

Using sed

$ sed -E 's/(([0-9]+,?){1,2}),[0-9-].*/\1/' input_file
30
30,05
41,9


Answered By - HatLess
Answer Checked By - Katrina (WPSolving Volunteer)