多次复制文件并在每个文件中添加写入不同的行

多次复制文件并在每个文件中添加写入不同的行

我知道如何使用命令多次复制文件bash,但现在我想知道如何在每个复制文件中写入不同的输出。例如:

我有 1 个包含 5 行的文件,每行包含不同的注释,例如:

1    16451824   16451824
2    17322876   17322876
3    17354363   17354363
4    17355234   17355234
5    17371253   17371253

我还有另一个文件,我已经复制了 5 次。

现在我想在每个文件中写入第一个文件中的 1 行,因此它们都有唯一的注释。

使用 来执行此操作的最简单方法是什么bash

编辑

所以我的“另一个文件”重复了 5 次,如下所示:

1 40780020  40780020
2 41223003  41223003
3 43218205  43218205
4 43395462  43395462
5 43803907  43803907

我想要一个像这样的输出:

1 40780020  40780020
2 41223003  41223003
3 43218205  43218205
4 43395462  43395462
5 43803907  43803907
1 16451824  16451824  <-- first line from first file

下一个文件...

1 40780020  40780020
2 41223003  41223003
3 43218205  43218205
4 43395462  43395462
5 43803907  43803907
2 17322876  17322876 <-- second line from my first file

ETC..

我希望这更清楚一些

答案1

使用 GNU coreutils

split -l1 file --filter='cat another - > "$FILE"'

输出文件默认命名xaaxab等:

$ head xa?
==> xaa <==
1 40780020  40780020
2 41223003  41223003
3 43218205  43218205
4 43395462  43395462
5 43803907  43803907
1    16451824   16451824

==> xab <==
1 40780020  40780020
2 41223003  41223003
3 43218205  43218205
4 43395462  43395462
5 43803907  43803907
2    17322876   17322876

==> xac <==
1 40780020  40780020
2 41223003  41223003
3 43218205  43218205
4 43395462  43395462
5 43803907  43803907
3    17354363   17354363

==> xad <==
1 40780020  40780020
2 41223003  41223003
3 43218205  43218205
4 43395462  43395462
5 43803907  43803907
4    17355234   17355234

==> xae <==
1 40780020  40780020
2 41223003  41223003
3 43218205  43218205
4 43395462  43395462
5 43803907  43803907
5    17371253   17371253

答案2

Awk解决方案:

awk -v f2="$(cat file2)" '{ print f2 ORS $0 > "file"++c".txt" }' file1
  • file1file2- 分别是您的“第一个”和“另一个”文件
  • f2- 包含内容的变量file2

查看结果:

$ head file[0-9]*.txt
==> file1.txt <==
1 40780020  40780020
2 41223003  41223003
3 43218205  43218205
4 43395462  43395462
5 43803907  43803907
1 16451824  16451824

==> file2.txt <==
1 40780020  40780020
2 41223003  41223003
3 43218205  43218205
4 43395462  43395462
5 43803907  43803907
2 17322876  17322876

==> file3.txt <==
1 40780020  40780020
2 41223003  41223003
3 43218205  43218205
4 43395462  43395462
5 43803907  43803907
3 17354363  17354363

==> file4.txt <==
1 40780020  40780020
2 41223003  41223003
3 43218205  43218205
4 43395462  43395462
5 43803907  43803907
4 17355234  17355234

==> file5.txt <==
1 40780020  40780020
2 41223003  41223003
3 43218205  43218205
4 43395462  43395462
5 43803907  43803907
5 17371253  17371253

答案3

我把这个脚本搞乱了,不是最优化的,但是有效:

#!/usr/bin/env bash

# Create an array of the lines in file f1
# create a counter
count=0
#declare an array
declare -a m

while IFS=$'\n' read -r data
do
    # Add each line to the m array
    m["$count"]="$data"
    # increase the count
    count=$((count+1))

done < "$1"    

# Declare another array to hold the duplicate files
# locations
declare -a c
c=(/path/to/duplicate/files/folder/*)
# Create another counter
f=0

for ((i=0; i<"${#c[@]}"; i++))
do
        # inject lines into the duplicate files
        echo "${m[$f]}" >> "${c[$i]}"
        f=$((f+1))
done

f1:

1    16451824   16451824
2    17322876   17322876
3    17354363   17354363
4    17355234   17355234
5    17371253   17371253

结果:

f2

1 40780020  40780020
2 41223003  41223003
3 43218205  43218205
4 43395462  43395462
5 43803907  43803907
1    16451824   16451824

f3

1 40780020  40780020
2 41223003  41223003
3 43218205  43218205
4 43395462  43395462
5 43803907  43803907
2    17322876   17322876

f4

1 40780020  40780020
2 41223003  41223003
3 43218205  43218205
4 43395462  43395462
5 43803907  43803907
3    17354363   17354363

f5

1 40780020  40780020
2 41223003  41223003
3 43218205  43218205
4 43395462  43395462
5 43803907  43803907
4    17355234   17355234

f6

1 40780020  40780020
2 41223003  41223003
3 43218205  43218205
4 43395462  43395462
5 43803907  43803907
5    17371253   17371253

答案4

我确信有人会想出一行代码可以做到这一点,但这里有一个简单的 sh 脚本。

#!/bin/sh
# This script assumes the files that will have the line
# added are named f1.txt f2.txt f3.txt f4.txt f5.txt
#
# The data file contains the 5 lines that will be 
# copied to the 5 files.

# The "per line" data file
DATA=`cat f0.txt`

# The number of files that need to have a line added
F_LIST="1 2 3 4 5"

# NOTE: No error checking!!!
IFS="
"
for l in $DATA
do
    f=${F_LIST%% *}
    echo "$l" >> "f${f}.txt"
    F_LIST=${F_LIST#* }
done
exit

相关内容