Monday, 15 July 2013

Append, Insert, Replace and Count File Lines into Linux using sed scripting.


Sometime Linux admin has to append, replace and insert the lines into Linux files without using editor. sed is strong scripting utility, through which we can perform these action using command line options. Here we will discuss one by one all option of append, replace, insert and count the file lines.

First of all we create a sample file which can be altered using sed command for file lines actions.

[jitendrakumar@paragtesting ~]$ cat jitendrakumaryogi.txt
This is first line.
My Name is Jitendra Kumar.
I am a Software Engineer.
Oracle is a database Software.
Tomcat and Jboss are application servers.
We love India.
India is a strong nation.
--------Hello------------------
Server1
Server2
Server3
Server4
____________END________________

1.      Append Lines Using Sed.

We use option “a” with sed command to append a line after every line with the address or pattern.

SYNTAX:
#sed ‘Address a\
                        Line which need to be appended’ filename
#sed ‘/PATTERN/ a\
                        Line which need to be appended’ filename
Example1:

Add a line “Software and Development” after 4th line. Sed “a” option will insert line after 4th line.

[jitendrakumar@paragtesting ~]$ sed '4 a\
> Software and Development' jitendrakumaryogi.txt
This is first line.
My Name is Jitendra Kumar.
I am a Software Engineer.
Oracle is a database Software.
Software and Development
Tomcat and Jboss are application servers.
We love India.
India is a strong nation.
--------Hello------------------
Server1
Server2
Server3
Server4
____________END________________

Example2:

Add a line “Shell Scripting” after pattern “Jitendra Kumar”.

[jitendrakumar@paragtesting ~]$ sed '/Jitendra Kumar/a \
Shell Scripting' jitendrakumaryogi.txt
This is first line.
My Name is Jitendra Kumar.
Shell Scripting
I am a Software Engineer.
Oracle is a database Software.
Tomcat and Jboss are application servers.
We love India.
India is a strong nation.
--------Hello------------------
Server1
Server2
Server3
Server4
____________END________________

Example3: Append a line after the End of file.

The following example will add “Testing” after end of line.
[jitendrakumar@paragtesting ~]$ sed '$ a\
> Testing' jitendrakumaryogi.txt
This is first line.
My Name is Jitendra Kumar.
I am a Software Engineer.
Oracle is a database Software.
Tomcat and Jboss are application servers.
We love India.
India is a strong nation.
--------Hello------------------
Server1
Server2
Server3
Server4
____________END____________

Testing

2.      Insert the Line using Sed command.

Sed command used “I” option for inserting line before particular line or pattern.

SYNTAX:
#sed ‘Address i\
                        New Line’ filename
#sed ‘/PATTERN/i \
                        New Line’ filename

Example1: Insert “Implementation of Script” before line 6.

[jitendrakumar@paragtesting ~]$ sed '6 i\
Implementation of Script' jitendrakumaryogi.txt
This is first line.
My Name is Jitendra Kumar.
I am a Software Engineer.
Oracle is a database Software.
Tomcat and Jboss are application servers.
Implementation of Script
We love India.
India is a strong nation.
--------Hello------------------
Server1
Server2
Server3
Server4
____________END____________

Example2:  Insert “Deployment of Application” before pattern “database”.

[jitendrakumar@paragtesting ~]$ sed '/database/i\
Deployment of Application' jitendrakumaryogi.txt
This is first line.
My Name is Jitendra Kumar.
I am a Software Engineer.
Deployment of Application
Oracle is a database Software.
Tomcat and Jboss are application servers.
We love India.
India is a strong nation.
--------Hello------------------
Server1
Server2
Server3
Server4
____________END____________


3.      Replace line using sed command.

“c” command in sed used to replace every line matches with the pattern or ranges with the new given line.

SYNTAX:
#sed ‘Address c\
                        Replace line’ filename
#sed ‘/PATTERN/c \
                        Replace Line’ filename


Example1: Replace a first line of file.

[jitendrakumar@paragtesting ~]$ sed '1 c \
First Line' jitendrakumaryogi.txt
First Line
My Name is Jitendra Kumar.
I am a Software Engineer.
Oracle is a database Software.
Tomcat and Jboss are application servers.
We love India.
India is a strong nation.
--------Hello------------------
Server1
Server2
Server3
Server4
____________END____________

Example2: Replace a line which matches patteen.

Replace the line which contain pattern “Server1” to “It is nice to have it.”

[jitendrakumar@paragtesting ~]$ sed '/Server1/c \
It is nice to have it' jitendrakumaryogi.txt
This is first line.
My Name is Jitendra Kumar.
I am a Software Engineer.
Oracle is a database Software.
Tomcat and Jboss are application servers.
We love India.
India is a strong nation.
--------Hello------------------
It is nice to have it
Server2
Server3
Server4
____________END____________

4.      Print Line Number using sed command.

“=” is a command to print the current line number to standard output.

SYNTAX:

#sed ‘=’ filename

The above send command syntax prints line number in the first line and the original line from the file in the next line.
SYNTAX:
Sed –n ‘/PATTERN/,/PATTERN/ {
=
P
}’ filename

Example1:
Print the line numbers for the lines matches from the pattern “Software” to “Server”

[jitendrakumar@paragtesting ~]$ sed -n '/Software/,/Server/ {
> =
> p
> }' jitendrakumaryogi.txt
3
I am a Software Engineer.
4
Oracle is a database Software.
5
Tomcat and Jboss are application servers.
6
We love India.
7
India is a strong nation.
8
--------Hello------------------
9
Server1

sed ‘=’ command accepts only one address, so if you want to print line number for a range of lines, you must use the curly braces
Example 2: Print the total number of lines in a file
[jitendrakumar@paragtesting ~]$ sed -n '$=' jitendrakumaryogi.txt

14

No comments:

Post a Comment