Technology / Software / 

05 Jan 2022

Replace last string in each line of text using 'sed'

One-liner to replace the last comma-space sequence in each line with a tab… useful to turn lists with dates separated by a comma into TSV for use in tables.

Bash sed -E 's/(.*), /\1\t/' < inputfile > outputfile

N.B.: The second regular expression contains \1 (forward-slash, numeral one), not to be confused with \l (forward-slash, lowercase letter L).


If our input file looks like this:

Went to the library, June 10 Saw Jack, Jill, and Henry, June 11 Picked strawberries, June 13

Then by running the command above, we transform it into (after making tabs visible by piping the output through cat -T):

Went to the library^IJune 10 Saw Jack, Jill, and Henry^IJune 11 Picked strawberries^IJune 13

Note that on the second line, where there are multiple commas, only the final comma-space sequence is replaced. This was particularly important when I was trying to do some ETL shenanigans with a poorly-escaped CSV file full of mixed text, dates, and financial data.