在每行的开头添加前缀字符串
我有一个如下文件:
line1
line2
line3
我想得到:
prefixline1
prefixline2
prefixline3
我可以编写一个 Ruby 脚本,但如果我不需要,那就更好了。
前缀
将包含/
。它是一个路径,例如 /opt/workdir/
。
I have a file as below:
line1
line2
line3
And I want to get:
prefixline1
prefixline2
prefixline3
I could write a Ruby script, but it is better if I do not need to.
prefix
will contain /
. It is a path, /opt/workdir/
for example.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
如果您需要在具有特定字符串的每行开头添加文本,请尝试以下操作。在下面的示例中,我在包含“rock”一词的每一行的开头添加 #。
If you need to prepend a text at the beginning of each line that has a certain string, try following. In the following example, I am adding # at the beginning of each line that has the word "rock" in it.
对于使用 BSD/OSX 系统的人来说,有一个名为
lam
的实用程序,它是“层压板”的缩写。lam -s prefix file
将执行您想要的操作。我在管道中使用它,例如:find -type f -exec lam -s "{}: " "{}" \; | fzf
...它将找到所有文件,对每个文件执行 lam ,为每个文件提供一个自己的文件名前缀。 (并将输出泵送到 fzf 进行搜索。)
For people on BSD/OSX systems there's utility called
lam
, short for laminate.lam -s prefix file
will do what you want. I use it in pipelines, eg:find -type f -exec lam -s "{}: " "{}" \; | fzf
...which will find all files, exec lam on each of them, giving each file a prefix of its own filename. (And pump the output to fzf for searching.)
另一种选择(
海绵
是可选的):示例:
Another alternative (
sponge
is optional):Example:
如果
prefix
包含/
,您可以使用prefix
之外的任何其他字符,或者对
/
进行转义,因此sed
命令变为If
prefix
contains/
, you can use any other character not inprefix
, orescape the
/
, so thesed
command becomes在 awk 中,默认操作是
'{print $0}'
(即打印整行) ,所以上面相当于:使用 Perl(就地替换):
In awk the default action is
'{print $0}'
(i.e. print the whole line), so the above is equivalent to:With Perl (in place replacement):
您可以在 Ex 模式下使用 Vim:
%
选择所有行s
替换< /p>x
保存并关闭You can use Vim in Ex mode:
%
select all liness
replacex
save and close如果你的前缀有点复杂,只需将它放在一个变量中:
然后,传递该变量并让 awk 处理它:
If your prefix is a bit complicated, just put it in a variable:
Then, you pass that variable and let awk deal with it:
这是使用
ts
来自 moreutils 的命令以及它是如何逐步派生的:
请注意,前缀将与内容以空格分隔
Here is a oneliner solution using the
ts
command from moreutilsAnd how it's derived step by step:
Note that the prefix will be space separated from the content
如果你有 Perl:
If you have Perl:
使用 & (与模式匹配的输入的整个部分”):
或使用反向引用:
Using & (the whole part of the input that was matched by the pattern”):
OR using back references:
使用外壳:
Using the shell:
虽然我不认为 pierr 有这个顾虑,但我需要一个不会延迟文件实时“尾部”输出的解决方案,因为我想同时监视多个警报日志,并在每一行前面加上各自日志的名称作为前缀。
不幸的是,sed、cut 等引入了太多缓冲,使我无法看到最新的行。 Steven Penny 关于使用
nl
的-s
选项的建议很有趣,测试证明它没有引入令我担心的不需要的缓冲。不过,使用 nl 存在一些问题,这些问题与删除不需要的行号的愿望有关(即使您不关心它的美观,也可能在某些情况下使用额外的列是不可取的)。首先,使用“cut”删除数字会重新引入缓冲问题,因此破坏了解决方案。其次,使用“-w1”没有帮助,因为这不会将行号限制为单列 - 它只是随着需要更多数字而变得更宽。
如果您想在其他地方捕获它,那就不太漂亮了,但由于这正是我不需要做的(所有内容都已写入日志文件,我只想实时观看几个),所以最好丢失行号并且只有我的前缀的方法是用回车符(CR或^M或Ctrl-M)开始
-s
字符串。例如:While I don't think pierr had this concern, I needed a solution that would not delay output from the live "tail" of a file, since I wanted to monitor several alert logs simultaneously, prefixing each line with the name of its respective log.
Unfortunately, sed, cut, etc. introduced too much buffering and kept me from seeing the most current lines. Steven Penny's suggestion to use the
-s
option ofnl
was intriguing, and testing proved that it did not introduce the unwanted buffering that concerned me.There were a couple of problems with using
nl
, though, related to the desire to strip out the unwanted line numbers (even if you don't care about the aesthetics of it, there may be cases where using the extra columns would be undesirable). First, using "cut" to strip out the numbers re-introduces the buffering problem, so it wrecks the solution. Second, using "-w1" doesn't help, since this does NOT restrict the line number to a single column - it just gets wider as more digits are needed.It isn't pretty if you want to capture this elsewhere, but since that's exactly what I didn't need to do (everything was being written to log files already, I just wanted to watch several at once in real time), the best way to lose the line numbers and have only my prefix was to start the
-s
string with a carriage return (CR or ^M or Ctrl-M). So for example:使用 ed:
对于每一行 (
,
),该行的开头 (^
) 都替换为前缀
。wq
保存并退出。如果替换字符串包含斜杠,我们可以使用不同的分隔符来代替
s
:我引用了here-doc分隔符
EOE
(“end of ed”)来防止参数扩展。在此示例中,它也可以不加引号地工作,但如果您的 ed 脚本中有$
,那么最好的做法是防止出现意外。Using ed:
This substitutes, for each line (
,
), the beginning of the line (^
) withprefix
.wq
saves and exits.If the replacement string contains a slash, we can use a different delimiter for
s
instead:I've quoted the here-doc delimiter
EOE
("end of ed") to prevent parameter expansion. In this example, it would work unquoted as well, but it's good practice to prevent surprises if you ever have a$
in your ed script.这是使用此答案中的
sed
方法的包装示例:prefix_lines
Here's a wrapped up example using the
sed
approach from this answer:prefix_lines
您还可以使用反向引用技术来实现这一点
您也可以像这样与 awk 一起使用
You can also achieve this using the backreference technique
You can also use with awk like this
使用 Pythonize (
pz
):Using Pythonize (
pz
):您可以使用 AWK
或
对于后缀:
awk '{print $0"suffix"}'
对于前缀和后缀:
awk '{print "prefix"$0 “后缀”}'
You can do it using AWK
or
For suffix:
awk '{print $0"suffix"}'
For prefix and suffix:
awk '{print "prefix"$0"suffix"}'
在 bash 命令行上使用 for 循环的简单解决方案:
将输出保存到文件中:
Simple solution using a for loop on the command line with bash:
Save the output to a file: