While playing around with ex, I found one another script which is using "ed" to edit the files without actually opening them. Lets say in a text file, you want to insert particular string "Sunday is a Holiday" before every line on which the word Monday is found. Here is how you can write .ed script.
#####SCRIPT BEGINS#####
#!/bin/sh
TERM=vt100
export TERM
ed -s $1 "<<"MYEOF >/dev/null
1
/Monday/
i
Sunday is a holiday
.
w
q
MYEOF
#
exit 0
#####SCRIPT ENDS#####
Run the script as
bash-2.05$./test.ed abc.txt
So many hidden gems there!
Thursday, April 17, 2008
External editing of the files in Unix
While debugging a build break, I came across some scripts which were strangely named with extension .ed. Just out of curiosity I opened one of them and I was taken aback by the contents. I just wondered to myself how the hell did I never knew this?
It was using ex (actually just a command line interface to vi), which I now realize is such a powerful tool to edit files without actually opening them in editor window one by one. Let me give you a small example of how to use it. That ease of use will demonstrate its awesome powers.
Lets say you have a file named, abc.txt. The contents of the file are
Solaris
Linux
Hpux
Aix
Windows
Now lets say you want to replace string Windows with Apple. You can write one script named replace.ed which will look something like this:
#####SCRIPT BEGINS#####
#!/bin/sh
TERM=vt100
export TERM
ex -s $1 "<<" MYEOF >/dev/null
set noignorecase
%s/Windows/Apple/g
.
w
q
MYEOF
#
exit 0
##### SCRIPT ENDS#####
Now, all you have to do is to run the script by providing the file to be edited as an input.
bash-2.05$./replace.ed abc.txt
The utility example that I have displayed here is pretty simple, but surely good enough to emphasize the power of this tool. Just imagine how quickly and automatically you can do search and replace text for n number of files by calling this script from some iterative loop.
Happy Editing!
It was using ex (actually just a command line interface to vi), which I now realize is such a powerful tool to edit files without actually opening them in editor window one by one. Let me give you a small example of how to use it. That ease of use will demonstrate its awesome powers.
Lets say you have a file named, abc.txt. The contents of the file are
Solaris
Linux
Hpux
Aix
Windows
Now lets say you want to replace string Windows with Apple. You can write one script named replace.ed which will look something like this:
#####SCRIPT BEGINS#####
#!/bin/sh
TERM=vt100
export TERM
ex -s $1 "<<" MYEOF >
set noignorecase
%s/Windows/Apple/g
.
w
q
MYEOF
#
exit 0
##### SCRIPT ENDS#####
Now, all you have to do is to run the script by providing the file to be edited as an input.
bash-2.05$./replace.ed abc.txt
The utility example that I have displayed here is pretty simple, but surely good enough to emphasize the power of this tool. Just imagine how quickly and automatically you can do search and replace text for n number of files by calling this script from some iterative loop.
Happy Editing!
Thursday, January 10, 2008
Memory usage report for Unix system
Following simple script will generate the report of memory usage on unix system. It will store user memory, system memory and total memory consumption numbers in the report file, mem_stat.txt
##### SCRIPT BEGIN ########
#!/bin/sh
rm -rf /tmp/mem_stat.txt
top -f /tmp/mem.txt
while [ 1 ]
do
top -f /tmp/mem.txt;
grep Memory /tmp/mem.txt awk -F" " '{print $2,$5,$8}' sed -e 's/K//g' >> /tmp/mem_stat.txt;
rm -f /tmp/mem.txt;
sleep 30;
done>> /tmp/mem_stat.txt;
rm -f /tmp/mem.txt
########SCRIPT END#########
##### SCRIPT BEGIN ########
#!/bin/sh
rm -rf /tmp/mem_stat.txt
top -f /tmp/mem.txt
while [ 1 ]
do
top -f /tmp/mem.txt;
grep Memory /tmp/mem.txt awk -F" " '{print $2,$5,$8}' sed -e 's/K//g' >> /tmp/mem_stat.txt;
rm -f /tmp/mem.txt;
sleep 30;
done>> /tmp/mem_stat.txt;
rm -f /tmp/mem.txt
########SCRIPT END#########
Thursday, December 27, 2007
Command Completion Character
(Snippet from Microsoft Site)
To Activate Automatic Completion Permanently loadTOCNode(3, 'summary');
WARNING: If you use Registry Editor incorrectly, you may cause serious problems that may require you to reinstall your operating system. Microsoft cannot guarantee that you can solve problems that result from using Registry Editor incorrectly. Use Registry Editor at your own risk.
1. Click Start, click Run, type regedit, and then click OK.
2. To enable automatic completion for the computer, locate and click the HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor key.
3. To enable automatic completion for the current user, locate and click the HKEY_CURRENT_USER\Software\Microsoft\Command Processor key.
4. For folder name completion, double-click the CompletionChar value. Type in hexadecimal the control character that you want to use. For example, if you want to use the TAB key as the control character, the control character is 0x9 (type 9 as the value; Windows converts it to hexadecimal). If you want to use the same control characters that you use for a single command session, type 0x4 for CTRL+D and 0x6 for CTRL+F. You can use the same control character for both folder and file name completion.
5. For file name completion, double-click the PathCompletionChar value. Type in hexadecimal the control character that you want to use.For example, if you want to use the TAB key as the control character, the control character is 0x9 (type 9 as the value; Windows converts it to hexadecimal). If you want to use the same control characters that you use for a single command session, type 0x4 for CTRL+D and 0x6 for CTRL+F. You can use the same control character for both folder and file name completion.
6. Quit Registry Editor.If you enable this feature for the computer, the feature is available to all users. However, you can deactivate the feature for any individual user, or you can use different control characters for that user than you applied to the computer. The user settings take precedence over the computer settings.
To Activate Automatic Completion Permanently loadTOCNode(3, 'summary');
WARNING: If you use Registry Editor incorrectly, you may cause serious problems that may require you to reinstall your operating system. Microsoft cannot guarantee that you can solve problems that result from using Registry Editor incorrectly. Use Registry Editor at your own risk.
1. Click Start, click Run, type regedit, and then click OK.
2. To enable automatic completion for the computer, locate and click the HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor key.
3. To enable automatic completion for the current user, locate and click the HKEY_CURRENT_USER\Software\Microsoft\Command Processor key.
4. For folder name completion, double-click the CompletionChar value. Type in hexadecimal the control character that you want to use. For example, if you want to use the TAB key as the control character, the control character is 0x9 (type 9 as the value; Windows converts it to hexadecimal). If you want to use the same control characters that you use for a single command session, type 0x4 for CTRL+D and 0x6 for CTRL+F. You can use the same control character for both folder and file name completion.
5. For file name completion, double-click the PathCompletionChar value. Type in hexadecimal the control character that you want to use.For example, if you want to use the TAB key as the control character, the control character is 0x9 (type 9 as the value; Windows converts it to hexadecimal). If you want to use the same control characters that you use for a single command session, type 0x4 for CTRL+D and 0x6 for CTRL+F. You can use the same control character for both folder and file name completion.
6. Quit Registry Editor.If you enable this feature for the computer, the feature is available to all users. However, you can deactivate the feature for any individual user, or you can use different control characters for that user than you applied to the computer. The user settings take precedence over the computer settings.
Thursday, December 20, 2007
Add tasks to the folder
1. Open CM Synergy command line section
2. If you have a list of CMSynergy task ids in a file D:\tasks.txt, run command
D:> for /f %a in (D:\tasks.txt) do @ccm folder -modify -at %a "-Folder ID-"
2. If you have a list of CMSynergy task ids in a file D:\tasks.txt, run command
D:> for /f %a in (D:\tasks.txt) do @ccm folder -modify -at %a "-Folder ID-"
Library Path Variables
If some Unix application is crashing because it did not find some dependant library, you need to add the path of library file to standary library search path for Unix.
If /usr/foo/bar is the lcoation where you have the dependant library file available, then this is how you can add it to standard library for different OS's (assumming you are in bash shell):
Solaris & Linux:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/foo/bar
Aix:
export LIBPATH=$LIBPATH:/usr/foo/bar
HPUX:
export SHLIB_PATH=$SHLIB_PATH:/usr/foo/bar
If /usr/foo/bar is the lcoation where you have the dependant library file available, then this is how you can add it to standard library for different OS's (assumming you are in bash shell):
Solaris & Linux:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/foo/bar
Aix:
export LIBPATH=$LIBPATH:/usr/foo/bar
HPUX:
export SHLIB_PATH=$SHLIB_PATH:/usr/foo/bar
Monday, December 3, 2007
Rsh configuration
Requirement : User abc should be able to do an rsh and execute command on remote linux machine foo.bar.com from myfoo.bar.com
Solution : (Ideally first 3 steps should be enough to setup rsh execution. Please continue with steps 4 onwards to dig more.)
1. Create .rhosts file in user home directory.
2. Add following entries in it
foo.bar.com abc
myfoo.bar.com abc
3. Edit /etc/hosts.equiv (create new if it does not exist already) file to add entries
foo.bar.com abc
myfoo.bar.com abc
4. Edit /etc/securetty to have following entries
rsh
rlogin
rexec
rsync
5. Make sure that disable=no is set for following scripts in /etc/xinetd.d
rlogin
rsh
rexec
Solution : (Ideally first 3 steps should be enough to setup rsh execution. Please continue with steps 4 onwards to dig more.)
1. Create .rhosts file in user home directory.
2. Add following entries in it
foo.bar.com abc
myfoo.bar.com abc
3. Edit /etc/hosts.equiv (create new if it does not exist already) file to add entries
foo.bar.com abc
myfoo.bar.com abc
4. Edit /etc/securetty to have following entries
rsh
rlogin
rexec
rsync
5. Make sure that disable=no is set for following scripts in /etc/xinetd.d
rlogin
rsh
rexec
Subscribe to:
Posts (Atom)