How can I identify a line start with a special pattern and add something to the end of the line?
if the pattern that should be added is not already appended
Let's say I'd like to find a specific line in the host file either by the pattern at the beginning may be an ip-address or by the comment that is above the line
An example may be:
#This is your hosts file
127.0.0.1 localhost linux
#This is added automatically
192.168.1.2 domain1. com
#this is added automatically to
192.168.1.2 sub.domain1.com www.domain1.com
How do I tell bash when you find the IP I tell you. go ro the lines end and add something
or the other case
When bash finds the comment #This is added automatically
go down by 2 and than go to the end of the line and add something
You see I'm a beginner and don't have any Idea what to use here and how. Is dis done by sed? or could this be done with grep? do I have to learn AWK for that stuff?
This inline sed should work:
192.168.1.2
ADDED
at the end of those linesThis will add some text to the end of the line with a pattern "127.0.0.1".
Following will add to the line in the file that begins with
127.0.0.1
via sed :TO do the same, you can also use
awk
:If you want to call the IP address through a variable then syntax might be a bit different :
Given the following:
Textfile:
bash script:
Usage:
Simply enter in the ip address you're trying to find and this script matches on the first occurrence of it.
Hope this helps!