bash Bash如何将单词附加到行尾?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14835977/
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-09 23:19:47  来源:igfitidea点击:

Bash how to append word to end of a line?

bashunixsedawkgrep

提问by Palace Chan

I have executed a command in bash to retrieve some addresses from a file like this:

我在 bash 中执行了一个命令来从这样的文件中检索一些地址:

grep address file.txt | cut -d'=' -f2 | tr ':' ' '

yields:

产量:

xxx.xx.xx.xxx port1
xxx.xx.xx.xxx port2

and I would like to append ' eth0' to each of those output lines and then ideally for loop over the result to call a command with each line. Problem I'm having is getting that extra string in the end to each line. I tried:

我想将“ eth0”附加到每个输出行,然后理想地循环结果以对每一行调用命令。我遇到的问题是在每一行的最后得到那个额外的字符串。我试过:

| sed -e 's/\(.+)\n/ eth0/g'

which didn't work..and then supposing I got it there, if I wrap it in a for loop it won't pass in the full lines since they contain spaces. So how do I go about this?

这不起作用..然后假设我得到了它,如果我将它包装在 for 循环中,它不会传递整行,因为它们包含空格。那么我该怎么做呢?

回答by FatalError

You can match $to append to a line, like:

您可以匹配$以附加到一行,例如:

sed -e 's/$/ eth0/'

EDIT:

编辑:

To loop over the lines, I'd suggest using a whileloop, like:

要遍历这些行,我建议使用while循环,例如:

while read line
do
  # Do your thing with $line
done < <(grep address file.txt | cut -d'=' -f2 | tr ':' ' ' | sed -e 's/$/ eth0')

回答by Chris Seymour

How about just using awk:

如何只使用awk

awk -F= '/address/{gsub(/:/," ");print ,"eth0"}' file

Demo:

演示:

$ cat file
junk line
address=192.168.0.12:80
address=127.0.0.1:25
don not match this line

$ awk -F= '/address/{gsub(/:/," ");print ,"eth0"}' file
192.168.0.12 80 eth0
127.0.0.1 25 eth0

Or just with sed:

或者只是使用sed

$ sed -n '/address/{s/:/ /g;s/.*=//;s/$/ eth0/p}' file
192.168.0.12 80 eth0
127.0.0.1 80 eth0

回答by Ed Morton

All you need is:

所有你需要的是:

awk -F'[=:]' '{print , , "eth0"}' file.txt |
while IFS= read -r ip port eth
do
   printf "ip=%s, port=%s, eth=%s\n" "$ip" "$port" "$eth"
done

Always use IFS= and -r when using read unless you have a very specific reason not to. google for why.

使用 read 时总是使用 IFS= 和 -r ,除非您有非常具体的理由不这样做。谷歌为什么。

回答by pedram bashiri

typeset TMP_FILE=$( mktemp )
touch "${TMP_FILE}"
cp -p filename "${TMP_FILE}"
sed -e 's/$/stringToAdd/' "${TMP_FILE}" > filename

回答by Calvin Taylor

I came here looking for the same answer, but none of the above do it as clean as

我来到这里寻找相同的答案,但上面没有一个像

sed -i 's/address=.*/& eth0/g' file

Search and replace inline with sed for lines begining with address, replace with the same line plus 'eth0'

搜索并用 sed 内联替换以地址开头的行,替换为同一行加上 'eth0'

eg.

例如。

sed -i 's/address=.*/& eth0/g' file; cat file
junk line
address=192.168.0.12:80 eth0
address=127.0.0.1:25 eth0
don not match this line

回答by Kent

is this ok for you?

这对你好吗?

kent$ echo "xxx.xx.xx.xxx port1
xxx.xx.xx.xxx port2"|sed 's/.*/& eth0/'
xxx.xx.xx.xxx port1 eth0
xxx.xx.xx.xxx port2 eth0

P.S you could merge your cut, tr (even grep in your example) into one sed/awk call, to make the cmdline simpler and faster.

PS,您可以将您的剪辑、tr(甚至在您的示例中为 grep)合并到一个 sed/awk 调用中,以使 cmdline 更简单、更快。