perl - sed, replace globally a delimiter with the first part of the line -


lets have following lines:

 1:a:b:c 2:d:e:f 3:a:b 4:a:b:c:d:e:f 

how can edit sed (or perl) in order read:

 1a1b1c 2d2e2f 3a3b 4a4b4c4d4e4f 

i have done awk this:

 awk -f':' '{gsub(/:/, $1, $0); print $0}' 

but takes ages complete! looking faster.

'tis tad tricky, can done sed (assuming file data contains sample input):

$ sed '/^\(.\):/{ s//\1/ : retry s/^\(.\)\([^:]*\):/\1\2\1/ t retry }' data 1a1b1c 2d2e2f 3a3b 4a4b4c4d4e4f $ 

you may able flatten script 1 line semi-colons; sed on macos x bit cranky @ times , objected parts, split out 6 lines. first line matches lines starting single character , colon , starts sequence of operations when recognized. first substitute replaces, example, '1:' '1'. : retry label branching - key part of this. next substitution copies first character on line on first colon. t retry goes label if substitute changed anything. last line delimits entire sequence of operations matched line.


Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -