在文件中找到一行并在 bash 中的行尾添加一些内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18877229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:37:05  来源:igfitidea点击:

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

linuxbashfile-iosed

提问by Josh

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

假设我想在主机文件中找到特定行,要么通过开头的模式可能是一个 ip 地址,要么通过该行上方的注释

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

当您找到我告诉您的 IP 时,我如何告诉 bash。去 ro 行结束并添加一些东西

or the other case

或者其他情况

When bash finds the comment #This is added automatically

当 bash 找到评论时 #This is added automatically

go down by 2 and than go to the end of the line and add something

下降 2 然后到行尾并添加一些东西

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?

你看我是一个初学者,没有任何想法在这里使用什么以及如何使用。dis 是由 sed 完成的吗?或者这可以用grep完成吗?我必须为那些东西学习 AWK 吗?

采纳答案by Malcolm Jones

Given the following:

鉴于以下情况:

Textfile:

文本文件:

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

127.0.0.1 localhost linux 
[root@yourserver ~]# 

bash script:

bash脚本:

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

_IP_TO_FIND=""

# 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.

只需输入您要查找的 IP 地址,该脚本就会在第一次出现时匹配。

Hope this helps!

希望这可以帮助!

回答by iamauser

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

这将使用模式“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.1via sed :

以下将添加到文件中以127.0.0.1via 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

awk '/^127.0.0.1/{print 
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 
sed -i.bak 's/^192\.168\.1\.2.*$/& ADDED/' hosts 
,"ADD MORE TEXT"}' file > newfile && mv newfile file
,"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 :

如果您想通过变量调用 IP 地址,则语法可能会有所不同:

##代码##

回答by anubhava

This inline sed should work:

这个内联 sed 应该可以工作:

##代码##
  1. This sed command find lines that start with 192.168.1.2
  2. If found it adds ADDEDat the end of those lines
  1. 此 sed 命令查找以开头的行 192.168.1.2
  2. 如果找到它会ADDED在这些行的末尾添加