gpt4 book ai didi

bash - 尝试使用 bash 从给定文件中获取输入文件和文本行并将其保存到其他文件

转载 作者:行者123 更新时间:2023-11-29 09:51:17 24 4
gpt4 key购买 nike

我有一个文件(我们称它为“xfile”),包含如下行

file1 <- this line goes to file1
file2 <- this goes to file2

我想做的是运行一个脚本,该脚本执行实际获取行并将它们写入文件的工作。

我手动执行此操作的方式可能如下(第一行)

(echo "this line goes to file1"; echo) >> file1

所以,为了让它自动化,这就是我尝试做的

IFS=$'\n'
for l in $(grep '[a-z]* <- .*' xfile); do
$(echo $l | sed -e 's/\([a-z]*\) <- \(.*\)/(echo "\2"; echo)\>\>\1/g')
done
unset IFS

但是我得到的是

-bash: file1(echo "this content goes to file1"; echo)>>: command not found
-bash: file2(echo "this goes to file2"; echo)>>: command not found

(在 OS X 上)

怎么了?

最佳答案

这解决了你在 Linux 上的问题

awk -F ' <- ' '{print $2 >> $1}' xfile

请注意选择字段分隔符,确保新文件没有前导或尾随空格。

在 OSX 上试一试

关于bash - 尝试使用 bash 从给定文件中获取输入文件和文本行并将其保存到其他文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41994531/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com