自动化和遍历批处理脚本

时间:2020-08-25 23:14:08

标签: linux bash automation batch-processing

我是新来的一批。我想遍历列表,并使用输出内容替换另一个文件中的字符串。

ls -l somefile | grep .txt | awk 'print $4}' | while read file
do
  toreplace="/Team/$file"
  sed 's/dataFile/"$toreplace"/$file/ file2 > /tmp/test.txt
done

运行代码时出现错误

 sed: 1: "s/dataFile/"$torepla ...": bad flag in substitute command: '$' 

具有文件路径列表的某文件示例

foo/name/xxx/2020-01-01.txt
foo/name/xxx/2020-01-02.txt
foo/name/xxx/2020-01-03.txt

但是,我想要的输出是使用somefile目录中的文件路径列表替换另一个file2内容中的字符串。像这样:

This is the directory of locations where data from /Team/foo/name/xxx/2020-01-01.txt ............

2 个答案:

答案 0 :(得分:1)

我不确定我是否理解您想要的结果,但是希望这可以帮助您解决问题:

您的目录中有三个文件:

TEAM/foo/name/xxx/2020-01-02.txt
TEAM/foo/name/xxx/2020-01-03.txt
TEAM/foo/name/xxx/2020-01-01.txt

您还有另一个名为to_be_changed.txt的文件,其中包含文本This is the directory of locations where data from TO_BE_REPLACED ............,并且您想获取三个文件的文件名并将其插入到to_be_changed.txt文件中,就可以了与:

while read file
do
  filename="$file"
  sed "s/TO_BE_REPLACED/${filename##*/}/g" to_be_changed.txt >> changed.txt
done < <(find ./TEAM/ -name "*.txt")

然后您将创建一个名为changed.txt的文件,其中包含:

This is the directory of locations where data from 2020-01-02.txt ............
This is the directory of locations where data from 2020-01-03.txt ............
This is the directory of locations where data from 2020-01-01.txt ............

这是您要达到的目标吗?如果您需要进一步说明,我们很乐意编辑此答案以提供更多详细信息/说明。

答案 1 :(得分:0)

Type token = new TypeToken<List<Immobile>>() {}.getType();
List<Immobile> immobileList = new Gson().fromJson(response, token);

不。不,不,诺诺。

ls -l somefile | grep .txt | awk 'print $4}' | while read file 仅显示ls -l somefile,除非它是目录。
(不要将目录命名为“ somefile”。)
如果您的意思是somefile,请在您的帖子中进行说明。

somefile.txt将翻阅为三个字符grep .txt所显示的行,前面三个字符都包含任何字符(点是正则表达式通配符)。由于您要求提供txt的长列表,因此找不到该列表,因此请勿传递任何内容。

somefile是不会编译的错字。 awk 'print $4}'将崩溃。

保持简单。我怀疑你的意思是

awk

然后进入

for file in *.txt

您对toreplace="/Team/$file" sed 's/dataFile/"$toreplace"/$file/ file2 > /tmp/test.txt 的期望并不乐观-$fileawk买入的4美元似乎不太可能。
假设它是上面ls -l的文件名,然后尝试

for

有帮助吗?根据需要纠正我。对不起,如果我看起来很苛刻。
欢迎来到SO。 ;)