sed: Insert character in the beginning or end of line with matched pattern

In my last articles I had shared the arguments with sed which can be used to perform case insensitive actions (search, replace..) in a file and to delete all blank lines from the file.
Before starting with some command examples let us take a file sample output where we want to perform our sed operation.
Sample File "/tmp/file" with below content

# Port rpc.statd should listen on.
STATD_PORT=662
Outgoing port statd should used. The default is port
# is random
STATD_OUTGOING_PORT=2020
Specify callout program
STATD_HA_CALLOUT="/usr/local/bin/foo"

 

Add content at the beginning of the line

Example 1
Add a "#" comment hash in the beginning of line containing "<span style="color: purple;">STATD_PORT</span>"
Solution

# sed '/STATD_PORT/ s/^/#/' /tmp/file
# Port rpc.statd should listen on.
#STATD_PORT=662
Outgoing port statd should used. The default is port
# is random
STATD_OUTGOING_PORT=2020
Specify callout program
STATD_HA_CALLOUT="/usr/local/bin/foo"

To do in place replacement

# sed -i '/STATD_PORT/ s/^/#/' /tmp/file

 
Example 2
What if the text to be matched in somewhere in the middle of the line
Here match "callout" and add a "#" comment hash in the beginning of the line
Solution

# sed '/callout/ s/^/#/' /tmp/file
# Port rpc.statd should listen on.
STATD_PORT=662
Outgoing port statd should used. The default is port
# is random
STATD_OUTGOING_PORT=2020
#Specify callout program
STATD_HA_CALLOUT="/usr/local/bin/foo"

To do in place replacement use below command

# sed -i '/callout/ s/^/#/' /tmp/file

 
Example 3
You can use the same command to add in some text or word in the beginning of the line of the matched pattern, just replace the "#" with "your text"
For eg

# sed '/callout/ s/^/your text /' /tmp/file
# Port rpc.statd should listen on.
STATD_PORT=662
Outgoing port statd should used. The default is port
# is random
STATD_OUTGOING_PORT=2020
your text Specify callout program
STATD_HA_CALLOUT="/usr/local/bin/foo"

 

Add content at the end of the line

Example 1
Add 'your text' at the end of the line which matches 'callout'

# sed '/callout/ s/$/ your text/' /tmp/file
# Port rpc.statd should listen on.
STATD_PORT=662
Outgoing port statd should used. The default is port
# is random
STATD_OUTGOING_PORT=2020
Specify callout program your text
STATD_HA_CALLOUT="/usr/local/bin/foo"

 
To do the same "in place" replacement

 # sed -i '/callout/ s/$/ your text/' /tmp/file

 
Example 2

Match any text starting with "STATD" and add "your text" at the end of all the matching lines

# sed '/^STATD/ s/$/ your text/' /tmp/file
# Port rpc.statd should listen on.
STATD_PORT=662 your text
Outgoing port statd should used. The default is port
# is random
STATD_OUTGOING_PORT=2020 your text
Specify callout program
STATD_HA_CALLOUT="/usr/local/bin/foo" your text

 
To do "in place" replacement

# sed -i '/^STATD/ s/$/ your text/' /tmp/file
IMPORTANT NOTE:

Do not use this unless you are very sure the command will not impact anything else, it is always recommended to take a backup of such file where you plan to do in place replacement

1 thought on “sed: Insert character in the beginning or end of line with matched pattern”

  1. I have file content with
    a
    ab
    abc
    abc text
    abc text1
    abc text2
    My text3 abc

    Q1: i want add semicolon in front of “text”
    Q2: i want add # with where file contents abc, if abc in of line i need bring beginning of the line
    Q3: if sed ‘/abc/s/^/#/’ filename. This command makes insert # Infront of line. but i want also instead of abc, will Abc i want same result

    Reply

Leave a Comment