bash - 在行末添加 sed

标签 bash sed

给定一个包含几行的纯文本文档,例如:

c48 7.587 7.39
c49 7.508 7.345983
c50 5.8 7.543
c51 8.37454546 7.34

我需要在行尾后添加一些信息 2 个空格,所以对于每一行我会得到:

c48 7.587 7.39  def
c49 7.508 7.345983  def
c50 5.8 7.543  def
c51 8.37454546 7.34  def

我需要为数千个文件执行此操作。我想这可能与 sed 有关,但不知道该怎么做。有什么提示吗?您能否也给我一些有关此案例的教程或表格的链接?

谢谢

最佳答案

如果你所有的文件都在一个目录下

sed -i.bak 's/$/  def/' *.txt

递归执行(GNU 查找)

find /path -type f -iname '*.txt' -exec sed -i.bak 's/$/  def/' "{}" +;

可以看到here介绍sed

您可以使用的其他方式,

你好

for file in *
do
  awk '{print $0" def"}' $file >temp
  mv temp "$file"
done 

bash 壳

for file in *
do
  while read -r line
  do
      echo "$line def"
  done < $file >temp
  mv temp $file
done

关于bash - 在行末添加 sed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2516343/

相关文章:

python - 如何从 bash 中的 python 脚本的一组不同输入文件中输出多个文件

linux - 正则表达式: Search a word and append a new word at the end of the line

bash - 在 bash 中如何替换字符串变量,并将其设置为 sed 命令

linux - 使用 bash 在文件中的模式后面插入一行

Ruby 到 bash 脚本的翻译

regex - Bash - Sed/Awk 用新行替换字符串

shell - 选择一个模式可能出现两次的标记模式之间的线

linux - 使用 Sed 过滤器为变量分配路径

linux - 登录后如何显示消息

bash - AWK 如何执行和读取记录和字段