Wednesday, January 23, 2008

VI editor: Search and Replace text

IMPORTANT! Before start changing your text file, please remember the commands to UNDO and REDO:


:u or :undo[n]
:red or :redo[n]

where "n" is the number of changes. The default value is "n=1".


With the command (in ESC mode):

:%s/foo/bar/gc

the active file will be searched for the pattern "foo" and replaced with "bar". For every replacement you'll be asked for a confirmation.

With

:%s/foo/bar/g

you won't be asked for a confirmation.

:%s/foo/bar/gi is case-insensitive
:%s/foo/bar/gI is case-sensitive

With ESC or "q" you terminate the searching.

Note that above we used the % symbol. This is a shortcut that means "from the first line to the end of file".

The "substitute" (:s) command accepts, before of itself, a range of lines, e.g.:


:28s/text1/text2/ only on line 28
:.s/text1/text2/ only on the current line
:.,20s/text1/text2/ from current to line 20
:1,$s/text1/text2/ from line 1 to the end of file
:%s/text1/text2/ over the whole file. Like 1,$


Morevore please notice that if you want to use special characters like [ you have to escape them, like:


:.,$s/lego/\[lego/gc


this command substitutes all the occurrences of "lego" with "[lego", from the current line to the end of file, asking for confirmation before each substitution.

IMPORTANT. Please notice that you have to escape symbols ONLY within the substituting text, not in the text you look for. E.g.:

:.,$s/(ciao)/\[\(ciao)\)\]/gc

substitutes "(ciao)" with "[(ciao)]".

Saturday, January 12, 2008

History Brush on Photoshop CS3: Make a Black&White Image with coloured areas

Open an image in Photoshop CS3.



Edit the image as necessary. Open the Image->Adjustements->Black&White in order to get a nice b&w image.



Click on "History" and click on the little square on the left of a status of the image just before the black&white conversion. This status will be taken as "Source" for the "History Brush".



Now come back to the image clicking on the last status. Select the "History Brush" and "paint" an area of your image.


You'll see the area coming back to its original colour!

Friday, January 11, 2008

Rename files

Thanks to Stephan [my room-mate :-) ] I discovered the command "rename"!

N.B. The following works on Debian:

Let's say I want to rename a file callled "test11.txt" to "test12.txt"; the command which does the job is:

rename 's/11/12/' test11.txt

where 's/11/12/' is a perl-style Regular Expression (RE), where 's' stands for "string".

If you are not sure about your RE you can test it with the "-n" option, as:

rename -n 's/11/12/' test11.txt

it just shows to you the result of the operation without actually doing it.

If you have many files in a folder and you want to rename all of them you can use the "*" wildcard:

rename 's/11/12/' *

the command above renames all files containing a string "11" in the name, changing it to "12".

N.B. And the following works on SLC4 (and I guess on Fedora Core then)

the command:

rename .htm .html *.html

changes all the files ending with .htm into .html

P.S.
And if you want, for example, to rename or to add a suffix to a list of files in a directory you can use these commands:

for FILE in * ; do mv $FILE $FILE.txt ; done

where we add the suffix .txt to all files in the directory.
(Note for physicists: Useful to rename AOD or DPD data files to .root files ;-) )

Wednesday, January 9, 2008

VI editor: comment a whole line or N lines

If you want to comment N lines of a Bash script (for example) with the VI (or VIM) editor you have to type in ESC- mode (type ESC before typing the command):


:.,25s/^/# /

where ".,25s" stands for "from the current line to line number 25" and the sequence added to comment a line is "# " (hash plus a white space).
For a single line:

:11s/^/# /

for the 11th line or

:.s/^/# /

for the current line pointed by the cursor.



To uncomment:

:1,$s/^# //

where "$" stands for "last line", i.e. "from line 1 to the end of file".

Print the Copiright Sign in Python

To print the Copiright Sign with Python we need to use the Unicode string literal format.

A quoted string with a "u" or a "U" just before the leading quote is an Unicode string.
The escape sequence "\N{name}" prints the character specified by "name", where "name" is a standard Unicode name, a s listed in www.unicode.org/charts/

Example:

print u'\N{Copyright Sign}'

prints the Copyright sign.

Tuesday, July 10, 2007

Grave errore di stampa del OR logico nel libro C++ di Dietel (Apogeo)

(An English version of this post follows at the end)



