在每行的开头添加前缀字符串

pie*_*fou 310 linux scripting text-processing

我有一个文件如下:

line1
line2
line3
Run Code Online (Sandbox Code Playgroud)

我想得到:

prefixline1
prefixline2
prefixline3
Run Code Online (Sandbox Code Playgroud)

我可以写一个Ruby脚本,但如果我不需要它会更好.

prefix将包含/./opt/workdir/例如,这是一条道路.

Alo*_*hal 488

# If you want to edit the file in-place
sed -i -e 's/^/prefix/' file

# If you want to create a new file
sed -e 's/^/prefix/' file > file.new
Run Code Online (Sandbox Code Playgroud)

如果prefix包含/,则可以使用不在其中的任何其他字符prefix,或者转义/,以便sed命令变为

's#^#/opt/workdir#'
# or
's/^/\/opt\/workdir/'
Run Code Online (Sandbox Code Playgroud)

  • 如果要在每行末尾添加字符串,请使用`sed -e'/ $/postfix /'file`. (8认同)
  • 不要忘记你也可以在管道中使用`sed`,例如`foo | sed -e 's/^/x /' | 酒吧`。 (2认同)

Vij*_*jay 119

awk '$0="prefix"$0' file > new_file
Run Code Online (Sandbox Code Playgroud)

  • 使用管道/流或变量:`prtinf"$ VARIABLE \n"| awk'$ 0 ="前缀"$ 0'` (8认同)
  • 对于大文件(12 G),`awk`报告`awk:outreadrec 1 source line number 1`的内存不足,但是使用sed的解决方案已成功完成。 (2认同)

小智 31

您可以在Ex模式下使用Vim:

ex -sc '%s/^/prefix/|x' file
Run Code Online (Sandbox Code Playgroud)
  1. % 选择所有行

  2. s 更换

  3. x 保存并关闭


Mel*_*lka 21

如果您的前缀有点复杂,只需将其放在变量中:

prefix=path/to/file/
Run Code Online (Sandbox Code Playgroud)

然后,你传递该变量并让awk处理它:

awk -v prefix="$prefix" '{print prefix $0}' input_file.txt
Run Code Online (Sandbox Code Playgroud)


Maj*_*imi 9

如果你有Perl:

perl -pe 's/^/PREFIX/' input.file
Run Code Online (Sandbox Code Playgroud)


nav*_*aid 7

这是一个使用tsmoreutils 命令的高度可读的 oneliner 解决方案

$ cat file | ts prefix | tr -d ' '
Run Code Online (Sandbox Code Playgroud)

以及它是如何逐步推导出来的:

# Step 0. create the file

$ cat file
line1
line2
line3
Run Code Online (Sandbox Code Playgroud)
# Step 1. add prefix to the beginning of each line

$ cat file | ts prefix
prefix line1
prefix line2
prefix line3
Run Code Online (Sandbox Code Playgroud)
# Step 2. remove spaces in the middle

$ cat file | ts prefix | tr -d ' '
prefixline1
prefixline2
prefixline3
Run Code Online (Sandbox Code Playgroud)

  • 许多 Linux 发行版上默认不安装“ts”。另外,投票是因为此答案中的尾随“tr -d ' '”将删除行中的所有空格,而不仅仅是“ts”添加的空格 (5认同)

小智 7

使用 &(与模式匹配的整个输入部分”):

cat in.txt | sed -e "s/.*/prefix&/" > out.txt
Run Code Online (Sandbox Code Playgroud)

或使用反向引用:

cat in.txt | sed -e "s/\(.*\)/prefix\1/" > out.txt
Run Code Online (Sandbox Code Playgroud)


gho*_*g74 5

使用外壳:

#!/bin/bash
prefix="something"
file="file"
while read -r line
do
 echo "${prefix}$line"
done <$file > newfile
mv newfile $file
Run Code Online (Sandbox Code Playgroud)


小智 5

虽然我不认为 pierr 有这个问题,但我需要一个不会延迟从文件的实时“尾部”输出的解决方案,因为我想同时监视多个警报日志,在每一行前面加上各自日志的名称.

不幸的是,sed、cut 等引入了过多的缓冲,使我无法看到最新的行。Steven Penny 的使用-s选项的建议nl很有趣,测试证明它没有引入我所关心的不需要的缓冲。

nl但是,使用 存在一些问题,与删除不需要的行号有关(即使您不关心它的美观,在某些情况下也可能不希望使用额外的列)。首先,使用“cut”去除数字重新引入了缓冲问题,因此它破坏了解决方案。其次,使用“-w1”没有帮助,因为这不会将行号限制为单列 - 随着需要更多数字,它只会变得更宽。

如果你想在别处捕捉它并不漂亮,但因为这正是我不需要做的(一切都已经被写入日志文件,我只想实时观看几个),最好的丢失行号并且只有我的前缀的方法是-s用回车符(CR 或 ^M 或 Ctrl-M)开始字符串。例如:

#!/bin/ksh

# Monitor the widget, framas, and dweezil
# log files until the operator hits <enter>
# to end monitoring.

PGRP=$$

for LOGFILE in widget framas dweezil
do
(
    tail -f $LOGFILE 2>&1 |
    nl -s"^M${LOGFILE}>  "
) &
sleep 1
done

read KILLEM

kill -- -${PGRP}
Run Code Online (Sandbox Code Playgroud)

  • 对 sed 使用 `-u` 选项以避免缓冲。 (2认同)
  • 可以使用 unbuffer/stdbuf 关闭缓冲,参见 http://unix.stackexchange.com/q/25372/6205 (2认同)