파일을 여러 번 복사하고 각 파일에 다른 줄을 추가하세요.

파일을 여러 번 복사하고 각 파일에 다른 줄을 추가하세요.

명령을 사용하여 파일을 여러 번 복사하는 방법을 알고 있지만 bash이제는 복사된 각 파일에 다른 출력을 작성하는 방법을 알고 싶습니다. 예를 들어:

5줄의 파일이 1개 있습니다. 각 줄에는 다른 설명이 포함되어 있습니다. 예를 들면 다음과 같습니다.

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

5번 복사한 또 다른 파일이 있습니다.

이제 각 파일의 첫 번째 파일에서 한 줄을 작성하여 모두 고유한 주석을 갖게 하려고 합니다.

이를 수행하는 가장 간단한 방법은 무엇입니까 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

등..

이것이 더 명확하길 바랍니다

답변1

GNU 사용 coreutils:

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

출력 파일의 이름은 기본적 xaa으로 지정됩니다 xab.

$ 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

관련 정보