Find a line in a file and add something to the end

2020-06-23 05:23发布

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?

3条回答
做个烂人
2楼-- · 2020-06-23 06:01

This inline sed should work:

sed -i.bak 's/^192\.168\.1\.2.*$/& ADDED/' hosts 
  1. This sed command find lines that start with 192.168.1.2
  2. If found it adds ADDED at the end of those lines
查看更多
女痞
3楼-- · 2020-06-23 06:02

This will add some text to the end of the line with a pattern "127.0.0.1".

grep -F "127.0.0.1" file | sed -ie 's/$/& ADDing SOME MORE TEXT AT THE END/g'

Following will add to the line in the file that begins with 127.0.0.1 via sed :

sed -ie 's/^127.0.0.1.*$/& ADDing MORE TEXT TO THE END/g' file

TO do the same, you can also use awk :

awk '/^127.0.0.1/{print $0,"ADD MORE TEXT"}' file > newfile && mv newfile file
  • EDIT

If you want to call the IP address through a variable then syntax might be a bit different :

var="127.0.0.1"
grep -F "$var" file | sed -ie 's/$/& ADD MORE TEXT/g'
sed -ie "s/^$var.*$/& ADD MORE TEXT/g" file
awk '/^'$var'/{print $0,"ADD MORE TEXT"}' file > newfile && mv newfile file
查看更多
Viruses.
4楼-- · 2020-06-23 06:12

Given the following:

Textfile:

[root@yourserver ~]# cat text.log 
#This is your hosts file

127.0.0.1 localhost linux 
[root@yourserver ~]# 

bash script:

[root@yourserver ~]# cat so.sh 
#!/bin/bash

_IP_TO_FIND="$1"

# sysadmin 101 - the sed command below will backup the file just in case you need to revert

_BEST_PATH_LINE_NUMBER=$(grep -n "${_IP_TO_FIND}" text.log | head -1 | cut -d: -f1)
_LINE_TO_EDIT=$(($_BEST_PATH_LINE_NUMBER+2))
_TOTAL_LINES=$( wc -l text.log)
if [[ $_LINE_TO_EDIT -gte $_TOTAL_LINES ]]; then
   # if the line we want to add this to is greater than the size of the file, append it
  sed -i .bak "a\${_LINE_TO_EDIT}i#This is added automatically\n\n192.168.1.2 domain1. com" text.log
else
  # else insert it directly 
  sed -i .bak "${_LINE_TO_EDIT}i\#This is added automatically\n\n192.168.1.2 domain1. com" text.log
fi

Usage:

bash ./so.sh 127.0.0.1

Simply enter in the ip address you're trying to find and this script matches on the first occurrence of it.

Hope this helps!

查看更多
登录 后发表回答