4

I want to create multiple files at a time using names from a file list.txt, how can I do it?

Sample list.txt:

EOG090W002U
EOG090W00C1
EOG090W00DC
EOG090W00DE
EOG090W00E5
EOG090W00HR
EOG090W00MH
EOG090W00MS
EOG090W00PB
EOG090W00U4
EOG090W00UK
EOG090W00WM
EOG090W00WR

Suppose I have this list.txt containing some id-numbers. Now I want to make separate files using these ids as the name (e.g. EOG090W002U_M0.ctl, EOG090W00C1_M0.ctl, EOG090W00DC_M0.ctl). Also, the contents of the files need to be change accordingly. For example the content of EOG090W002U_M0.ctl, EOG090W00C1_M0.ctl file will be

seqfile = EOG090W002U_p.phy
treefile = Constant.txt
outfile = EOG090W002U_M0_mlc

or

seqfile = EOG090W00C1_p.phy
treefile = Constant.txt
outfile = EOG090W00C1_M0_mlc

where *.phy and Constant.txt will be provided in the same folder.

AdminBee
  • 22,803
  • Welcome to the site. Please edit your question to explain where you want the newly created files to be placed, if you simply want to create empty files, and whether the filename is to be amended by a suffix etc. – AdminBee Jan 23 '20 at 14:48
  • "multiple files at a time"? Please be more clear. – FedKad Jan 23 '20 at 14:49
  • 5
    Please don't change your question so much that it becomes a new question. You have now invalidated all answers. You can ask a new question instead. – jesse_b Jan 23 '20 at 15:12

5 Answers5

5

Simplest:

xargs touch <List.txt

The magic is that xargs takes every line in its stdin and adds it as an argument to the command.

xenoid
  • 8,888
3

Using GNU parallel in a script:

#!/bin/bash

constant=constant

populate_file () {
    local const=$1
    local file=$(basename -s '.M0.ctl' "$2")
    printf '%s\n%s\n%s\n' \
    "seqfile = ${file}_p.phy" \
    "treefile = ${const}.txt" \
    "outfile = ${file}_M0_mlc" > "$2"
}

export -f populate_file

parallel populate_file "$constant" {}.M0.ctl :::: list.txt

This will read lines from list.txt and execute the populate_file function against each one in parallel. The populate_file function will output the three lines in the desired format into each file.

In the absence of GNU parallel you can use a while read loop:

#!/bin/bash

constant=constant

populate_file () {
    local const=$1
    local file=$(basename -s '.M0.ctl' "$2")
    printf '%s\n%s\n%s\n' \
    "seqfile = ${file}_p.phy" \
    "treefile = ${const}.txt" \
    "outfile = ${file}_M0_mlc" > "$2"
}

while IFS= read -r file; do
    populate_file "$constant" "${file/ /}.M0.ctl"
done < list.txt
jesse_b
  • 37,005
  • Thanks, but sorry that it shows an error that parallel: command not found. I think I should ask another question. – Debajyoti Kabiraj Jan 23 '20 at 15:47
  • @DebajyotiKabiraj: Install GNU parallel. – jesse_b Jan 23 '20 at 15:48
  • Thanks, @Jesse_b the 2nd one working for me, but there is little problem that it is creating space before the extension in the file name as well as inside the filename.

    seqfile = EOG090W0AK7 _p.phy treefile = SameTREE.txt outfile = EOG090W0AK7 _M0_mlc

    – Debajyoti Kabiraj Jan 24 '20 at 05:15
  • @DebajyotiKabiraj: Ah, it sounds like your file may have spaces at the end of each line, I'll update a bit later to remove them – jesse_b Jan 24 '20 at 12:03
  • @DebajyotiKabiraj: I've updated the second solution to remove the whitespace – jesse_b Jan 24 '20 at 14:05
  • No difference, same output... – Debajyoti Kabiraj Jan 24 '20 at 14:45
  • Well do some investigating into what is wrong with your input then. We have identified that your input has some weird whitespace issues and I don't have your input so you need to do a little leg work and figure it out. If there were trailing tabs it wouldn't produce the same output you are describing so I don't know what the problem is. Maybe you just need to dos2unix your file or something. – jesse_b Jan 24 '20 at 14:49
0

You can try something like: for i in $(cat list.txt); do touch $i; done

BulletBob
  • 646
0

Another way

tr -s '[:blank:]' '[\n*]' < list.txt | 
  while IFS= read -r word; do
   touch "$word";
  done

This answer has been provided with the assumption that all "words" in file was in the same (first) line and where separated by blank characters.

Paulo Tomé
  • 3,782
0
#!/bin/bash

tr -d '[:blank:]' < list.txt > outputFile.tmp

for i in $(cat outputFile.tmp)
do
  echo "seqfile = ${i}_p.phy" >> ${i}_M0.ctl
  echo "treefile = constant.txt" >> ${i}_M0.ctl
  echo "outfile = ${i}_M0_mlc" >> ${i}_M0.ctl
done
exit 0

Explanation:

  1. tr -d '[:blank:]' < list.txt > outputFile.tmp will remove blanks from list and copy it to outputFile.tmp
  2. for loop reads all lines from outputFile.tmp and adds the necessary context to the files by creating them on the fly.
  • Hello, Thanks The desired results is not coming,...

    seqfile = EOG090W01HI _M0.ctl_p.phy treefile = constant.txt outfile = EOG090W01HI _M0.ctl_M0_mlc

    The space is still there and in addition _M0.ctl is included in in the text which is not needed and spaces are still remained

    – Debajyoti Kabiraj Jan 24 '20 at 07:16
  • I have updated the answer; now please try. – Vikram Tidake Jan 24 '20 at 09:43
  • Hi Thanks, I tried it gives different kind of outcome.. The file name EOG090W076L?.ctl and inside the file name the results are like... _p.phye = EOG090W076L treefile = SameTree.txt _M0_mlc = EOG090W076L – Debajyoti Kabiraj Jan 24 '20 at 10:28
  • This script was verified and it worked perfectly at my machine. I am using Cygwin terimnal. Try doing dos2unix on your list.txt and then try the script. Don't copy the script using copy paste. Create new script by typing the lines. – Vikram Tidake Jan 25 '20 at 10:17