Search code examples
regexreplacerecord

Regex to replace the last n delimiters in record


I have a csv file with records like this:

^Tablete Internet^|GoClever|^TAB I71^|^Tableta GoClever TAB I71, InfoTMIC iMAPx220 1GHz MKV, MOV, VOB, DAT, WMV, AVI, 3GP, MP4, RM, RMVB, FLV, MKV, MOV, VOB, DAT, WMV, MP3!WAV!OGG!FLAC!APE!AAC, Conectori: 1x mini jack 3.5 mm, 2 porturi USB 2.0, 4000 mAh lithium-ion polymer 3.7V^!92.00!USD!w!24!0!Link!!^5 ron^!!|
Group|Prod|Den|Description!12!EUR!^wait time^!160!Modified!Link!^Spec Obs^!^tv none^!more!info|

I need to replace the last 10 of the "!" from the end of the row with "|" so finaly it will look like:

^Tablete Internet^|GoClever|^TAB I71^|^Tableta GoClever TAB I71, InfoTMIC iMAPx220 1GHz MKV, MOV, VOB, DAT, WMV, AVI, 3GP, MP4, RM, RMVB, FLV, MKV, MOV, VOB, DAT, WMV, MP3!WAV!OGG!FLAC!APE!AAC, Conectori: 1x mini jack 3.5 mm, 2 porturi USB 2.0, 4000 mAh lithium-ion polymer 3.7V^|92.00|USD|w|24|0|Link||^5 ron^|||
Group|Prod|Den|Description|12|EUR|^wait time^|160|Modified|Link|^Spec Obs^|^tv none^|other|info|

I've tried with:

sed 's/!\([^\^!]*\)!\([^!]*\)$/|\1|\2/'

but this replaces only the last 2 occurences of "!". If I try

sed 's/!\([^\^!]*\)!\([^!]*\)!\([^!]*\)$/|\1|\2\3/'

it does not match anything... So how can I do this?

Thanks!


Solution

  • I know is not the most elegant solution but I've managed to do it with:

    perl -npe 's/^(.*)!(.*)!(.*)!(.*)!(.*)!(.*)!(.*)!(.*)!(.*)!(.*)!(.*)$/$1|$2|$3|$4|$5|$6|$7|$8|$9|$10|$11/'
    

    Thanks!