Se anche voi, come me, avete ottenuto un errore di compilazione del tipo

stray '\166' in program

...sicuramente state seguendo la versione italiana del libro di Dietel & Dietel "C++ Fondamenti di Programmazione" (seconda edizione, delle altre non so) !!!!! ;-)

In tutto il libro, dopo aver introdotto in modo corretto l'operatore OR logico "||" a pag. 202, si trova il simbolo "¦¦" al posto del "||" !!!!!

E il codice Unicode del simbolo "¦" e' proprio il "166" del messaggio di errore.

Cambiate "¦¦" con "||" e tutto funzionera' a meraviglia! :-)

Ciao!

English:
If you get an error like the red above, you probably input a character "¦" in your code. "166" is the Unicode code of the symbol "¦".
In an italian version of the Dietel book "C++ How to Program" there's a bad typo: in the whole book the logical OR operator is written like "¦¦" instead of "||"!! And if you compile the examples you get the error above.

Tuesday, May 15, 2007

Ganga Job submit() over Grid ... could have never started!! :-D

He he he!!! :)

It was some hours that I tried to submit an Athena job over the Grid with Ganga...

...the job was "loaded" fine, but it remained always in the "new" status.

In the end...I discovered that I mispelled the submit command into my python job script....

....in the last line I used

job.submit

instead of

job.submit()

Ah ah ah!!!! :-D ;-) Hence the numerous jobs I launched...never started!!!!!!! :-D Ah ah ah!!! :-P

Ehy Ma, Pay attention!! ;-)

How to fix "cannot access parent directories" shell error. (Also with Ganga,DQ2 on Grid)

If you are working in a shell and you get an error like this:

shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory

probably you are working in a directory you have deleted from within another shell. I.e. you are in shell A and you are working within /home/Joe/test/ folder. After a while you open a new shell B and you work there. After completed your work you delete the folder /home/Joe/test/ folder and close the shell B. After a while you come back to shell A and start to work there...and you get the Mistery error message, because actually you are working in a deleted directory! ;-)

Try this at the prompt [trust me, it's ugly but safe! :-) ] :

ls $(pwd) && ls /etc/
cd /

first you get an error like "No such file or directory", because the working folder has been previously deleted and doesn't exist anymore. But then, if you move to an existing directory, the problem is fixed! :-)

P.S. You can get this error also working with Ganga and DQ2. If you are working in a .../Local/jobid directory (e.g. in order to read the 'stdout' file) of a Job that you have deleted from withing Ganga CLIP (Command Line IPython Interpreter) with the command jobs[jobid].remove() and try to use dq2_get in order to retrieve an output from the Grid, you get exactly the same error above. Try the same solution.

Good work! :-)

Tuesday, May 8, 2007

How to fix the TortoiseSVN "invalid port number" problem

I waste a while to find the solution to this problem, searching the web and trying and melting various recipes. The following solution worked fine for me, both for plain TortoiseSVN under Windows.

Problem: When you try to connect to the repository hosted by another machine over a network (LAN or Internet) with SSH protocol, using PuTTY as client, you get an error like this:

"Invalid Port Number"

and after that the connection hangs or falls down.


Solution: Right-Click into a whichever folder; a pop-up menu shows up. Open the "TortoiseSVN" and then the "Settings" menu. Check the "Network" section: the "SSH client" field must be empty.

Now open PuTTY.exe and check the name you give to the session for connecting the machine where your SVN repository is. Assume you named "mysvnhost" the session to connect the "host.example.net" machine.

Now you have to use this name at the place of the machine name, when you set the address for the "svn+ssh" access protocol. For example like this:

svn+ssh://username@mysvnhost/folder/folder/svn/repository/trunk/

Friday, May 4, 2007

How to safely use backspace with VI editor under linux?

Sometimes, when you log on a linux machine and try to edit a file with VI editor, you get characters like '?>' when you click on backspace in order to delete a string. How to fix it?

Solution:

In your HOME directory (if you don't know where is it, just type 'cd' at the prompt) edit your .bashrc (or your .bash_profile) file and add these lines:

#Let VI work with Backspace
echo "I run 'stty sane'"
stty sane
echo "Done"

The .bashrc file is loaded (and sourced) every time you log in the machine or you start a shell; so your VI will properly work for ever since now on! : )

P.S. I like to know what my .bashrc does, but if you prefer you can delete the echo lines, of course.