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

Advertisements

5 thoughts on “perl regexp tips #1

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

    ๐Ÿ™‚

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s