fucking the white bunny rabbit

this blog is a feature

perl regexp tips #1

with 5 comments

I didn’t know how to set the title of this post, so I choose a generic and useless title :P

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

Written by whitenoise

October 30, 2007 at 10:56 am

Posted in know-how, scripting

Tagged with , , , , , ,

5 Responses

Subscribe to comments with RSS.

  1. ce la si fa anche senza il Perl: sed -e ’s/[01]/&;/g’ foo

    jigen

    October 30, 2007 at 7:02 pm

  2. annoto tutto sul post :D
    anzi ne creo un altro :P

    whitenoise

    October 30, 2007 at 8:00 pm

  3. [...] fucking the white bunny rabbit Riflessioni, Pensieri, Rigurgiti di pazzia « perl regexp tips #1 [...]

  4. 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

  5. grazie mille :-)

    Fa sempre piacere avere tanti commenti che migliorano qualcosa scritto nel post ;-)

    whitenoise

    November 1, 2007 at 3:31 pm


Leave a Reply