perl regexp tips #1
I didn’t know how to set the title of this post, so I choose a generic and useless title
I have a text file with 81 characters (one and zero) per rows. I want to add a semicolon (“;”) between every character.
Eg. if the line is:
010101101010
and I want to became:
0;1;0;1;0;1;1;0;1;0;
This is the solution script (thanks dfa)
perl -ne ‘s/([01])/\1;/g; print’ file_input > file_output






ce la si fa anche senza il Perl: sed -e ‘s/[01]/&;/g’ foo
jigen
October 30, 2007 at 7:02 pm
annoto tutto sul post
anzi ne creo un altro
whitenoise
October 30, 2007 at 8:00 pm
[...] fucking the white bunny rabbit Riflessioni, Pensieri, Rigurgiti di pazzia « perl regexp tips #1 [...]
sed regexp tips #1 « fucking the white bunny rabbit
October 30, 2007 at 8:07 pm
Tanto questi post, finiscono tutti in questo modo, quindi, aggiungo il mio contributo.
In perl, è più interessante farlo con la substitution in place:
perl -pi -e ‘s/([01])/\1;/g’ nome_del_file
Nota ancora, l’uso dello switch -i che permette (io l’ho omesso) di fare una copia di backup del file che modifichi…
perl -p -i .bak -e whatever
Tannoiser
November 1, 2007 at 2:27 pm
grazie mille
Fa sempre piacere avere tanti commenti che migliorano qualcosa scritto nel post
whitenoise
November 1, 2007 at 3:31 pm