如果缺少值,我想在文本文件中插入新行。例如,我有以下文本文件 (A.txt),缺少第 5 行。此外,由于文件应该有 12 行,因此第 11-12 行也丢失了。
1 2.30
2 3.01
3 3.22
4 3.34
6 3.01
7 2.90
8 2.99
9 3.00
10 3.02
Run Code Online (Sandbox Code Playgroud)
我的预期输出如下。对于缺失的情况,应添加一行,其中包含编号和 NA。如您所见,这在第 5、11 和 12 行按预期发生:
1 2.30
2 3.01
3 3.22
4 3.34
5 NA
6 3.01
7 2.90
8 2.99
9 3.00
10 3.02
11 NA
12 NA
Run Code Online (Sandbox Code Playgroud)
我可以通过使用以下脚本来做到这一点:
f1=/my-directory/
echo "new file" > "$f1"/newfile.txt
for i in {1..12}; do
l=$(awk '{print $1}' /"$f1"/A.txt | grep -wE ^$i /"$f1"/A.txt)
if grep --quiet …
Run Code Online (Sandbox Code Playgroud) 我想对树中 6 个目录下的文件执行一些处理(例如复制)。每个目录级别的目录名称完全不规则(随机数字和字母),在最后一级,我的文件所在的目录大约有 20 个。
一个案例的例子:
cp /000157/DZW123/AHG345/DFR987/000RE7/0025RTZ/file.xxx /destination/file.xxx
Run Code Online (Sandbox Code Playgroud)
具有不同最后级别的相同案例(在其他 19 个中):
cp /000157/DZW123/AHG345/DFR987/000RE7/1298FGT/file.xxx /destination/file.xxx
Run Code Online (Sandbox Code Playgroud)
但在许多其他情况下完全不同:
cp /001154/CVS456/SAQ452/FRO921/000VG5/0032RRT/file.xxx /destination/file.xxx
Run Code Online (Sandbox Code Playgroud)
由于情况不同,循环无济于事。如果有一个解决方案允许我为每个分支直接在树中向下移动 6 个目录(不管命名如何),那似乎最好。我试过 cd +n 但这不起作用。