Recent Notes

Displaying keyword search results 1 - 11
Created by magnum on June 20, 2011 20:52:22    Last update: June 21, 2011 08:44:01
Several ways to remove empty lines from a file with Unix utilities: Remove all empty lines with grep : cat test.txt | grep -v '^\s*$' or grep -v '^\s*$' test.txt Remove all empty lines with sed : sed '/^\s*$/d' test.txt Remove empty lines at the beginning of file with sed : sed '0,/\w/p' leadingBlankLines.txt | sed '0,/\w/d...
Created by woolf on May 20, 2011 14:00:35    Last update: May 20, 2011 14:01:36
To remove new line characters from a file: with tr : tr -d '\n' < the-file.txt or cat the-file.txt | tr -d '\n' with sed : sed ':a;N;$!ba;s/\n//g' the-file.txt or cat the-file.txt | sed ':a;N;$!ba;s/\n//g' The sed version is a bunch of commands to manipulate the register etc. Regex replace " s/\n//g " does not work because sed regex works on a single line. More tips from this Linux blog: http://linux.dsplabs.com.au/rmnl-remove-new-line-characters-tr-awk-perl-sed-c-cpp-bash-python-xargs-ghc-ghci-haskell-sam-ssam-p65/
Created by Dr. Xi on March 28, 2011 11:11:33    Last update: March 28, 2011 11:13:21
grep is a versatile command with many variations (grep, egrep, fgrep, then various implementations). It uses a regula expression (regex) pattern to filter input. But then there are basic and extended flavors of regex - leading to even more confusion. And, beware that there are lots of bad examples of regex in the wild... There are two critical questions to ask when you use grep: which grep implementation are you using? what is the flavor of the regex? Here are some examples for gnu grep v2.7: # Find all numbers (no decimal point), basic regex... Use the -o flag to show only the matching part instead of the whole matching line: grep -o -E '\b[0-9]{2}\b' The good thing about the gnu grep is that it...
Created by Dr. Xi on December 04, 2009 04:33:05    Last update: December 04, 2009 04:33:05
Variable Meaning $_ The default or implicit variable. @_ Within a subroutine the array @_ contains the parameters passed to that subroutine. $a, $b Special package variables when using sort() $<digit> Contains the subpattern from the corresponding set of capturing parentheses from the last pattern match, not counting patterns matched in nested blocks that have been exited already. $. Current line number for the last filehandle accessed. $/ The input record separator, newline by default. $| If set to nonzero, forces a flush right away and after every write or print on the currently selected output channel. Default is 0 (regardless of whether the channel is really buffered by the system or not; $| tells you only whether you've asked Perl explicitly to flush after...
Created by Dr. Xi on September 29, 2008 23:05:12    Last update: September 29, 2008 23:06:16
These variables are set or used by the Unix shell to modify its behavior. Variable Description ENV=file Name of script that gets executed at startup; Usually, ENV=$HOME/.kshrc FCEDIT=file Editor used by fc (fix command) command. If $FCEDIT is not defined, use $EDITOR, otherwise use the default (vi or ed). FPATH=dirs Directories to search for function definitions; undefined functions are set via typeset -fu . FPATH is searched when these functions are first referenced. HISTFILE=file File in which to store command history. Default is $HOME/.sh_history for Korn shell, $HOME/.bash_history for Bash. If not set, history is lost after logout. HISTSIZE=n Max number of commands to keep in history. HOME=dir Home directory; set by login from passwd file. IFS='chars' Internal field separators. Default is space, tab, and...
Created by Dr. Xi on September 05, 2008 23:28:14    Last update: September 05, 2008 23:28:14
In order for a user to run sudo , he must be in the sudoers file. You can use your favorite editor to change the file: sudo gedit /etc/sudoers Or, if you use vim , you can use: sudo visudo Scroll down to the line root ALL = (ALL) ALL and add the_user_to_add ALL = (ALL) ALL If you are not a sudoer but have root password, you should do su - to become root before editing /etc/sudoers . If you are using Ubuntu, this works too: sudo adduser username admin
Created by Dr. Xi on December 12, 2007 20:30:01    Last update: December 12, 2007 20:32:23
This is a script to tail a log file through the web browser. It uses AJAX, apache web server, mod_python, UNIX utilities tail (requires the --lines switch) and wc . The log file may reside on the web server or any other host accessible from the web server through SSH. Although it's written in python, it should be easy to port to other languages such as Perl. Apache httpd.conf : LoadModule python_module modules/mod_python.so ... Python script: import time, os from os.path import basename ...
Created by Dr. Xi on December 11, 2007 19:42:17    Last update: December 11, 2007 19:44:06
In .vimrc, or _vimrc on Windows, add line: " will try to open file in dos, unix, or mac forma...
Created by Dr. Xi on June 09, 2007 02:26:07    Last update: June 09, 2007 02:26:07
Use the nl command to display line numbers for each line in a file: nl file1 # display contents of file1, but...
Created by Dr. Xi on June 02, 2007 00:45:00    Last update: June 02, 2007 00:45:00
Use the diff command to find the difference between two files. It takes two arguments: 2 files, 1 file and 1 directory, or 2 directories. diff file1 file2 # display the differe...
Created by Dr. Xi on May 05, 2007 19:23:39    Last update: May 05, 2007 19:25:11
DOS files end a line with <CR><NL>, Unix files end a line with <NL> only, Mac files end a line with <CR>. When you edit a DOS file on Unix, you see the annoying ^M's at the end of each line. If you edit a Mac file on Windows, there'll be no line breaks - all lines are displayed as one line! You can use the "e" command to specify the file format: :e ++ff=dos which interpretes the line ending acording to the file format specified but does not alter the file. If you want to change a UNIX file to a DOS file, use this instead: :set ff=dos which would change all \n to \r\n.