Wednesday, April 24, 2013


How to provide expiry date to user                                         How to change login shell of user

We will go through the examples of Sed substitution using regular expression as follows. Sed command in unix examples, we have given as follows. For all examples, we will consider one file as an input and get desired output from that file. i.e. employee.txt

root@rhel:~/test# cat employee.txt
1001,Ajay,Manager,25000
101,Satish,Founder,30000
302,Atul,CEO,26000
434,Raj,Senior Manager, 26000
1231,Kalyan,Human Resource,20000
root@rhel:~/test#

Replace the last five characters in every line of file

In this example, we are replacing last five characters of each line by “Salary not decided” phrase.

root@rhel:~/test# cat employee.txt
1001,Ajay,Manager,25000
101,Satish,Founder,30000
302,Atul,CEO,26000
434,Raj,Senior Manager, 26000
1231,Kalyan,Human Resource,20000
root@rhel:~/test# sed 's/.....$/Salary not decided/g' employee.txt
1001,Ajay,Manager,Salary not decided
101,Satish,Founder,Salary not decided
302,Atul,CEO,Salary not decided
434,Raj,Senior Manager, Salary not decided
1231,Kalyan,Human Resource,Salary not decided
root@rhel:~/test#

Delete all commented lines in a file

In this example, we are deleting all commented lines of a file by sed command.

root@rhel:~/test# cat pattern.txt
1001,Ajay,Manager,Salary not decided
101,Satish,Founder,Salary not decided
302,Atul,CEO,Salary not decided
434,Raj,Senior Manager, Salary not decided
#1231,Kalyan,Human Resource,Salary not decided
#101,Satish,Founder,Salary not decided
434,Raj,Senior Manager, Salary not decided
1231,Kalyan,Human Resource,Salary not decided
root@rhel:~/test# sed 's/#.*//; /^$/d' pattern.txt
1001,Ajay,Manager,Salary not decided
101,Satish,Founder,Salary not decided
302,Atul,CEO,Salary not decided
434,Raj,Senior Manager, Salary not decided
434,Raj,Senior Manager, Salary not decided
1231,Kalyan,Human Resource,Salary not decided
root@rhel:~/test#


Recommended Article :  How to Lock User's Password by usermod command 

Strip all HTML tags from html file or delete all html tags from html file

We can remove all html tags from html file using sed command.


root@rhel:~/test# cat emp.html
<html>
<body>
<h1>
Linux Concepts and Commands….roundoverlinux.blogspot.com
</h1>
</body></html>
root@rhel:~/test#
root@rhel:~/test#
root@rhel:~/test# sed 's/<[^>]*>//g' emp.html
Linux Concepts and Commands ..roundoverlinux.blogspot.com
root@rhel:~/test#

Insert Line after the matched pattern in file

Below is the syntax for inserting line after matched pattern line in a file.

sed '[address] a the-line-to-append' input-file

root@rhel:~/test# cat employee.txt
1001,Ajay,Manager,25000
101,Satish,Founder,30000
302,Atul,CEO,26000
434,Raj,Senior Manager, 26000
1231,Kalyan,Human Resource,20000
root@rhel:~/test# sed '2 i 1034,Sagar,CFO,34000' employee.txt
1001,Ajay,Manager,25000
1034,Sagar,CFO,34000
101,Satish,Founder,30000
302,Atul,CEO,26000
434,Raj,Senior Manager, 26000
1231,Kalyan,Human Resource,20000
root@rhel:~/test#

Append line after the matched pattern containing line:

Example shows the line appended after the second line. i.e. new line will be 3rd line in file.

root@rhel:~/test# cat employee.txt
1001,Ajay,Manager,25000
101,Satish,Founder,30000
302,Atul,CEO,26000
434,Raj,Senior Manager, 26000
1231,Kalyan,Human Resource,20000
root@rhel:~/test# sed '2 a 1034,Sagar,CFO,34000' employee.txt
1001,Ajay,Manager,25000
101,Satish,Founder,30000
1034,Sagar,CFO,34000
302,Atul,CEO,26000
434,Raj,Senior Manager, 26000
1231,Kalyan,Human Resource,20000
root@rhel:~/test#

Delete the line that matches pattern and replaces it with multiple new lines
We can replace single line with multiple lines. 

Advanced Tricks of Sed command                               10 useful baisc tricks of sed command
Posted by Machindra Dharmadhikari On 4/24/2013 08:41:00 AM 1 comment

1 comment:

  1. Slots Casino Online | Jammy Casino
    Slots 과천 출장안마 Casino Online. Slot machine games 진주 출장안마 are designed 이천 출장마사지 for the player to have 영주 출장샵 fun and enjoy the thrill of the casino game. The thrill of winning money 태백 출장마사지 is

    ReplyDelete

  • RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
  • Youtube

    Chitika Ads 2

    Histat

    About

    Enter your email address:

    Delivered by FeedBurner