Tuesday, May 7, 2024
9
rated 0 times [  9] [ 0]  / answers: 1 / hits: 2201  / 1 Year ago, thu, december 8, 2022, 10:55:06

I have a file containing some data as in the following example:



data1: it is my data
data2: some more data
data3: even some more data


What I want is the following output in another file:



it is my data
some more data
even some more data


Please guide me how to do it!


More From » command-line

 Answers
2

This should work:



sed -i .bak 's/[^:]*: *//' file


Explanation: The -i .bak will edit the file in place, and create a backup of the original called filename.bak. s/pat/replacement/ means substitute pat with replacement. [^:]*: * means match the longest string of non-: characters, followed by a : then one or more space. The final result is that it will delete everything up to the first : and following spaces.



This approach has the advantage of working with data: or foo: or whatever and will also work if you have multiple : on the same line. For example, it can deal with this:



data: a line that contains : a colon!

[#26627] Saturday, December 10, 2022, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
utilitere

Total Points: 394
Total Questions: 110
Total Answers: 114

Location: Solomon Islands
Member since Wed, Mar 29, 2023
1 Year ago
;