Bhimalapuram.co.in
SED

Telugu SystemWithoutTelugu

sed STANDS FOR "streaming editor". sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed's ability to filter text in a pipeline which particularly distinguishes it from other types of editors. There are two ways for using substitutions - (1)redirect it to another file or (2)make changes in the file. We have to use the letter "i". To effect the changes in entire document "g" (global) has to be added.

Examples::

Disclaimer: Sed (rather sed- without capital letter- to be exact) has very precise syntax. Space omitted or unnecessary space, omission or unnecessary addition of punctuation marks/special characters will stop it from working or give wrong results. Check the syntax you use minutely.

(1). sed 's/Telengna/Telangana/g' NewState.txt> NewState2.txt. Or sed-i 's/Telengna/Telangana/g' NewState.txt. Here sed will correct the spelling mistake in the entire file. In first case inputfile (NewState.txt) remains unchanged, the corrected file is outputfile (NewState2.txt). In the second case the changes are made to the original file (NewState.txt) - the file in original format is lost. If used in a script, such changes can be made in entire directory using "for ....do ....done " loop. "for f in a*.html; do sed -i '46 s/తోండి/తోడి/' $f done". We used this to correct mistake in name of raga todi ..this script corrected the mistake "tondi" to "todi" in all files in the directory. Here 46 is the relevant line number. Using this one command (formed as 5mistakes.sh) the mistake in 40 files - spreadover in 14 directories - was corrected, checking app.15000 files.

(2). If a change has to be made at the beginning of lines: "sed -i 's/^/*/g' inputfile. Or "sed 's/^/*/g' inputfile> outputfile". Here "^" denotes beginning of the line. So "*" will be placed as the first character in each line.

(3). If a change has to be made at end of each line in a file- "sed -i 's/$/*/g' inputfile" or "sed 's/$/*/g' inputfile> outputfile". Here a "*" will be placed at the end of every line.

Multiple replacements with one command: "sed -i -e 's,to be replaced,new,g' -e 's,to be replaced,new,g' -e 's,to be replaced,<\/a><\/td><\/tr>,g' dhanyasi.txt" ....Here we wanted to insert "<\/a><\/td><\/tr>".......Note slashes and backslashes.......

(4)"echo hello | sed 's/[a-z]/\U&/g' " will give HELLO; and "echo HeLlO | sed 's/[A-Z]/\L&/g'" will give hello. The normal structure of sed command with "[A-Z] [a-z] \U& \L& " will work.

(5) Selecting a range of lines from a big file: In a file of say 19000 lines, if lines between 16224 to 16482 are to be selected the command will be "sed -n '16224,16482p;16483q' filename > newfile". Note second part with "q". This stops sed after the final record is checked - thus quickens the work.

(6)if we want to delete the lines from number 2 to 4, sed command is: "sed '2,4d' filename".

The above command just prints the lines on the terminal -does not remove from the file. To delete the lines from the source file itself use the -i option to the sed command. "sed -i '2,4d' filename".

(7)You can negate this operation and can delete lines that are not in the specified range using the following sed command: "sed -i '2,4!d' filename". Deletion may be risky. It may mean permanent removal. Note the " ! " .

Sed commands can be part of loop, or used with " | " - piped to or from another command.

Caution: We would suggest that (a) either a backup copy be kept or (b) the commands be used redirecting to another file. Often the need for original records may be felt after the work is effected.

Sed can do a lot more. Check the syntax carefully. Be careful when slashes and backslashes are involved. There are large number of books/articles on what sed can do - and also the official manual at wwwDOTgnuDOTorg.

Directory English Telugu SystemWithoutTelugu

Valid XHTML 1.0 Transitional

Valid CSS!