Unix Shell Scripting For DBA’s (PART – 07)
GNU
GNU stand for “GNU’s Not Unix!”
reference of this article from : www.orskl.com
It is a recursive acronym chosen because GNU’s design is Unix-like, but differs from Unix by being free software and containing no Unix code.
Here we are discussing five sub topics.
- Interactive editing
- Non Interactive editing
- AWK programming
- AWK print program
- AWK variables
1.Interactive editing
What is sed?
A Stream EDitor is used to perform basic transformations on text read from a file or a pipe. The result is sent to standard output. The syntax for the sed command has no output file specification, but results can be saved to a file using output redirection. The editor does not modify the original input.
What distinguishes sed from other editors, such as vi and ed, is its ability to filter text that it gets from a pipeline feed. You do not need to interact with the editor while it is running; that is why sed is sometimes called a batch editor. This feature allows use of editing commands in scripts, greatly easing repetitive editing tasks. When facing replacement of text in a large number of files, sed is a great help.
sed commands
The sed program can perform text pattern substitutions and deletions using regular expressions, like the ones used with the grep command.
The editing commands are similar to the ones used in the vi editor:
For all commands in sed will go with man command :
Most widely used Sed editing commands
Command Result
a\ Append text below current line.
c\ Change text in the current line with new text.
d Delete text.
i\ Insert text above current line.
p Print text.
r Read a file.
s Search and replace text.
w Write to a file.
Apart from editing commands, you can give options to sed. An overview is in the table below :
Sed options
Option Effect
-e SCRIPT . Add the commands in SCRIPT to the set of commands to be run while processing the input.
-f Add the commands contained in the file SCRIPT-FILE to the set of commands to be run while processing the input.
-n Silent mode.
-V Print version information and exit.
Printing lines containing a pattern
This is something you can do with grep, of course, but you can’t do a “find and replace” using that command. This is just to get you started.
Create a file with our own content .
1 2 3 4 5 6 7 |
[oracle@dba15 shellscripting]$ vi file.txt This is the first line of an example text. It is a text with erors. Lots of erors. So much erors, all these erors are making me sick. This is a line not containing any errors. This is the last line. |
Cat the file which we created.
1 2 3 4 5 6 7 |
[oracle@dba15 shellscripting]$ cat file.txt This is the first line of an example text. It is a text with erors. Lots of erors. So much erors, all these erors are making me sick. This is a line not containing any errors. This is the last line. |
To print the required pattern using p option
Use sed now to search a pattern called erors.
1 2 3 4 5 6 7 8 9 10 |
[oracle@dba15 shellscripting]$ sed '/erors/p' file.txt This is the first line of an example text. It is a text with erors. It is a text with erors. Lots of erors. Lots of erors. So much erors, all these erors are making me sick. So much erors, all these erors are making me sick. This is a line not containing any errors. This is the last line. |
In the above example sed will reads every line for word ‘erors’.if its not found erors word also it prints in output.
In case it founds then it will print for two times.
To avoid these type of repeats we have an option called -n .
Remove repeated outputs using sed while searching for a pattern.
1 2 3 4 |
[oracle@dba15 shellscripting]$ sed -n '/erors/p' file.txt It is a text with erors. Lots of erors. So much erors, all these erors are making me sick. |
Exclude lines containing a pattern
Use sed now to exclude a pattern called erors.
1 2 3 4 |
[oracle@dba15 shellscripting]$ sed '/erors/d' file.txt This is the first line of an example text. This is a line not containing any errors. This is the last line. |
All the erors are not part of the output.
Range of lines
Remove the lines from 2 to 4 from the file in the output.
1 2 3 4 5 6 7 8 9 10 11 12 |
[oracle@dba15 shellscripting]$ cat file.txt This is the first line of an example text. It is a text with erors. Lots of erors. So much erors, all these erors are making me sick. This is a line not containing any errors. This is the last line. [oracle@dba15 shellscripting]$ sed '2,4d' file.txt This is the first line of an example text. This is a line not containing any errors. This is the last line. |
To print 2,3,4 lines from a file
1 2 3 4 |
[oracle@dba15 shellscripting]$ sed -n '2,4p' file.txt It is a text with erors. Lots of erors. So much erors, all these erors are making me sick. |
Print the lines starting from 3rd line until end.
1 2 3 |
[oracle@dba15 shellscripting]$ sed '3,$d' file.txt This is the first line of an example text. It is a text with erors |
Print from one pattern until next pattern in a file.
1 2 3 4 5 6 7 8 9 10 11 12 |
[oracle@dba15 shellscripting]$ cat file.txt This is the first line of an example text. It is a text with erors. Lots of erors. So much erors, all these erors are making me sick. This is a line not containing any errors. This is the last line. [oracle@dba15 shellscripting]$ sed -n '/a text/,/This/p' file.txt It is a text with erors. Lots of erors. So much erors, all these erors are making me sick. This is a line not containing any errors. |
Find and replace
Find pattern erors and replace with errors.
1 2 3 4 5 6 7 |
[oracle@dba15 shellscripting]$ sed 's/erors/errors/' file.txt This is the first line of an example text. It is a text with errors. Lots of errors. So much errors, all these erors are making me sick. This is a line not containing any errors. This is the last line. |
– only first occurrences in the line will be replaced.
To replace all the patterns in all the lines.
1 2 3 4 5 6 7 |
[oracle@dba15 shellscripting]$ sed 's/erors/errors/g' file.txt This is the first line of an example text. It is a text with errors. Lots of errors. So much errors, all these errors are making me sick. This is a line not containing any errors. This is the last line. |
To insert string at beginning of every line.
1 2 3 4 5 6 7 |
[oracle@dba15 shellscripting]$ sed 's/^/>> /' file.txt >> This is the first line of an example text. >> It is a text with erors. >> Lots of erors. >> So much erors, all these erors are making me sick. >> This is a line not containing any errors. >> This is the last line. |
To insert pattern at ending of every line.
1 2 3 4 5 6 7 |
[oracle@dba15 shellscripting]$ sed 's/$/EOL/' file.txt This is the first line of an example text.EOL It is a text with erors.EOL Lots of erors.EOL So much erors, all these erors are making me sick.EOL This is a line not containing any errors.EOL This is the last line.EOL |
2.Non interactive editing
Reading sed commands from a file
Multiple sed commands can be put in a file and executed using the -f option. When creating such a file, make sure that:
- No trailing white spaces exist at the end of lines.
- No quotes are used.
- When entering text to add or replace, all except the last line end in a backslash.
- AWK programming
Redirect the output of the sed command to the different file.
1 2 3 4 5 6 7 8 9 |
[oracle@dba15 shellscripting]$ sed 's/erors/errors/' file.txt > corrected.txt [oracle@dba15 shellscripting]$ mv corrected.txt file.txt [oracle@dba15 shellscripting]$ cat file.txt This is the first line of an example text. It is a text with errors. Lots of errors. So much errors, all these erors are making me sick. This is a line not containing any errors. This is the last line. |
In next article we are discussing about AWK.
Thank you….