2

I know how to duplicate a file multiple times using bash commands, but now I want to know how to write in each of these duplicated files a different output. So for example:

I have 1 file with 5 lines and each line contains different annotations like:

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

And I have another file which I have duplicated 5 times.

Now I want to write in each of these files 1 line from my first file, so they all have a unique annotation.

What is the easiest way to do this using bash?

Edit

So my "Another file" which is duplicated 5 times looks like this:

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

And I would want a output like:

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

Next 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..

I hope that this is a bit more clearer

  • 1
    post the content of your "another file" and the final expected output – RomanPerekhrest Dec 21 '17 at 9:07
  • "Another file" which is duplicated 5 times : 1 22178279 22178279 2 22188618 22188618 3 22211006 22211006 4 22215993 22215993 5 25718484 25718484 – Osman Altun Dec 21 '17 at 9:21
  • sorry having some problems with pasting my content – Osman Altun Dec 21 '17 at 9:29
  • Are the file names sequential like f1.txt f2.txt f3...? – Tigger Dec 21 '17 at 10:00
  • yes they are sequential like that – Osman Altun Dec 21 '17 at 11:59
4

With GNU coreutils:

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

The output files are named xaa, xab etc. by default:

$ 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
  • Thank you it work as i wanted it to be, it was very useful! – Osman Altun Dec 21 '17 at 12:43
  • With any split: {cat another; sed '$!r another' file; } | split -l6 or, if you don't know the no. of lines in another beforehand n=$(wc -l <another); { cat another; sed '$!r another' file; } | split -l$((n+1)) – don_crissti Dec 21 '17 at 12:53
  • How would i do this if i want to skip the header and give names to the files with each one another number, like file1.txt, file2.txt, file3.txt etc... – Osman Altun Dec 22 '17 at 9:18
3

Awk solution:

awk -v f2="$(cat file2)" '{ print f2 ORS $0 > "file"++c".txt" }' file1
  • file1 and file2 - are your "first" and "another" files respectively
  • f2 - variable containing the contents of the file2

Viewing results:

$ 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
2

I mushed up this script, not the most optimum but works:

#!/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

Results:

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
1

I am sure someone will come up with a single line that can do this, but here is a simple sh script.

#!/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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.