I am putting collection of small scripts/codes for general use. I have collected them from my past use and will continue adding as and when I find some interesting. You are most welcome to use them or to contact me for help. Don’t expect something of Einstein’s proportion here. If such scripts were not very basic, I would not be pasting them here.
SED
When using sed, try to use find and replace instead of standard replace command as former is much faster. e.g. when replacing china with india in a text we usually write – sed ‘s/china/india/g’
Instead use this sed ‘/china/ s/china/india/g’ for faster results.
When we have a rough idea of which part of file is usable ( for example if I know I am going to use only first fifty lines of a thousand lines file only) we should better stop the execution of command as soon we are done. e.g. when printing first 50 lines of a file we usually write- sed -n ‘1, 50p’
Better to use this sed -n ’51q;1, 50p’
A few usually needed one-liners–
To double space – sed G
To double space a file which already has blank lines in it – sed ‘/^$/d;G’
To insert a blank line above every line which matches “pattern” – sed ‘/pattern/{x;p;x;}’
To insert a blank line below every line which matches “pattern” – sed ‘/pattern/G’
To print the line immediately before “pattern”, but not the line containing it – sed -n ‘/pattern/{g;1!p;};h’
To print the line immediately after “pattern”, but not the line containing it – sed -n ‘/pattern/{n;p;}’
To delete all blank lines – sed ‘/^$/d’
To create grep from sed
(equals grep ‘pattern’ ) – sed -n ‘/pattern/p’
(equals grep -v ‘pattern’) – sed -n ‘/pattern/!p’
MAILX
To send attachment use uuencode feature of mailx. This takes care of encoding file so that file doesn’t becomes part of mail, rather only an attachment. When sending mail to more than one user, separate addresses with space character. “-m” option will take care to exclude the ugly mime message appearing in the mail. I prefer to write mail first in a file and then cat it over to mail. This makes sure that formatting I want doesn’t get destroyed in mail creation.
Refer to this example script where Bush is sending an email to some big guns with a file attached to it (Plan_Of_Attack.doc)
Here is sample program (in C) related to link list. There are a few functions and I will be adding some as soon as I get some spare time. Please report me bugs/omissions, as this is first draft. Feel free to copy if it is of any help.
Sample program – Link List in C
Here is sample program (in C) related to signal handling.
Calling sqlplus/ftp from shell scripts
Here are example code for running sql scripts in sqlplus session and transferring file via ftp (both can be used in calling from within a shell script).
Some sql queries
Another usually occuring situation is when one needs to find out if a column in a table contains a non-numeric character when you expect only numbers. For instance, if you design a age field, you don’t expect a age like B18. Although it is a stupid programming practice to have VARCHARs or CHARs when you can do well with NUMBER, you never know what legacy programmers have left and you can never question your boss (who is most of the time designer of such code).
I use this simple query(OK not that simple) to find the guilty rows-
select * from table_name
where length(replace(translate(column_name),’0123456789′,’0′),’0′,”))>0
What it does is translates all digits 0 to 9 to 0 and then replaces all 0s with null character. Now if there is a non-numeric character it will still be left in that column, giving you length exceeding 0. I have checked the same in Oracle9i.
4 Comments on “Softview”
Leave a Reply
XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>
MS SQL Server has an inbuilt function to test if a string is a numeric value
The same query in T-SQL becomes –
SELECT * FROM table_name
WHERE ISNUMERIC(column_name) != 1
<sarcasm>
Perhaps the reason Oracle doesn’t have this function is to encourage better DB design ? (using correct datatype for the column)
</sarcasm>
hey Dear virendra 🙂
My interest same as you are working .
hi r u therer?
kaialsh says : no i am not hear
r u there?
if not, go there and fill presentably otherwise you are fire ………
Nice way to advertise yourself kailash. Unfortunately your link requires too much work right now.